<?php
/**
 * Agentic Square - Author Dynamic Financial & Asset Metric Cards Component
 * Path: seller/seller-card.php
 * Core Architecture: Secure Data Hydration Framework (Net Income From Orders)
 */

// Safety check to ensure parent wrapper context or configurations exist
if (!isset($pdo) || !isset($_SESSION['user_id'])) {
    die('Direct access restriction layer violation or active database session context missing.');
}

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

try {
    // 1. Fetch Current Balance directly from the sellers table
    $sellerStmt = $pdo->prepare("SELECT current_balance FROM sellers WHERE user_id = ? LIMIT 1");
    $sellerStmt->execute([$seller_user_id]);
    $sellerRow = $sellerStmt->fetch();
    $currentBalance = (float)($sellerRow['current_balance'] ?? 0.00);

    // 2. Calculate Total Items listed by this specific seller
    $itemsStmt = $pdo->prepare("SELECT COUNT(*) AS total_items FROM items WHERE user_id = ?");
    $itemsStmt->execute([$seller_user_id]);
    $itemsRow = $itemsStmt->fetch();
    $totalItems = (int)($itemsRow['total_items'] ?? 0);

    /**
     * 3. DIRECT REVENUE RECONCILIATION COUPLING
     * Pulls the count of successful sales and aggregates the exact saved snapshots 
     * from the net_seller_income column for transactions set to 'Paid'.
     */
    $ordersStmt = $pdo->prepare("
        SELECT 
            COUNT(*) AS total_sold,
            SUM(net_seller_income) AS total_net_income
        FROM orders 
        WHERE seller_id = ? AND payment_status = 'Paid'
    ");
    $ordersStmt->execute([$seller_user_id]);
    $metricsRow = $ordersStmt->fetch();

    $totalSold = (int)($metricsRow['total_sold'] ?? 0);
    $totalNetIncome = (float)($metricsRow['total_net_income'] ?? 0.00);

    // Secondary progress indicator metrics configuration benchmarks
    $salesGoalTarget = 500;
    $salesPercentage = $totalSold > 0 ? round(($totalSold / $salesGoalTarget) * 100, 1) : 0.0;

} 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 Layer Hydration Exception: ' . htmlspecialchars($e->getMessage()) . '</div>';
    
    // Fallback constants parameters
    $currentBalance = 0.00;
    $totalItems     = 0;
    $totalSold      = 0;
    $totalNetIncome = 0.00;
    $salesPercentage = 0.0;
}
?>

<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 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">// Current Balance</p>
                <h3 class="text-2xl font-black text-white tracking-tight font-mono">
                    <?php echo htmlspecialchars(format_currency($currentBalance), ENT_QUOTES, 'UTF-8'); ?>
                </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-wallet"></i>
            </div>
        </div>
        <div class="mt-4 pt-4 border-t border-slate-900/60 flex items-center text-[10px] text-emerald-400 font-bold tracking-wide uppercase">
            <i class="fa-solid fa-money-bill-transfer mr-1.5 text-xs animate-pulse"></i> <span>Ready for checkout request</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 Items</p>
                <h3 class="text-2xl font-black text-white tracking-tight font-mono">
                    <?php echo number_format($totalItems); ?>
                </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-boxes-stacked"></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"></i> <span>All assets verified & live</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-purple-500/5 rounded-full blur-xl pointer-events-none transition-all group-hover:bg-purple-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 Sold</p>
                <h3 class="text-2xl font-black text-white tracking-tight font-mono">
                    <?php echo number_format($totalSold); ?>
                </h3>
            </div>
            <div class="w-11 h-11 rounded-xl bg-purple-500/10 border border-purple-500/20 flex items-center justify-center text-purple-400 text-lg shadow-inner">
                <i class="fa-solid fa-bag-shopping"></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($salesPercentage, 100); ?>%"></div>
            </div>
            <span class="font-mono font-bold text-white text-[10px] ml-2"><?php echo $salesPercentage; ?>%</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 Income</p>
                <h3 class="text-2xl 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($totalNetIncome), 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-chart-line"></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>Net Earnings (Commissions Adjusted)</span>
            <i class="fa-solid fa-arrow-trend-up text-emerald-400 text-xs"></i>
        </div>
    </div>

</div>
