/**
 * /api/v1/agents — Agent CRUD.
 *
 * Mirrors `routes/llms.ts` shape: list / get-by-id-or-name / POST / PUT /
 * DELETE. RBAC is enforced by the global hook in `main.ts:mapUrlToPermission`
 * — the resource is `agents`. The chat endpoints live in `agent-chat.ts` and
 * map to `run:agents:<name>`.
 */
import type { FastifyInstance } from 'fastify';
import type { AgentService } from '../services/agent.service.js';
import { NotFoundError, ConflictError } from '../services/mcp-server.service.js';

export function registerAgentRoutes(
  app: FastifyInstance,
  service: AgentService,
): void {
  app.get('/api/v1/agents', async () => {
    return service.list();
  });

  app.get<{ Params: { id: string } }>('/api/v1/agents/:id', async (request, reply) => {
    try {
      return await getByIdOrName(service, request.params.id);
    } catch (err) {
      if (err instanceof NotFoundError) {
        reply.code(404);
        return { error: err.message };
      }
      throw err;
    }
  });

  app.post('/api/v1/agents', async (request, reply) => {
    try {
      const ownerId = request.userId ?? 'system';
      const row = await service.create(request.body, ownerId);
      reply.code(201);
      return row;
    } catch (err) {
      if (err instanceof ConflictError) {
        reply.code(409);
        return { error: err.message };
      }
      if (err instanceof NotFoundError) {
        reply.code(400);
        return { error: err.message };
      }
      throw err;
    }
  });

  app.put<{ Params: { id: string } }>('/api/v1/agents/:id', async (request, reply) => {
    try {
      const target = await getByIdOrName(service, request.params.id);
      return await service.update(target.id, request.body);
    } catch (err) {
      if (err instanceof NotFoundError) {
        reply.code(404);
        return { error: err.message };
      }
      throw err;
    }
  });

  app.delete<{ Params: { id: string } }>('/api/v1/agents/:id', async (request, reply) => {
    try {
      const target = await getByIdOrName(service, request.params.id);
      await service.delete(target.id);
      reply.code(204);
      return null;
    } catch (err) {
      if (err instanceof NotFoundError) {
        reply.code(404);
        return { error: err.message };
      }
      throw err;
    }
  });

  // GET /api/v1/projects/:projectName/agents — used by mcplocal's agents
  // plugin to enumerate agents for the bound project. Matches the existing
  // /projects/:p/servers endpoint convention.
  app.get<{ Params: { projectName: string } }>(
    '/api/v1/projects/:projectName/agents',
    async (request, reply) => {
      try {
        return await service.listByProject(request.params.projectName);
      } catch (err) {
        if (err instanceof NotFoundError) {
          reply.code(404);
          return { error: err.message };
        }
        throw err;
      }
    },
  );
}

const CUID_RE = /^c[a-z0-9]{24}/i;

async function getByIdOrName(service: AgentService, idOrName: string) {
  if (CUID_RE.test(idOrName)) {
    return service.getById(idOrName);
  }
  return service.getByName(idOrName);
}
