/**
 * Libellés traduits des valeurs de l'écran, en appels `t()` LITTÉRAUX : le test
 * des traductions (`automatisations-translations.test.ts`) lit les sources et
 * vérifie que chaque clé existe dans les deux fichiers de langue.
 */
import type { AutomationSource, AutomationTrigger } from '@/types/automatisations';

const NS = 'automatisations';

export type Translate = (namespace: string, key: string, fallback?: string) => string;

export function sourceLabel(source: AutomationSource, t: Translate): string {
    switch (source) {
        case 'airbnb':
            return t(NS, 'source_airbnb', 'Airbnb');
        case 'booking':
            return t(NS, 'source_booking', 'Booking.com');
        case 'expedia':
            return t(NS, 'source_expedia', 'Expedia');
        case 'site':
            return t(NS, 'source_site', 'Site thelandlord.tn');
        case 'local':
            return t(NS, 'source_local', 'Réservations locales');
        case 'other':
            return t(NS, 'source_other', 'Autres');
    }
}

export function triggerLabel(trigger: AutomationTrigger, t: Translate): string {
    switch (trigger) {
        case 'booking_confirmed':
            return t(NS, 'trigger_booking_confirmed', 'Après la réservation');
        case 'before_checkin':
            return t(NS, 'trigger_before_checkin', 'Avant l’arrivée');
        case 'checkin_day':
            return t(NS, 'trigger_checkin_day', 'Le jour de l’arrivée');
        case 'during_stay':
            return t(NS, 'trigger_during_stay', 'Pendant le séjour');
        case 'before_checkout':
            return t(NS, 'trigger_before_checkout', 'Avant le départ');
        case 'checkout_day':
            return t(NS, 'trigger_checkout_day', 'Le jour du départ');
        case 'after_checkout':
            return t(NS, 'trigger_after_checkout', 'Après le départ');
    }
}

/**
 * Une erreur de validation : texte du serveur tel quel, ou clé du schéma de
 * l'écran (`validation_xxx`, éventuellement `validation_xxx|valeur`) traduite.
 */
export function errorText(message: string, t: Translate): string {
    const match = /^(validation_[a-z_]+)(?:\|(.*))?$/.exec(message);
    if (!match) return message;

    const text = t(NS, match[1], match[1]);

    return match[2] !== undefined ? text.split(':token').join(match[2]) : text;
}
