import { FastifyInstance } from "fastify";
import { WatchListService } from "@shieldai/darkwatch";
import { IdentifierType } from "@shieldai/types";

export function watchlistRoutes(fastify: FastifyInstance) {
  const service = new WatchListService();

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

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

    const item = await service.addItem(userId, body.identifierType as IdentifierType, body.identifierValue);
    return reply.code(201).send(item);
  });

  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 items = await service.listItems(userId);
    return reply.send(items);
  });

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

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

    const result = await service.removeItem(userId, request.params.id);
    return reply.send({ count: result.count });
  });
}
