<?php
session_start();
if (!isset($_SESSION['unohs'])) {
    header("Location: index.php");
    exit();
}
include "conn.php";

// ─────────────────────────────────────────────
// মূল Summary সংখ্যা
// ─────────────────────────────────────────────
$totalUsers = $conn->query("SELECT COUNT(*) c FROM shonu_subjects")->fetch_assoc()['c'];
$activeUsers = $conn->query("SELECT COUNT(*) c FROM shonu_subjects WHERE status = 1")->fetch_assoc()['c'];
$totalAgents = $conn->query("SELECT COUNT(*) c FROM tb_agent")->fetch_assoc()['c'];
$activeAgents = $conn->query("SELECT COUNT(*) c FROM tb_agent WHERE status = 1")->fetch_assoc()['c'];

$todayDeposit = $conn->query("
    SELECT COALESCE(SUM(motta),0) t FROM thevani
    WHERE sthiti = 1 AND DATE(dinankavannuracisi) = CURDATE()
")->fetch_assoc()['t'];

$todayWithdraw = $conn->query("
    SELECT COALESCE(SUM(motta),0) t FROM hintegedukolli
    WHERE sthiti = 1 AND DATE(dinankavannuracisi) = CURDATE()
")->fetch_assoc()['t'];

$monthDeposit = $conn->query("
    SELECT COALESCE(SUM(motta),0) t FROM thevani
    WHERE sthiti = 1 AND MONTH(dinankavannuracisi) = MONTH(CURDATE()) AND YEAR(dinankavannuracisi) = YEAR(CURDATE())
")->fetch_assoc()['t'];

$monthWithdraw = $conn->query("
    SELECT COALESCE(SUM(motta),0) t FROM hintegedukolli
    WHERE sthiti = 1 AND MONTH(dinankavannuracisi) = MONTH(CURDATE()) AND YEAR(dinankavannuracisi) = YEAR(CURDATE())
")->fetch_assoc()['t'];

$monthProfit = $monthDeposit - $monthWithdraw;

// ─────────────────────────────────────────────
// Top 5 Agent — সবচেয়ে বেশি User এনেছে যে
// ─────────────────────────────────────────────
$topAgents = [];
$result = $conn->query("
    SELECT a.id, a.mobile, s.owncode,
           (SELECT COUNT(*) FROM shonu_subjects u WHERE u.code = s.owncode) AS user_count
    FROM tb_agent a
    LEFT JOIN shonu_subjects s ON s.id = a.userid
    HAVING user_count > 0
    ORDER BY user_count DESC
    LIMIT 5
");
if ($result) {
    while ($row = $result->fetch_assoc()) { $topAgents[] = $row; }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Business Overview - RAJ365 Admin</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="admin-gold-theme.css">
    <style>
        body { background: #0A0A0A; padding: 20px; }
        .metric-card {
            background: linear-gradient(160deg, #171512, #0F0D09);
            border: 1px solid rgba(212,175,55,.25);
            border-radius: 16px;
            padding: 18px;
            text-align: center;
        }
        .metric-value { font-size: 24px; font-weight: 800; color: #D4AF37; }
        .metric-label { font-size: 11.5px; color: #8A8270; margin-top: 4px; text-transform: uppercase; letter-spacing: .4px; }
        .metric-value.green { color: #1FB979; }
        .metric-value.red { color: #E8544A; }
        .section-title { color: #D4AF37; font-weight: 700; margin: 26px 0 12px; font-size: 16px; }
        .rank-row {
            display: flex; justify-content: space-between; align-items: center;
            background: #171512; border: 1px solid rgba(212,175,55,.15);
            border-radius: 10px; padding: 10px 16px; margin-bottom: 8px;
        }
    </style>
</head>
<body>
<div class="container" style="max-width:760px;">
    <h3 style="color:#D4AF37; margin-bottom:4px;">📊 Business Overview</h3>
    <p style="color:#8A8270; font-size:12.5px; margin-bottom:20px;">সব হিসাব শুধুমাত্র সফল (Completed) transaction থেকে</p>

    <div class="row g-3">
        <div class="col-6 col-md-3">
            <div class="metric-card">
                <div class="metric-value"><?= number_format($totalUsers) ?></div>
                <div class="metric-label">Total Users</div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="metric-card">
                <div class="metric-value green"><?= number_format($activeUsers) ?></div>
                <div class="metric-label">Active Users</div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="metric-card">
                <div class="metric-value"><?= number_format($totalAgents) ?></div>
                <div class="metric-label">Total Agents</div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="metric-card">
                <div class="metric-value green"><?= number_format($activeAgents) ?></div>
                <div class="metric-label">Active Agents</div>
            </div>
        </div>
    </div>

    <div class="section-title">💰 আজকের হিসাব</div>
    <div class="row g-3">
        <div class="col-6">
            <div class="metric-card">
                <div class="metric-value green">৳<?= number_format($todayDeposit, 2) ?></div>
                <div class="metric-label">Today Deposit</div>
            </div>
        </div>
        <div class="col-6">
            <div class="metric-card">
                <div class="metric-value red">৳<?= number_format($todayWithdraw, 2) ?></div>
                <div class="metric-label">Today Withdraw</div>
            </div>
        </div>
    </div>

    <div class="section-title">📅 এই মাসের হিসাব</div>
    <div class="row g-3">
        <div class="col-4">
            <div class="metric-card">
                <div class="metric-value green">৳<?= number_format($monthDeposit, 2) ?></div>
                <div class="metric-label">Deposit</div>
            </div>
        </div>
        <div class="col-4">
            <div class="metric-card">
                <div class="metric-value red">৳<?= number_format($monthWithdraw, 2) ?></div>
                <div class="metric-label">Withdraw</div>
            </div>
        </div>
        <div class="col-4">
            <div class="metric-card">
                <div class="metric-value <?= $monthProfit >= 0 ? 'green' : 'red' ?>">৳<?= number_format($monthProfit, 2) ?></div>
                <div class="metric-label">Net (আনুমানিক)</div>
            </div>
        </div>
    </div>

    <div class="section-title">🏆 Top 5 Agent (User সংখ্যা অনুযায়ী)</div>
    <?php if (empty($topAgents)): ?>
        <p style="color:#8A8270;">কোনো Agent-এর downline User পাওয়া যায়নি।</p>
    <?php else: ?>
        <?php $rank = 1; foreach ($topAgents as $a): ?>
            <div class="rank-row">
                <div>
                    <span style="color:#D4AF37; font-weight:700;">#<?= $rank++ ?></span>
                    &nbsp; <?= htmlspecialchars($a['mobile']) ?>
                    <span style="color:#8A8270; font-size:11.5px;">(Code: <?= htmlspecialchars($a['owncode']) ?>)</span>
                </div>
                <div style="color:#1FB979; font-weight:700;"><?= $a['user_count'] ?> Users</div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>

    <div style="margin-top:20px;">
        <a href="dashboard.php" style="color:#D4AF37;">← Dashboard-এ ফিরে যান</a>
    </div>
</div>
</body>
</html>
