import { z } from "zod";

/**
 * Contact Form Validation Schema
 *
 * Validates contact form submissions with strict security rules:
 * - Name: 2-100 characters, letters/spaces/hyphens only
 * - Email: RFC 5322 compliant, max 254 chars
 * - Company: Optional, max 200 chars
 * - Message: 10-2000 characters
 * - Honeypot: Must be empty (anti-bot)
 */
export const contactFormSchema = z.object({
  // Nom: 2-100 caractères, alphanumérique + espaces/tirets
  name: z
    .string()
    .min(2, "Le nom doit contenir au moins 2 caractères")
    .max(100, "Le nom ne peut dépasser 100 caractères")
    .regex(/^[a-zA-ZÀ-ÿ\s'-]+$/, "Caractères invalides dans le nom"),

  // Email: validation stricte RFC 5322
  email: z
    .string()
    .email("Adresse email invalide")
    .max(254, "Email trop long")
    .toLowerCase(),

  // Entreprise: optionnel, 200 caractères max
  company: z
    .string()
    .max(200, "Nom d'entreprise trop long")
    .optional()
    .or(z.literal("")),

  // Téléphone: optionnel, format flexible
  phone: z
    .string()
    .max(20, "Numéro de téléphone trop long")
    .optional()
    .or(z.literal("")),

  // Message: 10-2000 caractères
  message: z
    .string()
    .min(10, "Le message doit contenir au moins 10 caractères")
    .max(2000, "Le message ne peut dépasser 2000 caractères")
    .trim(),

  // Honeypot (anti-bot): doit être vide
  website: z.string().max(0, "Spam détecté").optional(),
});

export type ContactFormData = z.infer<typeof contactFormSchema>;

/**
 * Type for contact form state (used with useActionState)
 */
export type ContactFormState = {
  errors?: {
    name?: string[];
    email?: string[];
    company?: string[];
    message?: string[];
    _form?: string[];
  };
  success?: boolean;
  message?: string;
};
