<?php
/**
 * Agentic Square - Client Dynamic Financial & Inventory Metric Cards
 * Path: client/client-card.php
 */

if (!isset($pdo) || !isset($_SESSION['user_id'])) {
    die('Architecture Context Violation: Direct sub-module access block.');
}

$client_user_id = (int)$_SESSION['user_id'];

try {
    // 1. Calculate Total Items Ordered (Count all orders placed by this user framework ID)
    $stmtCount = $pdo->prepare("SELECT COUNT(*) AS total_ordered FROM orders WHERE user_id = ?");
    $stmtCount->execute([$client_user_id]);
    $rowOrdered = $stmtCount->fetch();
    $totalOrdered = (int)($rowOrdered['total_ordered'] ?? 0);

    // 2. Calculate Total Capital Spent (Sum price rows where status matches 'Paid')
    $stmtSpent = $pdo->prepare("SELECT SUM(price) AS total_spent FROM orders WHERE user_id = ? AND payment_status = 'Paid'");
    $stmtSpent->execute([$client_user_id]);
    $rowSpent = $stmtSpent->fetch();
    $totalSpent = (float)($rowSpent['total_spent'] ?? 0.00);

} catch (\PDOException $e) {
    echo '<div class="bg-rose-500/10 border border-rose-500/20 text-rose-400 p-4 rounded-xl text-xs font-mono">Metrics Compilation Failure: ' . htmlspecialchars($e->getMessage()) . '</div>';
    $totalOrdered = 0;
    $totalSpent   = 0.00;
}
?>

<div class="grid grid-cols-1 md:grid-cols-2 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-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 Items Ordered</p>
                <h3 class="text-3xl font-black text-white tracking-tight font-mono"><?php echo $totalOrdered; ?></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-cart-shopping"></i>
            </div>
        </div>
        <div class="mt-4 pt-4 border-t border-slate-900/60 flex items-center text-[10px] text-nftTextGray">
            <i class="fa-solid fa-receipt text-nftAccentCyan mr-1.5"></i> <span>Catalog acquisitions ledger history</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-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 Spent</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($totalSpent), 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-credit-card"></i>
            </div>
        </div>
        <div class="mt-4 pt-4 border-t border-slate-900/60 flex items-center text-[10px] text-emerald-400 font-medium">
            <i class="fa-solid fa-shield-check mr-1.5"></i> <span>All transactions fully settled and audit-verified</span>
        </div>
    </div>

</div>
