<?php
// public-report.php — Reporte público compartible (sin login requerido)
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/db.php';

$token = trim($_GET['token'] ?? '');
if (!$token || !preg_match('/^[a-f0-9]{64}$/', $token)) {
    http_response_code(404);
    die('Enlace no válido.');
}

// Verificar token y obtener reporte
try {
    $stmt = $pdo->prepare("
        SELECT r.html, r.period_label, r.title, r.created_at, c.name AS client_name
        FROM dm_report_tokens t
        JOIN dm_reports r ON r.id = t.report_id
        JOIN dm_clients c ON c.id = t.client_id
        WHERE t.token = ? AND t.expires_at > NOW()
        LIMIT 1
    ");
    $stmt->execute([$token]);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (Exception $e) {
    http_response_code(500);
    die('Error al cargar el reporte.');
}

if (!$row) {
    http_response_code(410);
?><!DOCTYPE html>
<html lang="es">
<head><meta charset="UTF-8"><title>Enlace expirado</title>
<style>body{font-family:sans-serif;display:flex;align-items:center;justify-content:center;min-height:100vh;margin:0;background:#f4f5f7;color:#333}
.box{text-align:center;padding:40px;background:#fff;border-radius:12px;box-shadow:0 4px 20px rgba(0,0,0,.08);max-width:400px}
.icon{font-size:48px;margin-bottom:16px}.h{font-size:20px;font-weight:700;margin-bottom:8px}.s{font-size:14px;color:#888}</style>
</head><body>
<div class="box"><div class="icon">⏰</div><div class="h">Enlace expirado</div>
<div class="s">Este enlace de reporte ya no está disponible. Solicita un nuevo enlace a DIMARK.</div></div>
</body></html>
<?php
    exit;
}

$period   = htmlspecialchars($row['period_label']);
$title    = htmlspecialchars($row['title'] ?: 'Reporte de rendimiento');
$client   = htmlspecialchars($row['client_name']);
$created  = date('d/m/Y', strtotime($row['created_at']));
$html     = $row['html'];
?><!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $title ?> — <?= $client ?> | DIMARK</title>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:'Helvetica Neue',Arial,sans-serif;background:#f4f5f7;color:#1a1a1a}
.pub-header{background:#0f0f0f;color:#fff;padding:14px 24px;display:flex;align-items:center;justify-content:space-between;gap:12px;flex-wrap:wrap}
.pub-logo{display:flex;align-items:center;gap:10px}
.pub-logo-mark{width:32px;height:32px;background:#E8272A;border-radius:8px;display:flex;align-items:center;justify-content:center;font-weight:700;font-size:15px;flex-shrink:0}
.pub-brand{font-size:14px;font-weight:700;letter-spacing:.3px}
.pub-sub{font-size:10px;letter-spacing:1.5px;text-transform:uppercase;color:#E8272A;margin-top:1px}
.pub-meta{text-align:right;font-size:12px;color:rgba(255,255,255,.6)}
.pub-meta strong{color:#fff;display:block;font-size:13px;margin-bottom:2px}
.pub-body{max-width:900px;margin:24px auto;padding:0 16px 40px}
.pub-notice{background:#fff;border:1px solid #e5e7eb;border-radius:10px;padding:12px 16px;font-size:12px;color:#6b7280;margin-bottom:20px;display:flex;align-items:center;gap:8px}
.pub-notice strong{color:#374151}
.pub-report{background:#fff;border-radius:12px;overflow:hidden;box-shadow:0 1px 4px rgba(0,0,0,.08)}
@media(max-width:600px){.pub-header{padding:12px 16px}.pub-meta{display:none}.pub-body{padding:0 10px 32px;margin-top:16px}}
</style>
</head>
<body>
<div class="pub-header">
  <div class="pub-logo">
    <div class="pub-logo-mark">D</div>
    <div><div class="pub-brand">DIMARK</div><div class="pub-sub">Super Cerebro</div></div>
  </div>
  <div class="pub-meta">
    <strong><?= $client ?></strong>
    <?= $period ?> · <?= $created ?>
  </div>
</div>
<div class="pub-body">
  <div class="pub-notice">
    🔒 <span><strong>Reporte confidencial</strong> — Generado exclusivamente para <?= $client ?>. No distribuir sin autorización de DIMARK.</span>
  </div>
  <div class="pub-report">
    <?= $html ?>
  </div>
</div>
</body>
</html>
