import { z } from "zod";

const EDUCATIONS = ['Matric', 'Intermediate', 'Bachelor', 'Master'] as const;
const INTERESTED_COUNTRIES = ['Italy', 'France', 'Turkey', 'China', 'Cyprus', 'Sweden', 'Finland', 'South Korea', 'UK'] as const;
const OFFICE_LOCATIONS = ['Lahore', 'Islamabad', 'Karachi', 'Faisalabad', 'Online Consultation'] as const;
const APPLY_FOR_OPTIONS = ['Study Visa', 'Visit Visa', 'IELTS'] 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()
  .min(1, "Email is required")
  .email("Invalid email address")
  .transform((s) => s.trim().toLowerCase());

// Enhanced phone schema to detect country codes in phone number
const phoneSchema = z.string()
  .min(1, "Phone number is required")
  .transform((s) => s.trim())
  .refine(
    (s) => /^[0-9]{7,15}$/.test(s), // Only numbers, no symbols or country codes
    "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"
  )

// Phone code validation
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)"
  );

export const FreeConsultationSchemaSoft = z.object({
  name: trimmed(2, 80),
  email: emailSchema,
  phone_number: phoneSchema,
  phonecode: phoneCodeSchema, // Added phonecode as required field
  last_education: z.enum(EDUCATIONS, {
    errorMap: () => ({ message: "Select a valid last education" }),
  }),
  interested_country: z.enum(INTERESTED_COUNTRIES, {
    errorMap: () => ({ message: "Select a valid interested country" }),
  }),
  apply_for: z.enum(APPLY_FOR_OPTIONS, {
    errorMap: () => ({ message: "Select a valid apply for option" }),
  }),
  office_to_visit: z.enum(OFFICE_LOCATIONS, {
    errorMap: () => ({ message: "Select a valid office location" }),
  }),
})