<?php
/**
 * Agentic Square - Premium Admin Infrastructure Analytics Panel
 * Path: admin/dashboard-card.php
 * Core Architecture: Secure Multi-Table Statistical Hydration
 */

// Strict Authorization Guard: Block component if execution happens out-of-context or user lacks admin credentials
if (!isset($pdo) || !isset($_SESSION['user_id']) || !isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
    die('Infrastructure Architecture Violation: Restricted administrative resource context block.');
}

try {
    // 1. Calculate Total Active Items (Filter status strictly for 'Active')
    $itemsStmt = $pdo->query("SELECT COUNT(*) AS total_active_items FROM items WHERE status = 'Active'");
    $itemsRow  = $itemsStmt->fetch();
    $totalActiveItems = (int)($itemsRow['total_active_items'] ?? 0);

    // 2. Calculate Total Sellers registered across the ecosystem
    $sellersStmt = $pdo->query("SELECT COUNT(*) AS total_sellers FROM sellers");
    $sellersRow  = $sellersStmt->fetch();
    $totalSellers = (int)($sellersRow['total_sellers'] ?? 0);

    // 3. Calculate Total Ecosystem Gross Revenue (Accumulate order item pricing where status is 'Paid')
    $revenueStmt = $pdo->query("SELECT SUM(price) AS gross_revenue FROM orders WHERE payment_status = 'Paid'");
    $revenueRow  = $revenueStmt->fetch();
    $totalRevenue = (float)($revenueRow['gross_revenue'] ?? 0.00);

    // Dynamic metrics markers for visual aesthetics
    $itemsGoalTarget = 5000;
    $itemsPercentage = $totalActiveItems > 0 ? round(($totalActiveItems / $itemsGoalTarget) * 100, 1) : 0.0;

} catch (\PDOException $e) {
    // Fail safely inside standard diagnostic dashboards blocks
    echo '<div class="bg-rose-500/10 border border-rose-500/20 text-rose-400 p-4 rounded-xl text-xs font-mono mb-6">Ecosystem Analytics Hydration Exception: ' . htmlspecialchars($e->getMessage()) . '</div>';
    
    $totalActiveItems = 0;
    $totalSellers     = 0;
    $totalRevenue     = 0.00;
    $itemsPercentage  = 0.0;
}
?>

<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
    
    <div class="glass-card p-6 rounded-2xl border border-slate-800/80 relative overflow-hidden group bg-gradient-to-b from-slate-950/40 to-slate-950/80">
        <div class="absolute -top-4 -right-4 w-24 h-24 bg-nftAccentCyan/5 rounded-full blur-xl pointer-events-none transition-all group-hover:bg-nftAccentCyan/10"></div>
        <div class="flex items-center justify-between">
            <div class="space-y-1.5">
                <p class="text-[10px] font-bold text-nftTextGray uppercase tracking-widest">// Total Active Items</p>
                <h3 class="text-3xl font-black text-white tracking-tight font-mono">
                    <?php echo number_format($totalActiveItems); ?>
                </h3>
            </div>
            <div class="w-11 h-11 rounded-xl bg-nftAccentCyan/10 border border-nftAccentCyan/20 flex items-center justify-center text-nftAccentCyan text-lg shadow-inner">
                <i class="fa-solid fa-cubes"></i>
            </div>
        </div>
        <div class="mt-4 pt-4 border-t border-slate-900/60 flex items-center justify-between text-[10px] text-nftTextGray">
            <div class="w-4/5 bg-slate-950 h-1.5 rounded-full overflow-hidden relative border border-slate-900">
                <div class="bg-gradient-to-r from-nftAccentCyan to-nftAccentPurple h-full rounded-full" style="width: <?php echo min($itemsPercentage, 100); ?>%"></div>
            </div>
            <span class="font-mono font-bold text-white text-[10px] ml-2"><?php echo $itemsPercentage; ?>%</span>
        </div>
    </div>

    <div class="glass-card p-6 rounded-2xl border border-slate-800/80 relative overflow-hidden group bg-gradient-to-b from-slate-950/40 to-slate-950/80">
        <div class="absolute -top-4 -right-4 w-24 h-24 bg-nftAccentPurple/5 rounded-full blur-xl pointer-events-none transition-all group-hover:bg-nftAccentPurple/10"></div>
        <div class="flex items-center justify-between">
            <div class="space-y-1.5">
                <p class="text-[10px] font-bold text-nftTextGray uppercase tracking-widest">// Total Sellers</p>
                <h3 class="text-3xl font-black text-white tracking-tight font-mono">
                    <?php echo number_format($totalSellers); ?>
                </h3>
            </div>
            <div class="w-11 h-11 rounded-xl bg-nftAccentPurple/10 border border-nftAccentPurple/20 flex items-center justify-center text-nftAccentPurple text-lg shadow-inner">
                <i class="fa-solid fa-users-gear"></i>
            </div>
        </div>
        <div class="mt-4 pt-4 border-t border-slate-900/60 flex items-center text-[10px] text-nftTextGray font-medium">
            <i class="fa-solid fa-circle-check text-emerald-400 mr-1.5 text-xs"></i> <span>Global author accounts verified</span>
        </div>
    </div>

    <div class="glass-card p-6 rounded-2xl border border-slate-800/80 sm:col-span-2 lg:col-span-1 relative overflow-hidden group bg-gradient-to-b from-slate-950/40 to-slate-950/80">
        <div class="absolute -top-4 -right-4 w-24 h-24 bg-emerald-500/5 rounded-full blur-xl pointer-events-none transition-all group-hover:bg-emerald-500/10"></div>
        <div class="flex items-center justify-between">
            <div class="space-y-1.5">
                <p class="text-[10px] font-bold text-nftTextGray uppercase tracking-widest">// Total Revenue</p>
                <h3 class="text-3xl font-black bg-clip-text text-transparent bg-gradient-to-r from-white via-white to-nftAccentCyan tracking-tight font-mono">
                    <?php echo htmlspecialchars(format_currency($totalRevenue), ENT_QUOTES, 'UTF-8'); ?>
                </h3>
            </div>
            <div class="w-11 h-11 rounded-xl bg-emerald-500/10 border border-emerald-500/20 flex items-center justify-center text-emerald-400 text-lg shadow-inner">
                <i class="fa-solid fa-sack-dollar"></i>
            </div>
        </div>
        <div class="mt-4 pt-4 border-t border-slate-900/60 flex items-center justify-between text-[10px] text-nftTextGray font-medium tracking-wide">
            <span>Ecosystem Transaction Value</span>
            <i class="fa-solid fa-arrow-trend-up text-emerald-400 text-xs"></i>
        </div>
    </div>

</div>
