import type { FastifyInstance } from 'fastify';
import type { Sql } from '../db.js';
import { getAgentsForProject } from '../services/agents.js';

export function registerAgentRoutes(app: FastifyInstance, sql: Sql): void {
  app.get<{ Params: { id: string } }>(
    '/api/projects/:id/agents',
    async (req, reply) => {
      const rows = await sql<{ path: string }[]>`
        SELECT path FROM projects WHERE id = ${req.params.id}
      `;
      if (rows.length === 0) {
        reply.code(404);
        return { error: 'project not found' };
      }
      // getAgentsForProject handles AGENTS.md presence/parse/cache; never throws.
      return await getAgentsForProject(rows[0]!.path);
    }
  );
}
