import { wrap } from "@typeschema/valibot";
import { createTRPCRouter, protectedProcedure } from "../utils";
import {
  AddWatchlistItemSchema,
  RemoveWatchlistItemSchema,
  ExposureFilterSchema,
  ExposureDetailsSchema,
  RunScanSchema,
  ReportFilterSchema,
} from "../schemas/darkwatch";
import * as darkwatchService from "~/server/services/darkwatch.service";

export const darkwatchRouter = createTRPCRouter({
  getWatchlist: protectedProcedure.query(async ({ ctx }) => {
    return darkwatchService.getWatchlistItems(ctx.user.id);
  }),

  addWatchlistItem: protectedProcedure
    .input(wrap(AddWatchlistItemSchema))
    .mutation(async ({ ctx, input }) => {
      return darkwatchService.addWatchlistItem(ctx.user.id, input.type, input.value);
    }),

  removeWatchlistItem: protectedProcedure
    .input(wrap(RemoveWatchlistItemSchema))
    .mutation(async ({ ctx, input }) => {
      return darkwatchService.removeWatchlistItem(ctx.user.id, input.itemId);
    }),

  getExposures: protectedProcedure
    .input(wrap(ExposureFilterSchema))
    .query(async ({ ctx, input }) => {
      return darkwatchService.getExposures(ctx.user.id, input);
    }),

  getExposureDetails: protectedProcedure
    .input(wrap(ExposureDetailsSchema))
    .query(async ({ ctx, input }) => {
      return darkwatchService.getExposureDetails(ctx.user.id, input.exposureId);
    }),

  runScan: protectedProcedure
    .input(wrap(RunScanSchema))
    .mutation(async ({ ctx }) => {
      return darkwatchService.runScan(ctx.user.id);
    }),

  getScanStatus: protectedProcedure.query(async ({ ctx }) => {
    return darkwatchService.getScanStatus(ctx.user.id);
  }),

  getReports: protectedProcedure
    .input(wrap(ReportFilterSchema))
    .query(async ({ ctx, input }) => {
      return darkwatchService.getReports(ctx.user.id, input);
    }),
});
