import { Injectable } from '@nestjs/common';
import { TrpcService } from '@server/trpc/trpc.service';
import { Prisma, UpdateOrderSchema } from '@nice/common';
import { ResourceService } from './resource.service';
import { z, ZodType } from 'zod';

const ResourceCreateArgsSchema: ZodType<Prisma.ResourceCreateArgs> = z.any();
const ResourceCreateManyInputSchema: ZodType<Prisma.ResourceCreateManyInput> =
  z.any();
const ResourceDeleteManyArgsSchema: ZodType<Prisma.ResourceDeleteManyArgs> =
  z.any();
const ResourceFindManyArgsSchema: ZodType<Prisma.ResourceFindManyArgs> =
  z.any();
const ResourceFindFirstArgsSchema: ZodType<Prisma.ResourceFindFirstArgs> =
  z.any();
const ResourceWhereInputSchema: ZodType<Prisma.ResourceWhereInput> = z.any();
const ResourceSelectSchema: ZodType<Prisma.ResourceSelect> = z.any();

@Injectable()
export class ResourceRouter {
  constructor(
    private readonly trpc: TrpcService,
    private readonly resourceService: ResourceService,
  ) {}
  router = this.trpc.router({
    create: this.trpc.protectProcedure
      .input(ResourceCreateArgsSchema)
      .mutation(async ({ ctx, input }) => {
        const { staff } = ctx;
        return await this.resourceService.create(input, { staff });
      }),
    createMany: this.trpc.protectProcedure
      .input(z.array(ResourceCreateManyInputSchema))
      .mutation(async ({ ctx, input }) => {
        const { staff } = ctx;

        return await this.resourceService.createMany({ data: input }, staff);
      }),
    deleteMany: this.trpc.procedure
      .input(ResourceDeleteManyArgsSchema)
      .mutation(async ({ input }) => {
        return await this.resourceService.deleteMany(input);
      }),
    findFirst: this.trpc.procedure
      .input(ResourceFindFirstArgsSchema) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword
      .query(async ({ input }) => {
        return await this.resourceService.findFirst(input);
      }),
    softDeleteByIds: this.trpc.protectProcedure
      .input(z.object({ ids: z.array(z.string()) })) // expect input according to the schema
      .mutation(async ({ input }) => {
        return this.resourceService.softDeleteByIds(input.ids);
      }),
    updateOrder: this.trpc.protectProcedure
      .input(UpdateOrderSchema)
      .mutation(async ({ input }) => {
        return this.resourceService.updateOrder(input);
      }),
    findMany: this.trpc.procedure
      .input(ResourceFindManyArgsSchema) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword
      .query(async ({ input }) => {
        return await this.resourceService.findMany(input);
      }),
    findManyWithCursor: this.trpc.protectProcedure
      .input(
        z.object({
          cursor: z.any().nullish(),
          take: z.number().nullish(),
          where: ResourceWhereInputSchema.nullish(),
          select: ResourceSelectSchema.nullish(),
        }),
      )
      .query(async ({ ctx, input }) => {
        const { staff } = ctx;
        return await this.resourceService.findManyWithCursor(input);
      }),
  });
}
