import { wrap } from "@typeschema/valibot";
import { createTRPCRouter, protectedProcedure } from "../utils";
import {
  CreateRemovalRequestSchema,
  RequestStatusSchema,
  ScanListingsSchema,
  BrokerListingsFilterSchema,
  RemovalRequestsFilterSchema,
} from "../schemas/removebrokers";
import * as removebrokersService from "~/server/services/removebrokers.service";

export const removebrokersRouter = createTRPCRouter({
  getBrokerRegistry: protectedProcedure.query(async () => {
    return removebrokersService.getBrokerRegistry();
  }),

  getRemovalRequests: protectedProcedure
    .input(wrap(RemovalRequestsFilterSchema))
    .query(async ({ ctx, input }) => {
      return removebrokersService.getRemovalRequests(ctx.user.id, input);
    }),

  createRemovalRequest: protectedProcedure
    .input(wrap(CreateRemovalRequestSchema))
    .mutation(async ({ ctx, input }) => {
      return removebrokersService.createRemovalRequest(ctx.user.id, input.brokerId, input.personalInfo);
    }),

  getRequestStatus: protectedProcedure
    .input(wrap(RequestStatusSchema))
    .query(async ({ ctx, input }) => {
      return removebrokersService.getRequestStatus(ctx.user.id, input.requestId);
    }),

  getBrokerListings: protectedProcedure
    .input(wrap(BrokerListingsFilterSchema))
    .query(async ({ ctx, input }) => {
      return removebrokersService.getBrokerListings(ctx.user.id, input);
    }),

  scanForListings: protectedProcedure
    .input(wrap(ScanListingsSchema))
    .mutation(async ({ ctx, input }) => {
      return removebrokersService.scanForListings(ctx.user.id, input.brokerId);
    }),

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