<?php declare(strict_types=1); require_once __DIR__ . '/system/init.php';

// === Email Verification Handler ===
$_verify_msg = '';
$_verify_token = '';

// Support clean URL: /verify/TOKEN
$_uri = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH);
if (preg_match('#^/verify/([a-f0-9]{64})$#', $_uri, $_m)) {
    $_verify_token = $_m[1];
}
// Legacy fallback: ?v=verify&token=TOKEN
if (!$_verify_token && isset($_GET['v']) && $_GET['v'] === 'verify' && !empty($_GET['token'])) {
    $_verify_token = trim($_GET['token']);
}

if ($_verify_token && preg_match('/^[a-f0-9]{64}$/', $_verify_token)) {
    $user = Database::one(
        "SELECT id, status, status_code FROM users WHERE verify_token = :token",
        [':token' => $_verify_token]
    );
    if ($user) {
        if ((int)$user['status_code'] === 1004) {
            Database::execute(
                "UPDATE users SET status = 'active', status_code = 1000, email_verified = 1, is_verified = 1, verify_token = NULL WHERE id = :id",
                [':id' => $user['id']]
            );
            $_verify_msg = 'verified';
        } else {
            $_verify_msg = 'already';
        }
    } else {
        $_verify_msg = 'invalid';
    }
}

ob_start(); include __DIR__ . '/pages/home.php'; $initialContent = ob_get_clean(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="<?= htmlspecialchars($csrf_token) ?>">
    <meta name="description" content="<?= htmlspecialchars($store_name) ?> - Premium products delivered to your door.">
    <title><?= htmlspecialchars($store_name) ?></title>
    <link rel="stylesheet" href="/assets/css/core.css">
</head>
<body>

    <?php if ($_verify_msg): ?>
    <div id="verify-banner" style="position:fixed;top:0;left:0;right:0;z-index:9999;padding:16px 20px;text-align:center;font-family:Arial,sans-serif;font-size:15px;font-weight:600;cursor:pointer;" onclick="this.remove()">
        <?php if ($_verify_msg === 'verified'): ?>
            <div style="background:#065f46;color:#ecfdf5;border-bottom:2px solid #10b981;">
                ✓ Your email has been verified! You can now log in to your account.
            </div>
        <?php elseif ($_verify_msg === 'already'): ?>
            <div style="background:#1e40af;color:#eff6ff;border-bottom:2px solid #3b82f6;">
                Your email is already verified. You can log in.
            </div>
        <?php else: ?>
            <div style="background:#991b1b;color:#fef2f2;border-bottom:2px solid #ef4444;">
                Invalid or expired verification link. Please request a new one.
            </div>
        <?php endif; ?>
    </div>
    <script>setTimeout(function(){var b=document.getElementById('verify-banner');if(b)b.style.transition='opacity 0.5s';b.style.opacity='0';setTimeout(function(){b.remove()},600);},8000);</script>
    <?php endif; ?>
    <div id="header">
      <div class="header-inner">
        <span class="store-validation">
            <a href="#home" data-route="home" class="logo-link">
                <span class="logo-title"><?= $L('brand_name', $store_name) ?></span>
                <span class="logo-tagline"><?= $L('brand_tagline', $store_tagline) ?></span>
            </a>
        </span>

        <span class="site-navigation">
            <span id="nav-home" data-route="home" class="active"><?= $L('nav_home', 'Home') ?></span>
            <span id="nav-reviews" data-route="reviews"><?= $L('nav_reviews', 'Reviews') ?></span>
            <span id="nav-faq" data-route="faq"><?= $L('nav_faq', 'FAQ') ?></span>
        </span>

        <span class="user-interactions">
            <span id="track-ui" data-route="track" title="Track Order">
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="7" width="13" height="13" rx="2"/><path d="M16 3h5v5"/><path d="M21 3l-9 9"/></svg>
            </span>
            <span id="track-accounts" data-route="account" title="Account">
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="8" r="4"/><path d="M4 20c0-4 4-7 8-7s8 3 8 7"/></svg>
            </span>
            <span id="track-cart" data-action="open-cart" title="Cart">
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 2L3 6v14a2 2 0 002 2h14a2 2 0 002-2V6l-3-4z"/><line x1="3" y1="6" x2="21" y2="6"/><path d="M16 10a4 4 0 01-8 0"/></svg>
                <span class="cart-badge" id="cart-badge" style="display:none"></span>
            </span>
        </span>
      </div>
    </div>

    <?php include __DIR__ . '/modules/cart-drawer.php'; ?>

    <div id="content">
        <?= $initialContent ?>
    </div>

    <div id="footer">
        <span class="privacy" data-route="privacy"><?= $L('nav_privacy', 'Privacy Policy') ?></span>
        <span class="assisted" data-route="exclude"><?= $L('nav_exclude', 'Self-Exclusion') ?></span>
        <span class="terms" data-route="terms"><?= $L('nav_terms', 'Terms of Service') ?></span>
    </div>

    <script src="/assets/js/bridge.js"></script>
    <script src="/assets/js/router.js"></script>
    <script src="/assets/js/cart.js"></script>
    <script src="/assets/js/telemetry.js"></script>
    <script src="/assets/js/anti-scrape.js"></script>
    <script src="/assets/js/screenshot-protection.js"></script>
    <script src="/assets/js/watchdog.js"></script>
    <script src="/assets/js/navigation.js"></script>
    <script src="/assets/js/product-cards.js" defer></script>
</body>
</html>