<?php
session_start();
require_once 'config.php';

if (!isset($_SESSION['user_id'])) { exit; }

$targetId = (int)($_GET['id'] ?? 0);
if (!$targetId) { echo "<p class='text-danger p-3'>ID invalide</p>"; exit; }

$stmt = $pdo->prepare("SELECT u.*, us.total_bets, us.won_bets, us.lost_bets, us.reliability_score
    FROM users u LEFT JOIN user_stats us ON us.user_id = u.id WHERE u.id = ?");
$stmt->execute([$targetId]);
$u = $stmt->fetch();
if (!$u) { echo "<p class='text-danger p-3'>Utilisateur introuvable</p>"; exit; }

// Cercles
$cStmt = $pdo->prepare("SELECT c.name FROM circles c JOIN circle_members cm ON cm.circle_id = c.id WHERE cm.user_id = ?");
$cStmt->execute([$targetId]);
$cercles = $cStmt->fetchAll();

// Trophées (depuis trophy_levels lié à user_stats)
$total = (int)($u['total_bets'] ?? 0);
$won   = (int)($u['won_bets']   ?? 0);
$lost  = (int)($u['lost_bets']  ?? 0);
$ratio = ($total > 0) ? round($won / $total * 100) : 0;

$tier = $u['tier'] ?? 'free';
$tierLabel = strtoupper($tier);
$tierColor = match($tier) { 'premium' => 'text-warning', 'premium_plus' => 'text-info', 'admin' => 'text-danger', default => 'text-secondary' };

$lastLogin = !empty($u['last_activity']) ? date('d M Y H:i', strtotime($u['last_activity'])) : 'Inconnu';
$since     = !empty($u['created_at'])    ? date('d M Y',     strtotime($u['created_at']))     : 'Inconnu';
$currentUserId = (int)$_SESSION['user_id'];

// Est-ce déjà un ami ?
$isFriend = false;
$fChk = $pdo->prepare("SELECT id FROM friends WHERE user_id = ? AND friend_id = ? AND status = 'active'");
$fChk->execute([$currentUserId, $targetId]);
if ($fChk->fetch()) $isFriend = true;
?>

<div class="modal-header border-secondary py-2">
    <h6 class="modal-title text-primary fw-bold">
         <?= htmlspecialchars($u['username']) ?>
        <?php if (!empty($u['nickname'])): ?>
            <small class="text-muted" style="font-size:0.7rem;">(<?= htmlspecialchars($u['nickname']) ?>)</small>
        <?php endif; ?>
    </h6>
    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body p-3">

    <div class="d-flex justify-content-between align-items-center mb-3">
        <div>
            <span class="badge <?= $tierColor ?> border border-secondary px-2" style="font-size:0.65rem;"><?= $tierLabel ?></span>
            <?php if ((int)($u['admin'] ?? 0)): ?><span class="badge bg-danger px-2 ms-1" style="font-size:0.65rem;">ADMIN</span><?php endif; ?>
        </div>
        <small class="text-muted" style="font-size:0.65rem;">Membre depuis <?= $since ?></small>
    </div>

    <!-- Stats -->
    <div class="row g-2 mb-3">
        <div class="col-4 text-center">
            <div class="card bg-dark border-secondary p-2">
                <div class="text-success fw-bold"><?= $won ?></div>
                <small class="text-muted" style="font-size:0.65rem;">Gagnés</small>
            </div>
        </div>
        <div class="col-4 text-center">
            <div class="card bg-dark border-secondary p-2">
                <div class="text-danger fw-bold"><?= $lost ?></div>
                <small class="text-muted" style="font-size:0.65rem;">Perdus</small>
            </div>
        </div>
        <div class="col-4 text-center">
            <div class="card bg-dark border-secondary p-2">
                <div class="text-info fw-bold"><?= $ratio ?>%</div>
                <small class="text-muted" style="font-size:0.65rem;">W/L</small>
            </div>
        </div>
    </div>

    <!-- Cercles -->
    <?php if (!empty($cercles)): ?>
    <div class="mb-3">
        <small class="text-muted text-uppercase fw-bold" style="font-size:0.65rem;">Cercles</small><br>
        <?php foreach ($cercles as $c): ?>
            <span class="badge bg-dark border border-info text-info px-2 me-1" style="font-size:0.6rem;"><?= htmlspecialchars($c['name']) ?></span>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>

    <div class="mb-2">
        <small class="text-muted" style="font-size:0.65rem;">Dernier login : <?= $lastLogin ?></small><br>
        <small class="text-muted" style="font-size:0.65rem;">Points interaction : <?= (int)($u['interaction_points'] ?? 0) ?></small>
    </div>

    <!-- Actions -->
    <div class="d-flex gap-2 mt-3 flex-wrap">
        <?php if ($targetId !== $currentUserId): ?>
            <?php if (!$isFriend): ?>
                <button class="btn btn-outline-info btn-sm" onclick="addFriend(<?= $targetId ?>)"><i class="fa-solid fa-user-plus fa-xs"></i> Ajouter</button>
            <?php endif; ?>
            <button class="btn btn-outline-secondary btn-sm" onclick="sendPoke(<?= $targetId ?>)"> Poke</button>
        <?php endif; ?>
    </div>
</div>
