<?php
// user_profile.php
require 'config/db.php';
require_once 'addons/verified_sellers/main.php'; 
require_once 'addons/seller_level/main.php';
require_once 'addons/last_seen/main.php'; 



// 1. Capture Seller ID
$user_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($user_id === 0) {
    header("Location: index.php");
    exit;
}

// 2. Fetch User Data
// We include a check for the 'bio' or 'motto' if you decide to add that column later
$stmt = $pdo->prepare("SELECT id, username, created_at FROM users WHERE id = ? LIMIT 1");
$stmt->execute([$user_id]);
$user = $stmt->fetch();

if (!$user) {
    include 'includes/header.php';
    echo "<div style='max-width:800px; margin:100px auto; text-align:center; color:var(--text-gray);'>
            <i class='fa-solid fa-user-slash' style='font-size:50px; margin-bottom:20px;'></i>
            <h2>User not found in the VVault database.</h2>
            <a href='index.php' style='color:var(--primary);'>Return to Market</a>
          </div>";
    include 'includes/footer.php';
    exit;
}

// 3. Update 'Last Seen' for the profile owner (Optional: helps track activity)
if (function_exists('update_last_seen')) {
    update_last_seen($user['id'], $pdo);
}

include 'includes/header.php';
?>

<style>
    .profile-card {
        max-width: 800px;
        margin: 60px auto;
        background: var(--nav-bg);
        border: 1px solid var(--border);
        border-radius: 24px;
        overflow: hidden;
        box-shadow: 0 25px 50px rgba(0,0,0,0.4);
    }
    .profile-header {
        background: linear-gradient(135deg, rgba(0, 255, 136, 0.05) 0%, rgba(0,0,0,0.6) 100%);
        padding: 60px 40px 40px;
        text-align: center;
        border-bottom: 1px solid var(--border);
        position: relative;
    }
    .avatar-circle {
        width: 110px;
        height: 110px;
        background: var(--primary);
        color: #000;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 48px;
        font-weight: 900;
        margin: 0 auto 25px;
        border: 5px solid #0d1117;
        box-shadow: 0 0 20px rgba(0, 255, 136, 0.2);
        font-family: 'Syncopate', sans-serif;
    }
    .username-title {
        font-family: 'Syncopate', sans-serif;
        font-size: 24px;
        color: white;
        margin: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 12px;
    }
    .stats-bar {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 1px;
        background: var(--border);
        margin-top: 30px;
    }
    .stat-item {
        background: var(--nav-bg);
        padding: 20px;
        text-align: center;
    }
    .stat-value { color: white; font-weight: 900; font-size: 18px; display: block; }
    .stat-label { color: var(--text-gray); font-size: 11px; text-transform: uppercase; letter-spacing: 1px; }
    
    @media (max-width: 600px) {
        .profile-card { margin: 20px; }
        .stats-bar { grid-template-columns: 1fr; }
    }
</style>

<div class="profile-card">
    <div class="profile-header">
        <div class="avatar-circle">
            <?php echo strtoupper(substr($user['username'], 0, 1)); ?>
        </div>
        
        <h1 class="username-title">
            @<?php echo htmlspecialchars($user['username']); ?>
            <?php if (function_exists('get_user_badges')) echo get_user_badges($user['id'], $pdo); ?>
        </h1>
        
        <div style="margin-top: 12px;">
            <?php if (function_exists('get_user_status')) echo get_user_status($user['id'], $pdo); ?>
        </div>

        <p style="color: var(--text-gray); font-size: 12px; margin-top: 20px; font-weight: bold; text-transform: uppercase; letter-spacing: 1px;">
            <i class="fa-regular fa-calendar-check" style="color: var(--primary);"></i> 
            Member Since <?php echo date('F Y', strtotime($user['created_at'])); ?>
        </p>

        <div style="max-width: 400px; margin: 30px auto 0; background: rgba(0,0,0,0.3); padding: 15px; border-radius: 12px; border: 1px solid rgba(255,255,255,0.03);">
            <?php if (function_exists('get_seller_xp_bar')) echo get_seller_xp_bar($user['id'], $pdo); ?>
        </div>
    </div>

    <div class="stats-bar">
        <div class="stat-item">
            <span class="stat-value">
                <?php 
                    // Example: Fetch total successful sales for this user
                    $stmt_sales = $pdo->prepare("SELECT COUNT(*) FROM orders WHERE seller_id = ? AND status = 'completed'");
                    $stmt_sales->execute([$user['id']]);
                    echo $stmt_sales->fetchColumn();
                ?>
            </span>
            <span class="stat-label">Successful Trades</span>
        </div>
        <div class="stat-item">
            <span class="stat-value">
                <?php 
                    // Example: Fetch positive reviews
                    $stmt_rev = $pdo->prepare("SELECT COUNT(*) FROM reviews WHERE seller_id = ? AND rating >= 4");
                    $stmt_rev->execute([$user['id']]);
                    echo $stmt_rev->fetchColumn();
                ?>
            </span>
            <span class="stat-label">Positive Feedback</span>
        </div>
    </div>

    <div style="padding: 40px; display: flex; flex-wrap: wrap; justify-content: center; gap: 15px; background: rgba(0,0,0,0.1);">
        <a href="chat_view.php?receiver_id=<?php echo $user['id']; ?>" class="v-btn-publish" style="text-decoration: none; padding: 15px 40px; display: flex; align-items: center; gap: 10px;">
            <i class="fa-solid fa-comment-dots"></i> INITIATE CONTACT
        </a>
        
        <a href="index.php?seller=<?php echo $user['id']; ?>" style="border: 1px solid var(--border); color: white; padding: 15px 40px; border-radius: 12px; text-decoration: none; font-size: 13px; font-weight: 800; text-transform: uppercase; letter-spacing: 1px; transition: 0.3s;" onmouseover="this.style.borderColor='var(--primary)'" onmouseout="this.style.borderColor='var(--border)'">
            Browse Inventory
        </a>
    </div>
</div>

<?php include 'includes/footer.php'; ?>