import { z } from "zod";

const EDUCATIONS = ['Matric', 'Intermediate', 'Bachelor', 'Master'] as const;
const OFFICE_LOCATIONS = ['Lahore', 'Islamabad', 'Karachi', 'Faisalabad', 'Online Consultation'] as const;

const trimmed = (min = 1, max = 200) =>
  z.string()
    .transform((s) => s.trim().replace(/\s+/g, " "))
    .pipe(z.string().min(min, `Field must be at least ${min} character(s)`).max(max, `Field must not exceed ${max} characters`));

const emailSchema = z.string()
  .optional()
  .refine((email) => !email || /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email), {
    message: "Invalid email address"
  })
  .transform((s) => s?.trim().toLowerCase() || '');

const phoneSchema = z.string()
  .min(1, "Phone number is required")
  .transform((s) => s.trim())
  .refine(
    (s) => /^[0-9]{7,15}$/.test(s),
    "Phone number should contain only numbers (7-15 digits)"
  )
  .refine(
    (s) => !s.startsWith('+'), 
    "Do not include country code in phone number - it's already selected separately"
  )

const phoneCodeSchema = z.string()
  .min(1, "Country code is required")
  .refine(
    (code) => /^[1-9]\d{0,3}$/.test(code),
    "Invalid country code format. It should be numbers only (e.g., 92, 1, 44)"
  );

const percentageSchema = z.string()
  .min(1, "Percentage is required")
  .refine(
    (percent) => /^(100(\.0{1,2})?|[1-9]?\d(\.\d{1,2})?)$/.test(percent),
    "Enter a valid percentage (0-100)"
  );

export const DiscountOfferSchema = z.object({
  name: trimmed(2, 80),
  email: emailSchema,
  phone_number: phoneSchema,
  phonecode: phoneCodeSchema,
  last_education: z.enum(EDUCATIONS, {
    errorMap: () => ({ message: "Select a valid last education" }),
  }),
  last_education_per: percentageSchema,
  office_location: z.enum(OFFICE_LOCATIONS, {
    errorMap: () => ({ message: "Select a valid office location" }),
  }),
});