import { z } from "zod"


const mobileNumberRegex = /^[0-9]\d{9}$/;

export  const employeeSchema = 
     z.object({
    firstName: z.string().min(1, { message: "First Name is required" }),
    middleName: z.string().optional(),
    lastName: z.string().min(1, { message: "Last Name is required" }),
    email: z
      .string()
      .max(80, "Email cannot exceed 80 characters")
      .optional()
      .refine((val) => !val || /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val), {
        message: "Invalid email format",
      })
      .refine(
        (val) => {
          if (!val) return true;
          const [local, domain] = val.split("@");
          return (
            val.length <= 320 && local?.length <= 64 && domain?.length <= 255
          );
        },
        {
          message: "Email local or domain part is too long",
        }
      ),
    currentAddress: z
      .string()
      .min(1, { message: "Current Address is required" })
      .max(500, { message: "Address cannot exceed 500 characters" }),
    birthDate: z
      .string()
      .min(1, { message: "Birth Date is required" })
      .refine(
        (date, ctx) => {
          return new Date(date) <= new Date();
        },
        {
          message: "Birth date cannot be in the future",
        }
      ),
    joiningDate: z
      .string()
      .min(1, { message: "Joining Date is required" })
      .refine(
        (date, ctx) => {
          return new Date(date) <= new Date();
        },
        {
          message: "Joining date cannot be in the future",
        }
      ),
    emergencyPhoneNumber: z
      .string()
      .min(1, { message: "Phone Number is required" })
      .regex(mobileNumberRegex, { message: "Invalid phone number " }),
    emergencyContactPerson: z
      .string()
      .min(1, { message: "Emergency Contact Person is required" })
      .regex(/^[A-Za-z\s]+$/, {
        message: "Emergency Contact Person must contain only letters",
      }),
    aadharNumber: z
      .string()
      .optional()
      .refine((val) => !val || /^\d{12}$/.test(val), {
        message: "Aadhar card must be exactly 12 digits long",
      }),
    gender: z
      .string()
      .min(1, { message: "Gender is required" })
      .refine((val) => val !== "Select Gender", {
        message: "Please select a gender",
      }),
    panNumber: z
      .string()
      .optional()
      .refine((val) => !val || /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/.test(val), {
        message: "Invalid PAN number",
      }),
    permanentAddress: z
      .string()
      .min(1, { message: "Permanent Address is required" })
      .max(500, { message: "Address cannot exceed 500 characters" }),
    phoneNumber: z
      .string()
      .min(1, { message: "Phone Number is required" })
      .regex(mobileNumberRegex, { message: "Invalid phone number " }),
    jobRoleId: z.string().min(1, { message: "Role is required" }),
    organizationId:z.string().min(1,{message:"Organization is required"}),
    hasApplicationAccess:z.boolean().default(false),
  }).refine((data) => {
  if (data.hasApplicationAccess) {
    return data.email && data.email.trim() !== "";
  }
  return true; 
}, {
  message: "Email is required when employee has access",
  path: ["email"], 
});

 
export const defatEmployeeObj = {
      firstName:  "",
      middleName: "",
      lastName: "",
      email:  "",
      currentAddress: "",
      birthDate:  "",
      joiningDate: "",
      emergencyPhoneNumber: "",
      emergencyContactPerson:  "",
      aadharNumber: "",
      gender:  "",
      panNumber: "",
      permanentAddress:  "",
      phoneNumber:  "",
      jobRoleId: null,
      organizationId:"",
      hasApplicationAccess:false
    }