import { z } from "zod"

export const assignmentSchema = z.object({
  id: z.string().optional(),
  quantity: z.coerce
    .number()
    .int()
    .nonnegative()
    .min(1, { message: "Quantity is required" }),
  notes: z.string().optional(),
  itemId: z.string().min(1, { message: "Item is required" }).optional(),
  assetId: z.string().optional(),
  recipientId: z.string().min(1, { message: "Recipient is required" }),
  assignmentDate: z.date().optional(),
  returnDate: z.date().optional(),
})

export const createAssignmentSchema = assignmentSchema.omit({
  id: true,
  returnDate: true,
})
export type CreateAssignmentFormType = z.infer<typeof createAssignmentSchema>

export const updateAssignmentSchema = assignmentSchema
  .omit({
    returnDate: true,
  })
  .superRefine((data, ctx) => {
    if (data.itemId && !data.assetId) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        message: "Asset ID is required when item ID is provided",
        path: ["assetId"],
      })
    }
  })

export type UpdateAssignmentFormType = z.infer<typeof updateAssignmentSchema>

export const returnAssignmentSchema = z.object({
  id: z.string().min(1, { message: "Assignment ID is required" }),
})
export type ReturnAssignmentFormType = z.infer<typeof returnAssignmentSchema>
