import * as z from "zod";

/**
 * Landing-page contact form. Messages are dictionary keys (resolved through
 * `t()` by FormField, per the auth-schema convention). Phone is optional but,
 * when provided, must be a Saudi E.164 mobile number (matches PhoneInput's
 * `+9665XXXXXXXX` output).
 */
export const contactSchema = z
  .object({
    fullName: z
      .string()
      .trim()
      .min(1, { message: "home.contact.validation.fullNameRequired" }),
    email: z
      .string()
      .trim()
      .min(1, { message: "home.contact.validation.emailRequired" })
      .email({ message: "home.contact.validation.emailInvalid" }),
    phone: z.string().optional(),
    subject: z
      .string()
      .trim()
      .min(1, { message: "home.contact.validation.subjectRequired" }),
    message: z
      .string()
      .trim()
      .min(1, { message: "home.contact.validation.messageRequired" })
      .min(10, { message: "home.contact.validation.messageMin" }),
  })
  .superRefine((data, ctx) => {
    if (data.phone && !/^\+9665\d{8}$/.test(data.phone)) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        path: ["phone"],
        message: "home.contact.validation.phoneInvalid",
      });
    }
  });

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