import { objectSchema } from '../../kernel/input-schema.mjs';
import { getDeploymentService } from './index.mjs';

const settingsSchema = objectSchema({
  provider: { type: 'string', enum: ['none', 'coolify'] },
  coolify_url: { type: 'string', maxLength: 1000 },
  resource_uuid: { type: 'string', maxLength: 120 },
  trigger_mode: { type: 'string', enum: ['webhook', 'api'] },
  deploy_method: { type: 'string', enum: ['GET', 'POST'] },
  deploy_endpoint: { type: 'string', maxLength: 500 },
  github_repository: { type: 'string', maxLength: 200 },
  github_branch: { type: 'string', maxLength: 120 },
  ghcr_image: { type: 'string', maxLength: 300 },
  update_channel: { type: 'string', enum: ['stable', 'beta'] },
  backup_before_deploy: { type: 'boolean' },
  backup_uploads: { type: 'boolean' },
  api_token: { type: 'string', maxLength: 16384, trim: false },
  deploy_webhook_url: { type: 'string', maxLength: 2000, trim: false },
});
const triggerSchema = objectSchema({
  force: { type: 'boolean' }, confirmation: { type: 'string', required: true, maxLength: 20 }, reason: { type: 'string', maxLength: 120 },
});

export function registerDeploymentRoutes({ app, routes, deps }) {
  routes.register(app, {
    id: 'deployments.status.read', method: 'GET', path: '/api/admin/deployment', owner: 'vendoo.deployments',
    policy: 'updates.manage', audit: 'deployments.status.read', rateLimit: 'administration', csrf: false, stability: 'stable',
    handler: async (_req, res) => res.json(getDeploymentService().status()),
  });
  routes.register(app, {
    id: 'deployments.settings.update', method: 'PUT', path: '/api/admin/deployment', owner: 'vendoo.deployments',
    policy: 'updates.manage', audit: 'deployments.settings.updated', rateLimit: 'administration', csrf: true, stability: 'stable', input: settingsSchema,
    handler: async (req, res) => {
      const result = getDeploymentService().update(req.validatedBody || {});
      deps.auditLog(req.user.id, req.user.email, 'deployment.settings.updated', 'deployment', 'coolify', null, deps.getClientIp(req));
      res.json(result);
    },
  });
  routes.register(app, {
    id: 'deployments.connection.test', method: 'POST', path: '/api/admin/deployment/test', owner: 'vendoo.deployments',
    policy: 'updates.manage', audit: 'deployments.connection.tested', rateLimit: 'administration', csrf: true, stability: 'stable',
    handler: async (req, res) => {
      const result = await getDeploymentService().testConnection();
      deps.auditLog(req.user.id, req.user.email, 'deployment.connection.tested', 'deployment', 'coolify', JSON.stringify({ ok: result.ok }), deps.getClientIp(req));
      res.json(result);
    },
  });
  routes.register(app, {
    id: 'deployments.updates.check', method: 'POST', path: '/api/admin/deployment/updates/check', owner: 'vendoo.deployments',
    policy: 'updates.manage', audit: 'deployments.updates.checked', rateLimit: 'administration', csrf: true, stability: 'stable',
    handler: async (_req, res) => res.json(await getDeploymentService().checkUpdates()),
  });
  routes.register(app, {
    id: 'deployments.trigger', method: 'POST', path: '/api/admin/deployment/trigger', owner: 'vendoo.deployments',
    policy: 'updates.manage', audit: 'deployments.triggered', rateLimit: 'administration', csrf: true, stability: 'stable', input: triggerSchema,
    handler: async (req, res) => {
      const result = await getDeploymentService().trigger(req.validatedBody || {});
      deps.auditLog(req.user.id, req.user.email, 'deployment.triggered', 'deployment', 'coolify', JSON.stringify({ backup_id: result.backup_id, branch: result.github_branch }), deps.getClientIp(req));
      res.status(202).json(result);
    },
  });
}
