<?php
require_once __DIR__ . '/includes/auth.php';

requireSuperAdmin();

$agencyCount = (int)$pdo->query("
    SELECT COUNT(*) FROM agencies
")->fetchColumn();

$websiteCount = (int)$pdo->query("
    SELECT COUNT(*)
    FROM agency_white_label_settings
    WHERE website_status = 'active'
")->fetchColumn();

$pendingCount = (int)$pdo->query("
    SELECT COUNT(*)
    FROM agencies
    WHERE status = 'pending'
")->fetchColumn();

$revenue = (float)$pdo->query("
    SELECT COALESCE(SUM(amount), 0)
    FROM agency_white_label_subscription_payments
    WHERE payment_status = 'paid'
")->fetchColumn();

$csrf = stagingCsrfToken();
?>

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>TooGoodTravels Central Dashboard</title>

<style>
* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: #f1f5f9;
    color: #1e293b;
}

header {
    background: #fff;
    padding: 20px 30px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #e2e8f0;
}

.brand {
    color: #008000;
    font-size: 22px;
    font-weight: bold;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 15px;
    font-size: 14px;
}

.staging {
    background: #dbeafe;
    color: #1d4ed8;
    padding: 6px 10px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: bold;
}

.logout {
    color: #dc2626;
    text-decoration: none;
}

main {
    max-width: 1250px;
    margin: auto;
    padding: 30px;
}

.welcome {
    margin-bottom: 28px;
}

.welcome h1 {
    margin-bottom: 8px;
}

.welcome p {
    color: #64748b;
}

.cards {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 18px;
}

.card {
    background: #fff;
    border-radius: 12px;
    padding: 23px;
    box-shadow: 0 3px 12px rgba(0,0,0,.03);
}

.card-label {
    color: #64748b;
    font-size: 14px;
}

.card-value {
    font-size: 30px;
    font-weight: bold;
    margin-top: 12px;
}

.section-title {
    margin: 35px 0 18px;
}

.modules {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
}

.module {
    background: #fff;
    padding: 23px;
    border-radius: 12px;
    border: 1px solid #e2e8f0;
}

.module h3 {
    margin-top: 0;
}

.module p {
    color: #64748b;
    font-size: 14px;
    line-height: 1.6;
}

.status {
    display: inline-block;
    background: #fef3c7;
    color: #92400e;
    font-size: 11px;
    padding: 5px 9px;
    border-radius: 20px;
}

footer {
    text-align: center;
    color: #94a3b8;
    padding: 30px;
    font-size: 12px;
}

@media(max-width: 850px) {
    .cards {
        grid-template-columns: repeat(2, 1fr);
    }

    .modules {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media(max-width: 550px) {
    main {
        padding: 18px;
    }

    header {
        padding: 16px;
    }

    .brand {
        font-size: 17px;
    }

    .cards,
    .modules {
        grid-template-columns: 1fr;
    }

    .header-right {
        gap: 7px;
        font-size: 12px;
    }
}
</style>

</head>

<body>

<header>

    <div class="brand">
        TooGoodTravels Central
    </div>

    <div class="header-right">

        <span class="staging">STAGING</span>

        <span>
            <?= htmlspecialchars(
                $_SESSION['staging_admin_name'],
                ENT_QUOTES,
                'UTF-8'
            ) ?>
        </span>

        <form method="POST" action="logout.php" style="margin:0;">

            <input
                type="hidden"
                name="csrf_token"
                value="<?= htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8') ?>"
            >

            <button
                type="submit"
                class="logout"
                style="background:none;border:0;padding:0;cursor:pointer;"
            >
                Logout
            </button>

        </form>

    </div>

</header>

<main>

    <div class="welcome">

        <h1>Super Admin Dashboard</h1>

        <p>
            Manage your centralised white-label flight booking platform.
        </p>

    </div>

    <div class="cards">

        <div class="card">
            <div class="card-label">Total Agencies</div>
            <div class="card-value">
                <?= number_format($agencyCount) ?>
            </div>
        </div>

        <div class="card">
            <div class="card-label">Active Websites</div>
            <div class="card-value">
                <?= number_format($websiteCount) ?>
            </div>
        </div>

        <div class="card">
            <div class="card-label">Pending Agencies</div>
            <div class="card-value">
                <?= number_format($pendingCount) ?>
            </div>
        </div>

        <div class="card">
            <div class="card-label">Subscription Revenue</div>
            <div class="card-value">
                ₦<?= number_format($revenue, 2) ?>
            </div>
        </div>

    </div>

    <h2 class="section-title">Platform Management</h2>

    <div class="modules">

        <div class="module">
            <h3>🏢 Agency Management</h3>
            <p>
                Register agencies, manage profiles, approve applications
                and manage agency accounts.
            </p>
            <span class="status">Next development stage</span>
        </div>

        <div class="module">
            <h3>🌐 Website Builder</h3>
            <p>
                Customise agency logos, colours, company information,
                domains and bank details.
            </p>
            <span class="status">Next development stage</span>
        </div>

        <div class="module">
            <h3>✈️ Flight Pricing</h3>
            <p>
                Configure domestic, international and business-class
                markups for each agency.
            </p>
            <span class="status">Next development stage</span>
        </div>

        <div class="module">
            <h3>💳 Subscription Management</h3>
            <p>
                Manage setup fees, monthly subscriptions, renewals
                and overdue accounts.
            </p>
            <span class="status">Next development stage</span>
        </div>

        <div class="module">
            <h3>🏦 Payment Verification</h3>
            <p>
                Review agency setup and subscription bank transfers.
            </p>
            <span class="status">Next development stage</span>
        </div>

        <div class="module">
            <h3>⚙️ System Settings</h3>
            <p>
                Manage central platform configuration and administration.
            </p>
            <span class="status">Next development stage</span>
        </div>

    </div>

</main>

<footer>
    TooGoodTravels Ltd — Central White-Label Platform
    <br>
    Development Environment
</footer>

</body>
</html>