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

if (!isset($_SESSION['user_id'])) { http_response_code(403); exit; }
$userId = (int)$_SESSION['user_id'];
$betId  = (int)($_GET['id'] ?? 0);
if (!$betId) { echo '<p class="text-muted small">Gage introuvable.</p>'; exit; }

try {
    $stmt = $pdo->prepare("
        SELECT b.*, u.username,
               u2.username AS parieur2_name,
               c.name AS circle_name
        FROM bets b
        JOIN users u ON u.id = b.user_id
        LEFT JOIN users u2 ON u2.id = b.parieur2_id
        LEFT JOIN circles c ON c.id = b.circle_id
        WHERE b.id = ?
    ");
    $stmt->execute([$betId]);
    $bet = $stmt->fetch();
} catch (Exception $e) { $bet = null; }

if (!$bet) { echo '<p class="text-muted small text-center py-3">Gage #'.$betId.' introuvable.</p>'; exit; }

$status     = strtoupper($bet['status'] ?? 'PENDING');
$isOwner    = (int)$bet['user_id'] === $userId;
$isGageur2  = (int)($bet['parieur2_id'] ?? 0) === $userId;
$voteTotal  = (int)$bet['vote_oui'] + (int)$bet['vote_non'];
$pctOui     = $voteTotal > 0 ? round(100 * $bet['vote_oui'] / $voteTotal) : 50;
$pctNon     = 100 - $pctOui;
$echeance   = $bet['echeance'] ?? '';
$echeanceStr = ($echeance && $echeance !== '0000-00-00 00:00:00')
    ? date('j M Y', strtotime($echeance))
    : 'Sans limite';

$statusColor = match($status) {
    'PENDING'   => '#ffc107',
    'ACCEPTED'  => '#0dcaf0',
    'COMPLETED','WON' => '#28a745',
    'LOST'      => '#dc3545',
    default     => '#888',
};

// Photos preuves
$photos = [];
try {
    $pStmt = $pdo->prepare("SELECT file_path FROM bet_media WHERE bet_id=? ORDER BY id ASC LIMIT 4");
    $pStmt->execute([$betId]);
    $photos = $pStmt->fetchAll(PDO::FETCH_COLUMN);
} catch (Exception $e) {}
?>

<div style="background:#080810;border:1px solid #1a1a2a;border-radius:8px;padding:12px 14px;margin-bottom:12px;">

    <!-- En-tête gage -->
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px;">
        <span style="font-size:0.7rem;color:#0dcaf0;font-weight:700;">#<?= $betId ?></span>
        <span style="font-size:0.62rem;font-weight:700;color:<?= $statusColor ?>;background:rgba(0,0,0,0.3);border:1px solid <?= $statusColor ?>;border-radius:10px;padding:1px 8px;"><?= $status ?></span>
    </div>

    <!-- Description -->
    <div style="font-size:0.85rem;color:#fff;font-weight:600;line-height:1.4;margin-bottom:10px;">
        <?= htmlspecialchars($bet['description'] ?? '') ?>
    </div>

    <!-- Infos principales -->
    <div style="display:flex;flex-wrap:wrap;gap:6px;margin-bottom:10px;">
        <span style="font-size:0.65rem;background:#0a0a14;border:1px solid #1a1a2a;border-radius:4px;padding:2px 8px;color:#777;font-weight:700;">
            ⚡ <?= (int)$bet['amount'] ?> pts
        </span>
        <?php if ((int)($bet['wallet_stake']??0) > 0): ?>
        <span style="font-size:0.65rem;background:#0a0a14;border:1px solid #1a1a2a;border-radius:4px;padding:2px 8px;color:#ffc107;font-weight:700;">
            🎰 <?= (int)$bet['wallet_stake'] ?> jetons
        </span>
        <?php endif; ?>
        <?php if ($bet['circle_name']): ?>
        <span style="font-size:0.65rem;background:#0a0a14;border:1px solid #1a1a2a;border-radius:4px;padding:2px 8px;color:#fd7e14;">
            🔵 <?= htmlspecialchars($bet['circle_name']) ?>
        </span>
        <?php endif; ?>
        <span style="font-size:0.65rem;background:#0a0a14;border:1px solid #1a1a2a;border-radius:4px;padding:2px 8px;color:#888;">
            📅 <?= $echeanceStr ?>
        </span>
    </div>

    <!-- Gageurs -->
    <div style="display:flex;gap:8px;margin-bottom:10px;">
        <div style="flex:1;background:#0d0d1a;border:1px solid #1a1a3a;border-radius:6px;padding:6px 10px;">
            <div style="font-size:0.55rem;color:#999;letter-spacing:1px;margin-bottom:2px;">LANCEUR</div>
            <div style="font-size:0.75rem;color:#ddd;font-weight:600;"><?= htmlspecialchars($bet['username'] ?? '—') ?></div>
        </div>
        <div style="flex:1;background:#0d0d1a;border:1px solid #1a1a3a;border-radius:6px;padding:6px 10px;">
            <div style="font-size:0.55rem;color:#999;letter-spacing:1px;margin-bottom:2px;">GAGEUR 2</div>
            <div style="font-size:0.75rem;color:<?= $bet['parieur2_name'] ? '#ddd' : '#444' ?>;font-weight:600;">
                <?= $bet['parieur2_name'] ? htmlspecialchars($bet['parieur2_name']) : 'En attente...' ?>
            </div>
        </div>
    </div>

    <!-- Votes -->
    <?php if ($voteTotal > 0): ?>
    <div style="margin-bottom:10px;">
        <div style="font-size:0.58rem;color:#999;margin-bottom:4px;letter-spacing:1px;">VOTES (<?= $voteTotal ?>)</div>
        <div style="display:flex;gap:0;border-radius:4px;overflow:hidden;height:18px;">
            <?php if ($pctOui > 0): ?>
            <div style="width:<?= $pctOui ?>%;background:#28a745;display:flex;align-items:center;justify-content:center;font-size:0.58rem;font-weight:700;color:#fff;">
                <?= $pctOui ?>% OUI
            </div>
            <?php endif; ?>
            <?php if ($pctNon > 0): ?>
            <div style="width:<?= $pctNon ?>%;background:#dc3545;display:flex;align-items:center;justify-content:center;font-size:0.58rem;font-weight:700;color:#fff;">
                <?= $pctNon ?>% NON
            </div>
            <?php endif; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Photos preuves -->
    <?php if (!empty($photos)): ?>
    <div style="display:flex;gap:4px;flex-wrap:wrap;">
        <?php foreach ($photos as $p): ?>
        <img src="<?= htmlspecialchars($p) ?>" style="width:48px;height:48px;object-fit:cover;border-radius:4px;border:1px solid #222;cursor:pointer;"
             onclick="window.open('<?= htmlspecialchars($p) ?>','_blank')">
        <?php endforeach; ?>
    </div>
    <?php endif; ?>

    <!-- Honorabilité -->
    <?php if (!empty($bet['honored'])): ?>
    <div style="margin-top:8px;font-size:0.65rem;color:#777;"><i class="fa-solid fa-circle-check fa-xs me-1"></i>Gage honoré</div>
    <?php elseif (!empty($bet['not_honored'])): ?>
    <div style="margin-top:8px;font-size:0.65rem;color:#dc3545;"><i class="fa-solid fa-circle-xmark fa-xs me-1"></i>Non honoré signalé</div>
    <?php endif; ?>
</div>
