<?php 
$root = dirname(__DIR__);
require $root . '/core/DB.php';
require $root . '/core/config.php';
require $root . '/core/Security.php';

Security::init();

$id = (int)($_GET['id'] ?? 0);

if (!$id) {
    header('Location: index.php');
    exit;
}

$stmt = $pdo->prepare("SELECT d.*, p.name as product_name, p.price FROM downloads d JOIN products p ON d.product_id = p.id WHERE d.id = ?");
$stmt->execute([$id]);
$download = $stmt->fetch();

if (!$download) {
    header('Location: index.php');
    exit;
}

$siteName = 'Cuboid Store';
$siteURL = $config['app']['url'];
$productName = htmlspecialchars($download['product_name']);
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Compra Exitosa - <?=$productName?> - Cuboid Store</title>
    
    <meta name="description" content="Tu compra de <?=$productName?> fue procesada exitosamente. Descarga tu archivo aquí.">
    <meta name="robots" content="noindex, follow">
    <link rel="canonical" href="<?=$siteURL?>/success.php?id=<?=$id?>">
    
    <meta property="og:type" content="website">
    <meta property="og:site_name" content="<?=htmlspecialchars($siteName)?>">
    <meta property="og:title" content="Compra Exitosa - <?=$productName?>">
    <meta property="og:description" content="Tu compra fue procesada exitosamente. Descarga tu archivo ahora.">
    <meta property="og:url" content="<?=$siteURL?>/success.php?id=<?=$id?>">
    
    <meta name="twitter:card" content="summary">
    <meta name="twitter:title" content="Compra Exitosa - <?=$productName?>">
    <meta name="twitter:description" content="Tu compra fue procesada exitosamente. Descarga tu archivo ahora.">
    
    <link href="styles.css" rel="stylesheet">
    <link rel="icon" type="image/x-icon" href="/favicon.ico">
    <title>Compra Exitosa - Cuboid Store</title>
    <link href="styles.css" rel="stylesheet">
    <script>
        if (localStorage.getItem('darkMode') === 'true' || (!localStorage.getItem('darkMode') && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
            document.documentElement.classList.add('dark');
        }
    </script>
</head>
<body>
    <nav class="navbar">
        <div class="navbar-content">
            <a href="index.php" class="logo">Cuboid</a>
            <div class="nav-links">
                <a href="index.php" class="nav-link">Tienda</a>
                <button class="dark-toggle" onclick="toggleDarkMode()" title="Cambiar modo">
                    <svg class="sun-icon" width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"/>
                    </svg>
                    <svg class="moon-icon" width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24" style="display:none">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"/>
                    </svg>
                </button>
            </div>
        </div>
    </nav>

    <div class="container">
        <div class="success-card">
            <div class="success-icon">
                <svg fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                </svg>
            </div>
            
            <h1>¡Compra Exitosa!</h1>
            <p class="success-message">Tu pago ha sido procesado correctamente.</p>

            <div class="success-details">
                <div class="detail-row">
                    <span class="detail-label">Producto</span>
                    <span class="detail-value"><?=htmlspecialchars($download['product_name'])?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Monto</span>
                    <span class="detail-value">$<?=number_format($download['price'], 0, ',', '.')?> CLP</span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Correo</span>
                    <span class="detail-value"><?=htmlspecialchars($download['email'])?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Descargas restantes</span>
                    <span class="detail-value"><?=$download['downloads_limit'] - $download['downloads_used']?> de <?=$download['downloads_limit']?></span>
                </div>
            </div>

            <div class="success-actions">
                <a href="download.php?token=<?=$download['token']?>" class="btn btn-primary">
                    <svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24" style="margin-right: 0.5rem;">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
                    </svg>
                    Descargar Ahora
                </a>
                <a href="index.php" class="btn btn-secondary">Seguir Comprando</a>
            </div>

            <p class="success-note">
                Este enlace expire el <?=date('d/m/Y', strtotime($download['expires_at']))?>. 
                Tienes <?=$download['downloads_limit'] - $download['downloads_used']?> descargas restantes.
            </p>
        </div>
    </div>

    <footer class="footer">
        <div class="footer-content">
            <div class="footer-section">
                <h3>Cuboid Store</h3>
                <p>Tu tienda de archivos digitales segura y confiable.</p>
            </div>
            <div class="footer-section">
                <h3>Enlaces</h3>
                <a href="index.php">Inicio</a>
                <a href="admin/index.php">Admin</a>
            </div>
            <div class="footer-section">
                <h3>Soporte</h3>
                <a href="#">Ayuda</a>
                <a href="#">Términos</a>
            </div>
        </div>
        <div class="footer-bottom">
            <p>&copy; 2026 Cuboid Store. Todos los derechos reservados.</p>
        </div>
    </footer>

    <script>
        function toggleDarkMode() {
            document.documentElement.classList.toggle('dark');
            localStorage.setItem('darkMode', document.documentElement.classList.contains('dark'));
            updateIcons(document.documentElement.classList.contains('dark'));
        }
        function updateIcons(isDark) {
            document.querySelector('.sun-icon').style.display = isDark ? 'none' : 'block';
            document.querySelector('.moon-icon').style.display = isDark ? 'block' : 'none';
        }
        updateIcons(document.documentElement.classList.contains('dark'));
    </script>
</body>
</html>