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



// 1. Session Security
if (!isset($_SESSION['user_id'])) { 
    header("Location: login.php"); 
    exit; 
}

$my_internal_id = $_SESSION['user_id']; // Direct numeric ID

try {
    // 2. Fetch User Information (Email, Username, Join Date)
    $stmt_user = $pdo->prepare("SELECT username, email, created_at FROM users WHERE id = ? LIMIT 1");
    $stmt_user->execute([$my_internal_id]);
    $user_info = $stmt_user->fetch();

    if (!$user_info) { 
        die("System Error: Account profile not found."); 
    }

    $display_name = $user_info['username'];

    // 3. Fetch Reputation Stats
    $stmt_avg = $pdo->prepare("SELECT AVG(rating) as average, COUNT(*) as total FROM reviews WHERE seller_id = ?");
    $stmt_avg->execute([$my_internal_id]);
    $rating_stats = $stmt_avg->fetch();
    
    $average_rating = round($rating_stats['average'] ?? 0, 1);
    $total_reviews = $rating_stats['total'];

} catch (PDOException $e) {
    die("Database Connection Error.");
}

include 'includes/header.php';
?>

<style>
    .profile-grid { max-width: 1100px; margin: 40px auto; display: grid; grid-template-columns: 300px 1fr; gap: 30px; padding: 0 20px; font-family: 'Inter', sans-serif; }
    .sidebar { background: var(--nav-bg); padding: 35px 25px; border-radius: 20px; border: 1px solid var(--border); height: fit-content; text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,0.2); }
    .avatar { width: 90px; height: 90px; background: var(--primary); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 20px; font-size: 36px; color: black; font-weight: 900; font-family: 'Syncopate', sans-serif; box-shadow: 0 0 20px rgba(0, 255, 136, 0.2); }
    .user-name-container { display: flex; align-items: center; justify-content: center; gap: 10px; margin-top: 15px; flex-wrap: wrap; }
    .stat-box { margin: 20px 0; padding: 15px; background: rgba(0,0,0,0.2); border-radius: 12px; border: 1px solid var(--border); }
    .logout-btn { color: #ff4d4d; font-size: 11px; text-decoration: none; font-weight: 900; border: 1px solid rgba(255, 77, 77, 0.3); padding: 12px; border-radius: 8px; display: block; margin-top: 30px; transition: 0.3s; letter-spacing: 1px; }
    .logout-btn:hover { background: rgba(255, 77, 77, 0.1); border-color: #ff4d4d; }
</style>

<div class="profile-grid">
    <aside class="sidebar">
        <div class="avatar"><?php echo strtoupper(substr($display_name, 0, 1)); ?></div>
        
        <div class="user-name-container">
            <h2 style="margin:0; color: white; font-family: 'Syncopate', sans-serif; font-size: 18px;">
                @<?php echo htmlspecialchars($display_name); ?>
            </h2>
            <?php if (function_exists('get_user_badges')) echo get_user_badges($my_internal_id, $pdo); ?>
        </div>

        <div style="margin-top: 8px;">
            <?php if (function_exists('get_user_status')) echo get_user_status($my_internal_id, $pdo); ?>
        </div>

        <div style="margin-top: 25px;">
            <?php if (function_exists('get_seller_xp_bar')) echo get_seller_xp_bar($my_internal_id, $pdo); ?>
        </div>

        <div class="stat-box">
            <div style="color: #ffcc00; font-size: 18px; margin-bottom: 5px;">
                <?php 
                for($i=1; $i<=5; $i++) {
                    echo ($i <= $average_rating) ? "★" : "☆";
                }
                ?>
            </div>
            <span style="display:block; font-size:11px; color:var(--text-gray); font-weight: bold; text-transform: uppercase; letter-spacing: 1px;">
                Merchant Rating (<?php echo $total_reviews; ?> reviews)
            </span>
        </div>
        
        <div style="font-size: 11px; color: var(--text-gray); margin-top: 15px;">
            Member since: <span style="color: white;"><?php echo date('M Y', strtotime($user_info['created_at'])); ?></span>
        </div>

        <a href="logout.php" class="logout-btn">TERMINATE SESSION</a>
    </aside>

    <main>
        <div style="background: var(--nav-bg); padding: 40px; border-radius: 20px; border: 1px solid var(--border);">
            <h3 style="margin-top:0; color: var(--primary); font-family: 'Syncopate', sans-serif; font-size: 20px;">
                MANAGEMENT CONSOLE
            </h3>
            <p style="color: var(--text-gray); line-height: 1.6;">
                Welcome to your personal dashboard. Here you can monitor your active listings, track your merchant growth, and manage your vault security settings.
            </p>
            
            <hr style="border: 0; border-top: 1px solid var(--border); margin: 30px 0;">
            
            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
                <a href="my_products.php" style="padding: 20px; background: rgba(255,255,255,0.02); border: 1px solid var(--border); border-radius: 12px; text-decoration: none; color: white; transition: 0.2s;">
                    <i class="fa-solid fa-box" style="color: var(--primary); margin-bottom: 10px; display: block; font-size: 20px;"></i>
                    <strong>My Listings</strong>
                    <span style="display: block; font-size: 12px; color: var(--text-gray); margin-top: 5px;">Edit or remove your products.</span>
                </a>
                <a href="my_orders.php" style="padding: 20px; background: rgba(255,255,255,0.02); border: 1px solid var(--border); border-radius: 12px; text-decoration: none; color: white; transition: 0.2s;">
                    <i class="fa-solid fa-cart-shopping" style="color: var(--primary); margin-bottom: 10px; display: block; font-size: 20px;"></i>
                    <strong>Purchase History</strong>
                    <span style="display: block; font-size: 12px; color: var(--text-gray); margin-top: 5px;">View items you have bought.</span>
                </a>
            </div>
        </div>
    </main>
</div>

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