<?php

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . '/../config/database.php';

/*
|--------------------------------------------------------------------------
| Authentication
|--------------------------------------------------------------------------
*/

if (
    !isset($_SESSION['admin_id']) ||
    !isset($_SESSION['is_admin'])
) {

    header('Location: login.php');
    exit;
}

/*
|--------------------------------------------------------------------------
| Escape helper
|--------------------------------------------------------------------------
*/

function e($value)
{
    return htmlspecialchars(
        (string)$value,
        ENT_QUOTES,
        'UTF-8'
    );
}

/*
|--------------------------------------------------------------------------
| Table exists helper
|--------------------------------------------------------------------------
*/

function admin_table_exists($table)
{
    try {

        $stmt = db()->prepare("
            SHOW TABLES LIKE ?
        ");

        $stmt->execute([$table]);

        return (bool)$stmt->fetchColumn();

    } catch (Throwable $e) {

        return false;
    }
}

/*
|--------------------------------------------------------------------------
| Layout Header
|--------------------------------------------------------------------------
*/

function admin_layout_header($title = 'Admin Panel')
{
?>
<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <title><?= e($title) ?></title>

    <script src="https://cdn.tailwindcss.com"></script>

</head>

<body class="bg-slate-100 min-h-screen">

<div class="min-h-screen flex">

    <!-- Sidebar -->
    <aside class="w-72 bg-slate-950 text-white p-6">

        <p class="uppercase tracking-[0.3em] text-xs text-blue-200">
            Institutional
        </p>

        <h1 class="text-2xl font-black mt-3">
            Control Panel
        </h1>

        <div class="mt-8 space-y-2">

            <?php

            $links = [

                'control_panel.php' => 'Dashboard',
                'api_settings.php' => 'API Settings',
                'symbols.php' => 'Symbols',
                'timeframes.php' => 'Timeframes',
                'trading_styles.php' => 'Trading Styles',
                'users.php' => 'Users',
                'news_engine.php' => 'News Engine',
                'institutional_engine.php' => 'Institutional Engine',
                'alert_engine.php' => 'Alert Engine',
                'alert_settings.php' => 'SMTP & Telegram',
                'active_alerts.php' => 'Active Alerts',
                'alert_logs.php' => 'Alert Logs',
                'prop_firm_rules.php' => 'Prop Firm Rules',
                'system_settings.php' => 'System Settings',
                'logout.php' => 'Logout'
            ];

            foreach ($links as $file => $label):

            ?>

                <a
                    href="<?= e($file) ?>"
                    class="block rounded-xl px-4 py-3 bg-slate-900 hover:bg-blue-600 transition"
                >
                    <?= e($label) ?>
                </a>

            <?php endforeach; ?>

        </div>

    </aside>

    <!-- Main -->
    <main class="flex-1 p-8">

        <div class="mb-8">

            <h2 class="text-3xl font-black text-slate-950">
                <?= e($title) ?>
            </h2>

            <p class="text-slate-500 mt-2">
                Institutional Trading Assistant Administration
            </p>

        </div>
<?php
}

/*
|--------------------------------------------------------------------------
| Layout Footer
|--------------------------------------------------------------------------
*/

function admin_layout_footer()
{
?>
    </main>

</div>

</body>
</html>
<?php
}