import { z } from 'zod';
import { isValidPhoneNumber } from 'react-phone-number-input';

/**
 * Schéma du formulaire /devenir-applicateur — copie conforme du formulaire client.
 * Champs CONTACT standard + champs ENTREPRISE (clientele, metier, annee_creation, chantier cool roof, source).
 * Requis : prénom, nom, email, téléphone, pays, année création, clientèle, source.
 */

// clientele (multi) — valeurs internes = libellés client
export const clienteleOptions = [
  { value: 'Particuliers', label: 'Particuliers' },
  { value: 'Tertiaire', label: 'Tertiaire' },
  { value: 'Industries', label: 'Industries' },
  { value: 'Grande distribution', label: 'Grande distribution' },
  { value: 'Collectivités', label: 'Collectivités' },
] as const;

const clienteleValues = clienteleOptions.map((o) => o.value) as [string, ...string[]];

export const applicateurSchema = z.object({
  firstName: z.string().min(2, 'Prénom requis'),
  lastName: z.string().min(2, 'Nom requis'),
  email: z.string().email('Email invalide'),
  phone: z
    .string()
    .min(1, 'Téléphone requis')
    .refine((v) => isValidPhoneNumber(v), 'Numéro de téléphone invalide'),
  company: z.string().optional(),
  country: z.string().min(1, 'Pays requis'),
  city: z.string().optional(),
  // Année de création de l'entreprise (number)
  anneeCreation: z
    .number({ invalid_type_error: "Indiquez l'année de création" })
    .int()
    .min(1900, 'Année invalide')
    .max(2100, 'Année invalide'),
  chantierCoolRoof: z.boolean().optional(),
  metier: z.string().optional(),
  clientele: z.array(z.enum(clienteleValues)).min(1, 'Sélectionnez au moins une clientèle'),
  source: z.string().min(1, 'Ce champ est requis'),
  rgpd: z.boolean().refine((v) => v === true, {
    message: 'Consentement requis pour traiter votre demande',
  }),
});

export type ApplicateurFormValues = z.infer<typeof applicateurSchema>;

export const applicateurDefaultValues: Partial<ApplicateurFormValues> = {
  firstName: '',
  lastName: '',
  email: '',
  phone: '',
  company: '',
  country: '',
  city: '',
  anneeCreation: undefined,
  chantierCoolRoof: false,
  metier: '',
  clientele: [],
  source: '',
  rgpd: false,
};

// Libellés humains pour les messages d'erreur exhaustifs.
export const fieldLabels: Record<string, string> = {
  firstName: 'Prénom',
  lastName: 'Nom',
  email: 'E-mail professionnel',
  phone: 'Numéro de téléphone',
  company: "Nom de l'entreprise",
  country: 'Pays/Région',
  city: 'Ville',
  anneeCreation: "Année de création de l'entreprise",
  chantierCoolRoof: 'Chantier cool roof déjà réalisé',
  metier: 'Métier(s)',
  clientele: 'Clientèle',
  source: 'Où avez-vous entendu parler de Covalba',
  rgpd: 'Consentement (case à cocher)',
};
