<?php
/**
 * trial_banner(array $sub) — banner de trial countdown + anuncios in-app
 * Incluir al inicio del <body> del dashboard principal
 */
function trial_banner(array $sub): void {
    $status    = $sub['status'] ?? '';
    $trial_end = $sub['trial_ends_at'] ?? null;

    // Banner trial
    if ($status === 'trial' && $trial_end) {
        $days_left = max(0, (int)ceil((strtotime($trial_end) - time()) / 86400));
        $color  = $days_left <= 1 ? '#dc2626' : ($days_left <= 3 ? '#d97706' : '#2563eb');
        $txt    = $days_left === 0
            ? '⚠ Tu trial vence HOY — activa tu plan para no perder acceso.'
            : "⏳ Tu trial vence en <strong>$days_left día".($days_left===1?'':'s')."</strong>. Activa tu plan y mantén todo tu trabajo.";
        $score  = (int)($sub['engagement_score'] ?? 0);
        echo "<div id='trial-banner' style='background:$color;color:#fff;padding:10px 20px;display:flex;align-items:center;justify-content:space-between;font-size:14px'>";
        echo "<span>$txt</span>";
        echo "<div style='display:flex;gap:10px;align-items:center'>";
        if ($score > 0) echo "<span style='background:rgba(255,255,255,.2);padding:2px 10px;border-radius:20px;font-size:12px'>Score: $score/100</span>";
        echo "<button onclick=\"document.getElementById('upgrade-modal').style.display='flex'\" style='background:#fff;color:$color;border:none;padding:6px 16px;border-radius:6px;font-weight:700;cursor:pointer'>Activar plan</button>";
        echo "<span onclick=\"document.getElementById('trial-banner').style.display='none';try{sessionStorage.setItem('tb_hidden','1')}catch(e){}\" title='Cerrar' style='cursor:pointer;font-size:22px;line-height:1;padding:0 2px;opacity:.85'>&times;</span>";
        echo "</div></div>";
        echo "<script>try{if(sessionStorage.getItem('tb_hidden')){var _tb=document.getElementById('trial-banner');if(_tb)_tb.style.display='none';}}catch(e){}</script>";
    }

    // Banner past_due
    if ($status === 'past_due') {
        echo "<div style='background:#7c2d12;color:#fff;padding:10px 20px;font-size:14px;display:flex;align-items:center;justify-content:space-between'>";
        echo "<span>⚠ Hay un problema con tu pago. Tienes 3 días para regularizar antes de perder acceso.</span>";
        echo "<button onclick=\"openBillingPortal()\" style='background:#fff;color:#7c2d12;border:none;padding:6px 16px;border-radius:6px;font-weight:700;cursor:pointer'>Actualizar pago</button>";
        echo "</div>";
    }

    // Anuncios in-app (dm_announcements)
    global $pdo;
    $announcements = $pdo->query("
        SELECT * FROM dm_announcements
        WHERE active=1 AND (ends_at IS NULL OR ends_at > NOW())
        AND (starts_at IS NULL OR starts_at <= NOW())
        ORDER BY created_at DESC LIMIT 3
    ")->fetchAll();

    foreach ($announcements as $ann) {
        $bg = match($ann['type']??'info') {
            'success' => '#14532d',
            'warning' => '#713f12',
            'error'   => '#7f1d1d',
            default   => '#1e3a5f',
        };
        echo "<div style='background:$bg;color:#e2e8f0;padding:8px 20px;font-size:13px;display:flex;justify-content:space-between;align-items:center'>";
        echo "<span>".htmlspecialchars($ann['message'])."</span>";
        echo "<span onclick=\"this.parentElement.remove()\" style='cursor:pointer;color:#aaa;font-size:16px;margin-left:16px'>×</span>";
        echo "</div>";
    }
}
