<?php
/**
 * Emails Transactionnels AVI - Scripting avec variables dynamiques
 *
 * Variables : {$nextSprintName}, {$currentScore}, {$infusionTimer}
 * Triggers : WELCOME, TIMER_DONE, SPRINT_UNLOCK, L21_FIRST_TIME
 * L21 : CTA Premium si utilisateur pas en Mode Gardien
 */

namespace Avision;

if (!defined('ABSPATH')) exit;

class Avision_Transactional_Emails {

    const TRIGGER_WELCOME = 'WELCOME';
    const TRIGGER_TIMER_DONE = 'TIMER_DONE';
    const TRIGGER_SPRINT_UNLOCK = 'SPRINT_UNLOCK';
    const TRIGGER_L21_FIRST_TIME = 'L21_FIRST_TIME';

    /** Noms des sprints par index (1-7) */
    const SPRINT_NAMES = [
        1 => 'La Fondation',
        2 => 'La Boutique',
        3 => "L'Expansion",
        4 => "L'Expansion Avancée",
        5 => 'La Domination',
        6 => 'La Domination',
        7 => 'La Domination Finale'
    ];

    const SPRINT_NAMES_EN = [
        1 => 'The Foundation',
        2 => 'The Store',
        3 => 'The Expansion',
        4 => 'Advanced Expansion',
        5 => 'Domination',
        6 => 'Domination',
        7 => 'Final Domination'
    ];

    const SPRINT_NAMES_ES = [
        1 => 'La Fundación',
        2 => 'La Tienda',
        3 => 'La Expansión',
        4 => 'Expansión Avanzada',
        5 => 'Dominación',
        6 => 'Dominación',
        7 => 'Dominación Final'
    ];

    private $database;

    public function __construct() {
        $this->database = new Avision_Database();
        $this->database->init();
    }

    /**
     * Récupère le nom du prochain sprint selon la langue
     */
    public function get_next_sprint_name($sprint_index, $lang = 'fr') {
        $idx = (int) $sprint_index;
        if ($idx < 1 || $idx > 7) return 'Sprint ' . $idx;
        $map = $lang === 'en' ? self::SPRINT_NAMES_EN : ($lang === 'es' ? self::SPRINT_NAMES_ES : self::SPRINT_NAMES);
        return $map[$idx] ?? self::SPRINT_NAMES[$idx];
    }

    /**
     * Remplace les variables dynamiques dans un template
     */
    public function substitute_variables($template, $context = []) {
        $vars = [
            '{$nextSprintName}' => $context['nextSprintName'] ?? '',
            '{$currentScore}' => $context['currentScore'] ?? '0',
            '{$infusionTimer}' => $context['infusionTimer'] ?? '',
            '{$companyName}' => $context['companyName'] ?? '',
            '{$dashboardUrl}' => $context['dashboardUrl'] ?? '',
            '{$premiumUrl}' => $context['premiumUrl'] ?? home_url('/premium-dashboard'),
        ];
        return str_replace(array_keys($vars), array_values($vars), $template);
    }

    /**
     * WELCOME - onUserSignUp
     */
    public function send_welcome($audit) {
        $url = $this->get_dashboard_url($audit);
        $template = $this->get_template(self::TRIGGER_WELCOME, $audit['language'] ?? 'fr');
        $context = [
            'nextSprintName' => $this->get_next_sprint_name(1, $audit['language'] ?? 'fr'),
            'currentScore' => $audit['score'] ?? '42',
            'infusionTimer' => '72h',
            'companyName' => $audit['business_name'] ?? '',
            'dashboardUrl' => $url,
            'premiumUrl' => $url,
        ];
        $subject = $this->get_subject(self::TRIGGER_WELCOME, $audit['language'] ?? 'fr');
        $message = $this->substitute_variables($template, $context);
        return wp_mail($audit['email'], $subject, $message, ['Content-Type: text/html; charset=UTF-8']);
    }

    /**
     * TIMER_DONE - onInfusionTimerEnd
     */
    public function send_timer_done($audit, $context) {
        $template = $this->get_template(self::TRIGGER_TIMER_DONE, $audit['language'] ?? 'fr');
        $ctx = array_merge([
            'dashboardUrl' => $this->get_dashboard_url($audit),
            'companyName' => $audit['business_name'] ?? '',
        ], $context);
        $subject = $this->get_subject(self::TRIGGER_TIMER_DONE, $audit['language'] ?? 'fr');
        $message = $this->substitute_variables($template, $ctx);
        return wp_mail($audit['email'], $subject, $message, ['Content-Type: text/html; charset=UTF-8']);
    }

    /**
     * SPRINT_UNLOCK - onSprintValidation (3 niveaux OK)
     */
    public function send_sprint_unlock($audit, $context) {
        $template = $this->get_template(self::TRIGGER_SPRINT_UNLOCK, $audit['language'] ?? 'fr');
        $ctx = array_merge([
            'dashboardUrl' => $this->get_dashboard_url($audit),
            'companyName' => $audit['business_name'] ?? '',
        ], $context);
        $subject = $this->get_subject(self::TRIGGER_SPRINT_UNLOCK, $audit['language'] ?? 'fr');
        $message = $this->substitute_variables($template, $ctx);
        return wp_mail($audit['email'], $subject, $message, ['Content-Type: text/html; charset=UTF-8']);
    }

    /**
     * L21_FIRST_TIME - onLevel21Success (isFirstTime == true)
     * Si !Mode Gardien → CTA Premium puissant pour éviter l'abandon
     */
    public function send_l21_first_time($audit, $context) {
        $is_guardian = !empty($context['isGuardianActive']);
        $template = $this->get_template(self::TRIGGER_L21_FIRST_TIME, $audit['language'] ?? 'fr', $is_guardian);
        $ctx = array_merge([
            'dashboardUrl' => $this->get_dashboard_url($audit),
            'companyName' => $audit['business_name'] ?? '',
            'premiumUrl' => add_query_arg(['audit_id' => $audit['id']], home_url('/premium-dashboard')),
        ], $context);
        $subject = $this->get_subject(self::TRIGGER_L21_FIRST_TIME, $audit['language'] ?? 'fr');
        $message = $this->substitute_variables($template, $ctx);
        return wp_mail($audit['email'], $subject, $message, ['Content-Type: text/html; charset=UTF-8']);
    }

    /**
     * Dispatch selon le trigger
     */
    public function dispatch($trigger, $audit_id, $context = []) {
        $audit = $this->database->get_audit_by_id($audit_id);
        if (!$audit || !$audit['email']) return false;

        switch ($trigger) {
            case self::TRIGGER_WELCOME:
                return $this->send_welcome($audit);
            case self::TRIGGER_TIMER_DONE:
                return $this->send_timer_done($audit, $context);
            case self::TRIGGER_SPRINT_UNLOCK:
                return $this->send_sprint_unlock($audit, $context);
            case self::TRIGGER_L21_FIRST_TIME:
                return $this->send_l21_first_time($audit, $context);
            default:
                return false;
        }
    }

    private function get_dashboard_url($audit) {
        return add_query_arg(['audit_id' => $audit['id']], home_url('/premium-dashboard'));
    }

    private function get_subject($trigger, $lang) {
        $subjects = [
            self::TRIGGER_WELCOME => ['fr' => 'Bienvenue chez AVI – Ton parcours commence', 'en' => 'Welcome to AVI – Your journey starts', 'es' => 'Bienvenido a AVI – Tu camino comienza'],
            self::TRIGGER_TIMER_DONE => ['fr' => 'AVI – Déblocage ! Prochaine étape disponible', 'en' => 'AVI – Unlocked! Next step available', 'es' => 'AVI – ¡Desbloqueado! Siguiente paso disponible'],
            self::TRIGGER_SPRINT_UNLOCK => ['fr' => 'Félicitations – Sprint débloqué !', 'en' => 'Congratulations – Sprint unlocked!', 'es' => '¡Felicidades – Sprint desbloqueado!'],
            self::TRIGGER_L21_FIRST_TIME => ['fr' => 'Parcours 90 jours terminé – Tu empire local est sécurisé', 'en' => '90-day journey complete – Your local empire is secured', 'es' => 'Recorrido de 90 días completado – Tu imperio local está asegurado'],
        ];
        return ($subjects[$trigger][$lang] ?? $subjects[$trigger]['fr']) ?: 'AVI';
    }

    private function get_template($trigger, $lang, $is_guardian = true) {
        $lang = in_array($lang, ['en', 'es']) ? $lang : 'fr';

        if ($trigger === self::TRIGGER_L21_FIRST_TIME && !$is_guardian) {
            return $this->get_l21_premium_cta_template($lang);
        }

        $templates = [
            self::TRIGGER_WELCOME => [
                'fr' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:linear-gradient(135deg,#667eea,#764ba2);padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>👋 Bienvenue chez AVI</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Ton parcours de visibilité locale commence.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p>Bonjour <strong>{$companyName}</strong>,</p>
                        <p>Ton score actuel : <strong>{$currentScore}/100</strong>.</p>
                        <p>Ton prochain objectif : <strong>{$nextSprintName}</strong>. Le premier timer d'infusion est de <strong>{$infusionTimer}</strong>.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#667eea;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;margin-top:15px;'>Accéder au Dashboard</a>
                    </div>
                </body></html>",
                'en' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:linear-gradient(135deg,#667eea,#764ba2);padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>👋 Welcome to AVI</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Your local visibility journey starts.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p>Hello <strong>{$companyName}</strong>,</p>
                        <p>Your current score: <strong>{$currentScore}/100</strong>.</p>
                        <p>Your next goal: <strong>{$nextSprintName}</strong>. First infusion timer: <strong>{$infusionTimer}</strong>.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#667eea;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;margin-top:15px;'>Access Dashboard</a>
                    </div>
                </body></html>",
                'es' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:linear-gradient(135deg,#667eea,#764ba2);padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>👋 Bienvenido a AVI</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Tu camino de visibilidad local comienza.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p>Hola <strong>{$companyName}</strong>,</p>
                        <p>Tu puntuación actual: <strong>{$currentScore}/100</strong>.</p>
                        <p>Tu próximo objetivo: <strong>{$nextSprintName}</strong>. El primer timer de infusión es <strong>{$infusionTimer}</strong>.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#667eea;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;margin-top:15px;'>Acceder al Dashboard</a>
                    </div>
                </body></html>",
            ],
            self::TRIGGER_TIMER_DONE => [
                'fr' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:#10b981;padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>⏰ Déblocage !</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Le timer d'infusion est terminé. Tu peux passer au niveau suivant.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p><strong>{$nextSprintName}</strong> t'attend. Score actuel : <strong>{$currentScore}/100</strong>.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#10b981;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;'>Continuer</a>
                    </div>
                </body></html>",
                'en' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:#10b981;padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>⏰ Unlocked!</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Infusion timer complete. You can move to the next level.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p><strong>{$nextSprintName}</strong> awaits. Current score: <strong>{$currentScore}/100</strong>.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#10b981;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;'>Continue</a>
                    </div>
                </body></html>",
                'es' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:#10b981;padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>⏰ ¡Desbloqueado!</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Timer de infusión completado. Puedes pasar al siguiente nivel.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p><strong>{$nextSprintName}</strong> te espera. Puntuación actual: <strong>{$currentScore}/100</strong>.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#10b981;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;'>Continuar</a>
                    </div>
                </body></html>",
            ],
            self::TRIGGER_SPRINT_UNLOCK => [
                'fr' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:linear-gradient(135deg,#f59e0b,#d97706);padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>🎉 Sprint validé !</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Tu as débloqué <strong>{$nextSprintName}</strong>.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p>Score actuel : <strong>{$currentScore}/100</strong>. Prochaine phase d'infusion : <strong>{$infusionTimer}</strong>.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#f59e0b;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;'>Ouvrir le Dashboard</a>
                    </div>
                </body></html>",
                'en' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:linear-gradient(135deg,#f59e0b,#d97706);padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>🎉 Sprint validated!</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>You unlocked <strong>{$nextSprintName}</strong>.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p>Current score: <strong>{$currentScore}/100</strong>. Next infusion phase: <strong>{$infusionTimer}</strong>.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#f59e0b;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;'>Open Dashboard</a>
                    </div>
                </body></html>",
                'es' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:linear-gradient(135deg,#f59e0b,#d97706);padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>🎉 ¡Sprint validado!</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Desbloqueaste <strong>{$nextSprintName}</strong>.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p>Puntuación actual: <strong>{$currentScore}/100</strong>. Siguiente fase de infusión: <strong>{$infusionTimer}</strong>.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#f59e0b;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;'>Abrir Dashboard</a>
                    </div>
                </body></html>",
            ],
            self::TRIGGER_L21_FIRST_TIME => [
                'fr' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:linear-gradient(135deg,#059669,#047857);padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>🏆 Parcours 90 jours terminé !</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Tu empire local est sécurisé. Score final : <strong>{$currentScore}/100</strong>.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p>Tu as validé les 21 niveaux. Mode Domination activé.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#059669;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;'>Accéder au Dashboard</a>
                    </div>
                </body></html>",
                'en' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:linear-gradient(135deg,#059669,#047857);padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>🏆 90-day journey complete!</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Your local empire is secured. Final score: <strong>{$currentScore}/100</strong>.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p>You validated all 21 levels. Domination Mode active.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#059669;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;'>Access Dashboard</a>
                    </div>
                </body></html>",
                'es' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                    <div style='background:linear-gradient(135deg,#059669,#047857);padding:25px;border-radius:10px 10px 0 0;'>
                        <h1 style='color:white;margin:0;'>🏆 ¡Recorrido de 90 días completado!</h1>
                        <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Tu imperio local está asegurado. Puntuación final: <strong>{$currentScore}/100</strong>.</p>
                    </div>
                    <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                        <p>Validaste los 21 niveles. Modo Dominación activo.</p>
                        <a href='{$dashboardUrl}' style='display:inline-block;background:#059669;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;'>Acceder al Dashboard</a>
                    </div>
                </body></html>",
            ],
        ];

        return $templates[$trigger][$lang] ?? $templates[$trigger]['fr'] ?? '';
    }

    /**
     * Template L21 avec CTA Premium puissant (utilisateur pas en Mode Gardien)
     */
    private function get_l21_premium_cta_template($lang) {
        $templates = [
            'fr' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                <div style='background:linear-gradient(135deg,#059669,#047857);padding:25px;border-radius:10px 10px 0 0;'>
                    <h1 style='color:white;margin:0;'>🏆 Parcours 90 jours terminé !</h1>
                    <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Tu empire local est sécurisé. Score final : <strong>{$currentScore}/100</strong>.</p>
                </div>
                <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                    <p>Bravo pour avoir mené ces 21 niveaux manuellement. C'est une vraie performance.</p>
                    <div style='background:linear-gradient(135deg,#fef3c7,#fde68a);padding:20px;border-radius:10px;margin:20px 0;border:2px solid #d97706;'>
                        <h3 style='margin:0 0 12px 0;color:#92400e;'>🚀 Ne laisse pas tes efforts manuels se perdre</h3>
                        <p style='margin:0;color:#78350f;'>70% des fiches optimisées subissent des attaques (concurrents, bots). Sans surveillance 24/7, ton travail peut être effacé en quelques jours.</p>
                        <p style='margin:12px 0 0 0;color:#78350f;'><strong>Le Mode Gardien</strong> protège ta fiche en continu : avis, posts, photos — tout est géré automatiquement. Tu passes de « Artisan » à « Pilote ».</p>
                        <a href='{$premiumUrl}' style='display:inline-block;background:#d97706;color:white;padding:14px 28px;text-decoration:none;border-radius:8px;margin-top:15px;font-weight:bold;'>Activer le Mode Gardien (29€/mois)</a>
                    </div>
                    <a href='{$dashboardUrl}' style='display:inline-block;color:#059669;padding:10px 0;'>Ou continuer en mode manuel</a>
                </div>
            </body></html>",
            'en' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                <div style='background:linear-gradient(135deg,#059669,#047857);padding:25px;border-radius:10px 10px 0 0;'>
                    <h1 style='color:white;margin:0;'>🏆 90-day journey complete!</h1>
                    <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Your local empire is secured. Final score: <strong>{$currentScore}/100</strong>.</p>
                </div>
                <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                    <p>Congratulations on completing all 21 levels manually. That's a real achievement.</p>
                    <div style='background:linear-gradient(135deg,#fef3c7,#fde68a);padding:20px;border-radius:10px;margin:20px 0;border:2px solid #d97706;'>
                        <h3 style='margin:0 0 12px 0;color:#92400e;'>🚀 Don't let your manual efforts go to waste</h3>
                        <p style='margin:0;color:#78350f;'>70% of optimized listings face attacks (competitors, bots). Without 24/7 monitoring, your work can be erased in days.</p>
                        <p style='margin:12px 0 0 0;color:#78350f;'><strong>Guardian Mode</strong> protects your listing continuously: reviews, posts, photos — all managed automatically. You go from 'Craftsman' to 'Pilot'.</p>
                        <a href='{$premiumUrl}' style='display:inline-block;background:#d97706;color:white;padding:14px 28px;text-decoration:none;border-radius:8px;margin-top:15px;font-weight:bold;'>Activate Guardian Mode (€29/month)</a>
                    </div>
                    <a href='{$dashboardUrl}' style='display:inline-block;color:#059669;padding:10px 0;'>Or continue in manual mode</a>
                </div>
            </body></html>",
            'es' => "<html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;'>
                <div style='background:linear-gradient(135deg,#059669,#047857);padding:25px;border-radius:10px 10px 0 0;'>
                    <h1 style='color:white;margin:0;'>🏆 ¡Recorrido de 90 días completado!</h1>
                    <p style='color:rgba(255,255,255,0.95);margin:10px 0 0 0;'>Tu imperio local está asegurado. Puntuación final: <strong>{$currentScore}/100</strong>.</p>
                </div>
                <div style='background:#fff;padding:25px;border:1px solid #e5e7eb;'>
                    <p>Felicitaciones por completar los 21 niveles manualmente. Es un logro real.</p>
                    <div style='background:linear-gradient(135deg,#fef3c7,#fde68a);padding:20px;border-radius:10px;margin:20px 0;border:2px solid #d97706;'>
                        <h3 style='margin:0 0 12px 0;color:#92400e;'>🚀 No dejes que tus esfuerzos manuales se pierdan</h3>
                        <p style='margin:0;color:#78350f;'>70% de las fichas optimizadas sufren ataques (competidores, bots). Sin vigilancia 24/7, tu trabajo puede borrarse en días.</p>
                        <p style='margin:12px 0 0 0;color:#78350f;'><strong>El Modo Guardián</strong> protege tu ficha continuamente: reseñas, posts, fotos — todo gestionado automáticamente.</p>
                        <a href='{$premiumUrl}' style='display:inline-block;background:#d97706;color:white;padding:14px 28px;text-decoration:none;border-radius:8px;margin-top:15px;font-weight:bold;'>Activar Modo Guardián (29€/mes)</a>
                    </div>
                    <a href='{$dashboardUrl}' style='display:inline-block;color:#059669;padding:10px 0;'>O continuar en modo manual</a>
                </div>
            </body></html>",
        ];
        return $templates[$lang] ?? $templates['fr'];
    }
}
