import { wrap } from "@typeschema/valibot";
import { createTRPCRouter, protectedProcedure } from "../utils";
import {
  AddPropertySchema,
  RemovePropertySchema,
  GetSnapshotsSchema,
  GetChangesSchema,
  RunScanSchema,
} from "../schemas/hometitle";
import * as hometitleService from "~/server/services/hometitle.service";

export const hometitleRouter = createTRPCRouter({
  getProperties: protectedProcedure.query(async ({ ctx }) => {
    return hometitleService.getProperties(ctx.user.id);
  }),

  addProperty: protectedProcedure
    .input(wrap(AddPropertySchema))
    .mutation(async ({ ctx, input }) => {
      return hometitleService.addProperty(ctx.user.id, input.address, input.parcelId, input.ownerName);
    }),

  removeProperty: protectedProcedure
    .input(wrap(RemovePropertySchema))
    .mutation(async ({ ctx, input }) => {
      return hometitleService.removeProperty(ctx.user.id, input.propertyId);
    }),

  getSnapshots: protectedProcedure
    .input(wrap(GetSnapshotsSchema))
    .query(async ({ ctx, input }) => {
      return hometitleService.getSnapshots(ctx.user.id, input.propertyId);
    }),

  getChanges: protectedProcedure
    .input(wrap(GetChangesSchema))
    .query(async ({ ctx, input }) => {
      return hometitleService.getChanges(ctx.user.id, input.propertyId, {
        severity: input.severity,
        changeType: input.changeType,
      });
    }),

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

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