import { FastifyInstance } from "fastify";
import { ScanService } from "@shieldai/darkwatch";
import { DataSource } from "@shieldai/types";

export function scanRoutes(fastify: FastifyInstance) {
  const scanService = new ScanService();

  fastify.post("/", async (request, reply) => {
    const userId = (request.user as { id: string })?.id;

    if (!userId) {
      return reply.code(401).send({ error: "User not authenticated" });
    }

    const body = request.body as { source?: string };
    const source = body.source ? (body.source as DataSource) : undefined;
    const resultCount = await scanService.runScan(userId, source);

    return reply.code(200).send({ scanned: true, resultCount });
  });

  fastify.get("/history", async (request, reply) => {
    const userId = (request.user as { id: string })?.id;

    if (!userId) {
      return reply.code(401).send({ error: "User not authenticated" });
    }

    const history = await scanService.getScanHistory(userId);
    return reply.send(history);
  });
}
