import { z } from "zod"

export const recipientSchema = z.object({
  id: z.string().optional(),
  username: z
    .string()
    .min(1, { message: "Username is required" })
    .nonempty("Username is required"),
  firstName: z.string().min(1, { message: "First name is required" }),
  lastName: z.string().min(1, { message: "Last name is required" }),
  department: z.enum(
    [
      "IT",
      "ENGINEERING",
      "TRAFFIC",
      "DRIVER",
      "LOGISTICS",
      "ADMINISTRATION",
      "SALES",
      "OTHER",
    ],
    {
      message: "Department is required",
    },
  ),
  email: z.string().optional().nullable(),
  phone: z.string().optional().nullable(),
})

export const createRecipientSchema = recipientSchema.superRefine(
  (data, ctx) => {
    if (data.email && !z.string().email().safeParse(data.email).success) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        message: "Email format is invalid",
        path: ["email"],
      })
    }
  },
)

export type CreateRecipientFormType = z.infer<typeof createRecipientSchema>

export const updateRecipientSchema = recipientSchema.extend({
  id: z.string().nonempty("ID is required"),
})

export type UpdateRecipientFormType = z.infer<typeof updateRecipientSchema>
