<?php

require_once __DIR__ . '/admin_tool_helper.php';

/*
|--------------------------------------------------------------------------
| Safe Stats Defaults
|--------------------------------------------------------------------------
*/
$stats = [
    'users'           => 0,
    'symbols'         => 0,
    'timeframes'      => 0,
    'styles'          => 0,
    'active_alerts'   => 0,
    'sent_alerts'     => 0,
    'api_providers'   => 0,
    'news_cache'      => 0,
    'api_logs'        => 0,
    'suggested_setups'=> 0,
];

/*
|--------------------------------------------------------------------------
| Load Dashboard Statistics
|--------------------------------------------------------------------------
*/
try {

    if (function_exists('db')) {

        if (admin_table_exists('users')) {
            $stats['users'] = (int) db()
                ->query("SELECT COUNT(*) FROM users")
                ->fetchColumn();
        }

        if (admin_table_exists('symbols')) {
            $stats['symbols'] = (int) db()
                ->query("SELECT COUNT(*) FROM symbols")
                ->fetchColumn();
        }

        if (admin_table_exists('timeframes')) {
            $stats['timeframes'] = (int) db()
                ->query("SELECT COUNT(*) FROM timeframes")
                ->fetchColumn();
        }

        if (admin_table_exists('trading_style_settings')) {
            $stats['styles'] = (int) db()
                ->query("SELECT COUNT(*) FROM trading_style_settings")
                ->fetchColumn();
        }

        if (admin_table_exists('institutional_alerts')) {

            $stats['active_alerts'] = (int) db()
                ->query("
                    SELECT COUNT(*)
                    FROM institutional_alerts
                    WHERE is_active = 1
                ")
                ->fetchColumn();

            $stats['sent_alerts'] = (int) db()
                ->query("
                    SELECT COUNT(*)
                    FROM institutional_alerts
                    WHERE triggered_at IS NOT NULL
                ")
                ->fetchColumn();
        }

        if (admin_table_exists('api_settings')) {
            $stats['api_providers'] = (int) db()
                ->query("SELECT COUNT(*) FROM api_settings")
                ->fetchColumn();
        }

        if (admin_table_exists('news_cache')) {
            $stats['news_cache'] = (int) db()
                ->query("SELECT COUNT(*) FROM news_cache")
                ->fetchColumn();
        }

        if (admin_table_exists('api_request_logs')) {
            $stats['api_logs'] = (int) db()
                ->query("SELECT COUNT(*) FROM api_request_logs")
                ->fetchColumn();
        }

        if (admin_table_exists('institutional_suggested_setups')) {
            $stats['suggested_setups'] = (int) db()
                ->query("SELECT COUNT(*) FROM institutional_suggested_setups WHERE status = 'PENDING'")
                ->fetchColumn();
        }
    }

} catch (Throwable $e) {

    error_log('Control Panel Error: ' . $e->getMessage());
}

/*
|--------------------------------------------------------------------------
| Page Header
|--------------------------------------------------------------------------
*/
admin_layout_header('Admin Control Panel');

$cards = [

    [
        'url' => 'api_monitor.php',
        'title' => 'API Monitor',
        'desc' => 'Monitor API credits, connections and request logs.',
        'color' => 'from-indigo-600 to-blue-600'
    ],


    [
        'url' => 'api_provider_usage_logs.php',
        'title' => 'API Provider Logs',
        'desc' => 'See which API key row successfully supplied each live quote or candle request.',
        'color' => 'from-cyan-700 to-blue-900'
    ],

    [
        'url' => 'api_settings.php',
        'title' => 'API Settings',
        'desc' => 'Control API keys and provider limits.',
        'color' => 'from-blue-600 to-cyan-600'
    ],

    [
        'url' => 'symbols.php',
        'title' => 'Symbols',
        'desc' => 'Manage forex, crypto, metals and indices.',
        'color' => 'from-emerald-600 to-green-600'
    ],

    [
        'url' => 'timeframes.php',
        'title' => 'Timeframes',
        'desc' => 'Control timeframe hierarchy and weighting.',
        'color' => 'from-purple-600 to-fuchsia-600'
    ],

    [
        'url' => 'candles.php',
        'title' => 'Candles Manager',
        'desc' => 'Sync, import, manually add, inspect and repair OHLC candle data.',
        'color' => 'from-amber-600 to-orange-700'
    ],

    [
        'url' => 'trading_styles.php',
        'title' => 'Trading Styles',
        'desc' => 'Configure SL/TP logic and execution models.',
        'color' => 'from-orange-500 to-amber-500'
    ],

    [
        'url' => 'users.php',
        'title' => 'Users',
        'desc' => 'Manage traders, plans and access.',
        'color' => 'from-pink-600 to-rose-600'
    ],

    [
        'url' => 'news_engine.php',
        'title' => 'News Engine',
        'desc' => 'Control blackout windows and speech tracking.',
        'color' => 'from-slate-700 to-slate-900'
    ],

    [
        'url' => 'institutional_engine.php',
        'title' => 'Institutional Engine',
        'desc' => 'Configure BOS, CHoCH, OB and liquidity rules.',
        'color' => 'from-violet-600 to-indigo-700'
    ],

    [
        'url' => 'alert_engine.php',
        'title' => 'Alert Engine',
        'desc' => 'Manage multi-stage institutional alerts.',
        'color' => 'from-red-600 to-rose-700'
    ],

    [
        'url' => 'suggested_setup_rules.php',
        'title' => 'Suggested Setup Rules',
        'desc' => 'Control derived setup notifications, premium/discount gates, freshness rules and anti-spam limits.',
        'color' => 'from-amber-600 to-red-700'
    ],

    [
        'url' => 'alert_settings.php',
        'title' => 'SMTP & Telegram',
        'desc' => 'Configure delivery channels and templates.',
        'color' => 'from-sky-600 to-cyan-700'
    ],

    [
        'url' => 'active_alerts.php',
        'title' => 'Active Alerts',
        'desc' => 'Track active institutional setups.',
        'color' => 'from-teal-600 to-emerald-700'
    ],

    [
        'url' => 'alert_logs.php',
        'title' => 'Alert Logs',
        'desc' => 'Review sent email and Telegram alerts.',
        'color' => 'from-yellow-500 to-orange-600'
    ],

    [
        'url' => 'prop_firm_rules.php',
        'title' => 'Prop Firm Rules',
        'desc' => 'Configure FTMO and FundedNext rules.',
        'color' => 'from-zinc-700 to-black'
    ],

    [
        'url' => 'system_settings.php',
        'title' => 'System Settings',
        'desc' => 'Manage branding and application defaults.',
        'color' => 'from-gray-600 to-slate-700'
    ],
];

?>

<style>

.dashboard-wrapper{
    max-width:1450px;
    margin:0 auto;
}

.hero-card{
    background:linear-gradient(135deg,#020617,#0f172a,#1e293b);
    border-radius:34px;
    padding:38px;
    color:#fff;
    margin-bottom:28px;
    position:relative;
    overflow:hidden;
}

.hero-card:before{
    content:"";
    position:absolute;
    width:420px;
    height:420px;
    background:rgba(59,130,246,.15);
    border-radius:50%;
    right:-120px;
    top:-120px;
}

.hero-title{
    position:relative;
    z-index:2;
    font-size:48px;
    font-weight:900;
    letter-spacing:-0.06em;
    line-height:1;
}

.hero-subtitle{
    position:relative;
    z-index:2;
    margin-top:14px;
    max-width:850px;
    color:#cbd5e1;
    font-size:15px;
    line-height:1.7;
}

.stats-grid{
    display:grid;
    grid-template-columns:repeat(auto-fit,minmax(180px,1fr));
    gap:18px;
    margin-bottom:28px;
}

.stat-card{
    background:#fff;
    border:1px solid #e2e8f0;
    border-radius:28px;
    padding:24px;
    box-shadow:0 10px 28px rgba(15,23,42,.05);
}

.stat-label{
    font-size:11px;
    text-transform:uppercase;
    letter-spacing:.08em;
    color:#64748b;
    font-weight:900;
}

.stat-value{
    margin-top:12px;
    font-size:38px;
    font-weight:900;
    color:#020617;
    letter-spacing:-0.05em;
}

.section-card{
    background:#fff;
    border:1px solid #e2e8f0;
    border-radius:32px;
    padding:30px;
    box-shadow:0 10px 28px rgba(15,23,42,.05);
}

.section-title{
    font-size:30px;
    font-weight:900;
    color:#020617;
    letter-spacing:-0.04em;
}

.section-subtitle{
    margin-top:8px;
    color:#64748b;
    font-size:15px;
}

.cards-grid{
    display:grid;
    grid-template-columns:repeat(auto-fit,minmax(260px,1fr));
    gap:18px;
    margin-top:28px;
}

.control-card{
    position:relative;
    overflow:hidden;
    border-radius:28px;
    padding:24px;
    color:#fff;
    min-height:180px;
    transition:.25s ease;
    box-shadow:0 16px 34px rgba(15,23,42,.14);
}

.control-card:hover{
    transform:translateY(-4px);
}

.control-card:before{
    content:"";
    position:absolute;
    width:180px;
    height:180px;
    background:rgba(255,255,255,.08);
    border-radius:50%;
    right:-60px;
    top:-60px;
}

.control-title{
    position:relative;
    z-index:2;
    font-size:24px;
    font-weight:900;
    line-height:1.1;
}

.control-desc{
    position:relative;
    z-index:2;
    margin-top:12px;
    color:rgba(255,255,255,.9);
    font-size:14px;
    line-height:1.6;
}

.control-link{
    position:relative;
    z-index:2;
    margin-top:20px;
    display:inline-flex;
    align-items:center;
    gap:8px;
    background:rgba(255,255,255,.14);
    padding:10px 16px;
    border-radius:999px;
    font-size:12px;
    font-weight:900;
    text-transform:uppercase;
    letter-spacing:.06em;
}

.sequence-grid{
    display:grid;
    grid-template-columns:repeat(auto-fit,minmax(220px,1fr));
    gap:16px;
    margin-top:24px;
}

.sequence-card{
    border-radius:24px;
    padding:22px;
    border:1px solid #e2e8f0;
    background:#f8fafc;
}

.sequence-step{
    font-size:11px;
    font-weight:900;
    text-transform:uppercase;
    letter-spacing:.08em;
    color:#64748b;
}

.sequence-title{
    margin-top:10px;
    font-size:22px;
    font-weight:900;
    color:#020617;
}

.sequence-desc{
    margin-top:8px;
    color:#64748b;
    font-size:14px;
    line-height:1.6;
}

.execution-card{
    background:linear-gradient(135deg,#ecfdf5,#d1fae5);
    border-color:#a7f3d0;
}

.execution-card .sequence-step{
    color:#047857;
}

.execution-card .sequence-title{
    color:#065f46;
}

</style>

<div class="dashboard-wrapper">

    <section class="hero-card">

        <div class="hero-title">
            Institutional Trading Assistant
        </div>

        <div class="hero-subtitle">
            Control institutional execution logic, API providers, liquidity sweep confirmations,
            BOS/CHoCH validation, smart alerts, multi-stage execution workflows and live market infrastructure.
        </div>

    </section>

    <section class="stats-grid">

        <?php foreach ($stats as $label => $value): ?>

            <div class="stat-card">

                <div class="stat-label">
                    <?= e(str_replace('_', ' ', $label)) ?>
                </div>

                <div class="stat-value">
                    <?= e($value) ?>
                </div>

            </div>

        <?php endforeach; ?>

    </section>

    <section class="section-card">

        <div class="section-title">
            Platform Controls
        </div>

        <div class="section-subtitle">
            Manage every major component powering the institutional trading engine.
        </div>

        <div class="cards-grid">

            <?php foreach ($cards as $card): ?>

                <a
                    href="<?= e($card['url']) ?>"
                    class="control-card bg-gradient-to-br <?= e($card['color']) ?>"
                >

                    <div class="control-title">
                        <?= e($card['title']) ?>
                    </div>

                    <div class="control-desc">
                        <?= e($card['desc']) ?>
                    </div>

                    <div class="control-link">
                        Open Module →
                    </div>

                </a>

            <?php endforeach; ?>

        </div>

    </section>

    <section class="section-card" style="margin-top:28px;">

        <div class="section-title">
            Smart Institutional Alert Sequence
        </div>

        <div class="section-subtitle">
            Alerts are only triggered after institutional confirmation layers align properly.
        </div>

        <div class="sequence-grid">

            <div class="sequence-card">

                <div class="sequence-step">
                    Stage 1
                </div>

                <div class="sequence-title">
                    Zone Approached
                </div>

                <div class="sequence-desc">
                    Price approaches premium or discount institutional zone.
                </div>

            </div>

            <div class="sequence-card">

                <div class="sequence-step">
                    Stage 2
                </div>

                <div class="sequence-title">
                    Liquidity Sweep
                </div>

                <div class="sequence-desc">
                    Internal or external liquidity gets swept near the order block.
                </div>

            </div>

            <div class="sequence-card">

                <div class="sequence-step">
                    Stage 3
                </div>

                <div class="sequence-title">
                    BOS / CHoCH
                </div>

                <div class="sequence-desc">
                    Lower timeframe structure confirms reversal or continuation.
                </div>

            </div>

            <div class="sequence-card execution-card">

                <div class="sequence-step">
                    Stage 4
                </div>

                <div class="sequence-title">
                    Execution Ready
                </div>

                <div class="sequence-desc">
                    Only then should Telegram and email alerts be triggered.
                </div>

            </div>

        </div>

    </section>

</div>

<?php admin_layout_footer(); ?>
