import { z } from "zod";

export const contactSchema = z.object({
  firstName: z
    .string()
    .trim()
    .nonempty("First Name is required")
    .min(3, "First Name must be at least 3 characters")
    .max(50, "First Name cannot exceed 50 characters")
    .regex(
      /^[A-Za-z]+(?: [A-Za-z]+)?$/,
      "First Name can only contain letters and at most one space",
    ),

  lastName: z
    .string()
    .trim()
    .nonempty("Last Name is required")
    .min(3, "Last Name must be at least 3 characters")
    .max(50, "Last Name cannot exceed 50 characters")
    .regex(
      /^[A-Za-z]+(?: [A-Za-z]+)?$/,
      "Last Name can only contain letters and at most one space",
    ),

  email: z
    .string()
    .trim()
    .min(1, "Please enter your email address")
    .email("Please enter a valid email address (e.g. john@example.com)")
    .refine((email) => {
      const [, domain] = email.split("@");

      if (!domain) return false;

      const labels = domain.split(".");

      return labels.every(
        (label) =>
          label.length > 0 && !label.startsWith("-") && !label.endsWith("-"),
      );
    }, "Please enter a valid email address (e.g. john@example.com)"),

  mobileNumber: z.object({
    dialCode: z.string().min(1, "Dial code is required"),
    number: z
      .string()
      .min(10, "Mobile number cannot be less than 10 digits")
      .max(15, "Mobile number cannot be more than 15 digits"),
  }),

  query: z
    .string()
    .trim()
    .min(10, "Query must be at least 10 characters")
    .max(1000, "Query cannot exceed 1000 characters"),
});

export type ContactFormValues = z.infer<typeof contactSchema>;
