import { FastifyInstance } from "fastify";
import { AlertPipeline } from "@shieldai/darkwatch";

export function alertRoutes(fastify: FastifyInstance) {
  const pipeline = new AlertPipeline();

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

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

    const limit = parseInt(request.query.limit as string) || 50;
    const offset = parseInt(request.query.offset as string) || 0;
    const alerts = await pipeline.getUserAlerts(userId, limit, offset);
    return reply.send(alerts);
  });

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

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

    await pipeline.markRead(request.params.id, userId);
    return reply.send({ read: true });
  });
}
