import { wrap } from "@typeschema/valibot";
import { createTRPCRouter, protectedProcedure } from "../utils";
import {
  CreateEnrollmentSchema,
  DeleteEnrollmentSchema,
  AnalyzeAudioSchema,
  AnalysisFilterSchema,
  AnalysisResultSchema,
  JobStatusSchema,
} from "../schemas/voiceprint";
import * as voiceprintService from "~/server/services/voiceprint.service";

export const voiceprintRouter = createTRPCRouter({
  getEnrollments: protectedProcedure.query(async ({ ctx }) => {
    return voiceprintService.getEnrollments(ctx.user.id);
  }),

  createEnrollment: protectedProcedure
    .input(wrap(CreateEnrollmentSchema))
    .mutation(async ({ ctx, input }) => {
      return voiceprintService.createEnrollment(ctx.user.id, input.name, input.audioBase64);
    }),

  deleteEnrollment: protectedProcedure
    .input(wrap(DeleteEnrollmentSchema))
    .mutation(async ({ ctx, input }) => {
      return voiceprintService.deleteEnrollment(ctx.user.id, input.enrollmentId);
    }),

  analyzeAudio: protectedProcedure
    .input(wrap(AnalyzeAudioSchema))
    .mutation(async ({ ctx, input }) => {
      return voiceprintService.analyzeAudio(ctx.user.id, input.audioBase64, input.enrollmentId);
    }),

  getAnalyses: protectedProcedure
    .input(wrap(AnalysisFilterSchema))
    .query(async ({ ctx, input }) => {
      return voiceprintService.getAnalyses(ctx.user.id, input);
    }),

  getAnalysisResult: protectedProcedure
    .input(wrap(AnalysisResultSchema))
    .query(async ({ ctx, input }) => {
      return voiceprintService.getAnalysisResult(ctx.user.id, input.analysisId);
    }),

  getJobStatus: protectedProcedure
    .input(wrap(JobStatusSchema))
    .query(async ({ ctx, input }) => {
      return voiceprintService.getJobStatus(ctx.user.id, input.jobId);
    }),
});
