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

// Query avançada: Puxa o ranking e conta as vitórias e derrotas no histórico
$sql = "
    SELECT 
        r.team_name, 
        r.rating, 
        u.username,
        (SELECT COUNT(*) FROM BATTLEDATA WHERE status = 2 AND ((team1 = r.team_name AND score1 > score2) OR (team2 = r.team_name AND score2 > score1))) AS wins,
        (SELECT COUNT(*) FROM BATTLEDATA WHERE status = 2 AND ((team1 = r.team_name AND score1 < score2) OR (team2 = r.team_name AND score2 < score1))) AS losses
    FROM robots r 
    JOIN users u ON r.user_id = u.id 
    ORDER BY r.rating DESC
";
$ranking = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="<?php echo $_SESSION['lang']; ?>" data-bs-theme="dark">
<head>
    <meta charset="UTF-8">
    <title><?php echo $t['title_hof']; ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background-color: #121212; } 
        .rank-1 { background-color: rgba(255, 215, 0, 0.1) !important; color: #ffd700; font-weight: 900; font-size: 1.2rem;} 
        .rank-2 { background-color: rgba(192, 192, 192, 0.1) !important; color: #c0c0c0; font-weight: bold; font-size: 1.1rem;} 
        .rank-3 { background-color: rgba(205, 127, 50, 0.1) !important; color: #cd7f32; font-weight: bold; font-size: 1.1rem;} 
        .lang-btn { text-decoration: none; margin-left: 10px; font-size: 1.2rem; opacity: 0.5; transition: 0.3s;} 
        .lang-btn:hover { opacity: 0.8; }
        .lang-btn.active { opacity: 1; border-bottom: 2px solid #ff4757;}
        .win-badge { background-color: rgba(40, 167, 69, 0.2); color: #28a745; padding: 4px 8px; border-radius: 4px; font-weight: bold; }
        .loss-badge { background-color: rgba(220, 53, 69, 0.2); color: #dc3545; padding: 4px 8px; border-radius: 4px; font-weight: bold; }
    </style>
</head>
<body>

    <nav class="navbar navbar-expand-lg navbar-dark bg-dark border-bottom border-secondary mb-4">
        <div class="container">
            <a class="navbar-brand fw-bold" href="index.php"><?php echo $t['nav_arena']; ?></a>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item"><a class="nav-link active text-warning" href="ranking.php"><?php echo $t['nav_ranking']; ?></a></li>
                    <li class="nav-item"><a class="nav-link" href="historico.php"><?php echo $t['nav_history']; ?></a></li>
                    <?php if (isset($_SESSION['user_id'])): ?>
                        <li class="nav-item"><a class="nav-link text-danger" href="index.php?logout=1"><?php echo $t['nav_logout']; ?></a></li>
                    <?php endif; ?>
                    <li class="nav-item d-flex align-items-center ms-3 border-start border-secondary ps-3">
                        <a href="?lang=pt" class="lang-btn <?php echo ($_SESSION['lang'] == 'pt') ? 'active' : ''; ?>">🇧🇷</a>
                        <a href="?lang=en" class="lang-btn <?php echo ($_SESSION['lang'] == 'en') ? 'active' : ''; ?>">🇺🇸</a>
                        <a href="?lang=de" class="lang-btn <?php echo ($_SESSION['lang'] == 'de') ? 'active' : ''; ?>">🇩🇪</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container text-center">
        <h2 class="mb-4 text-uppercase fw-bold text-warning"><?php echo $t['title_hof']; ?></h2>
        
        <div class="row justify-content-center">
            <div class="col-md-10">
                <div class="table-responsive shadow-lg">
                    <table class="table table-dark table-striped align-middle border-secondary text-center">
                        <thead class="table-secondary text-uppercase">
                            <tr>
                                <th><?php echo $t['col_pos']; ?></th>
                                <th class="text-start"><?php echo $t['title_arsenal']; ?></th>
                                <th><?php echo $t['col_commander']; ?></th>
                                <th><?php echo $t['col_wins']; ?></th>
                                <th><?php echo $t['col_losses']; ?></th>
                                <th>Elo</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            if ($ranking->num_rows > 0) {
                                $pos = 1;
                                while($row = $ranking->fetch_assoc()) {
                                    $class = ""; $medal = "#$pos";
                                    if ($pos == 1) { $class = "rank-1"; $medal = "🥇 1º"; }
                                    elseif ($pos == 2) { $class = "rank-2"; $medal = "🥈 2º"; }
                                    elseif ($pos == 3) { $class = "rank-3"; $medal = "🥉 3º"; }
                                    
                                    echo "<tr class='$class'>";
                                    echo "<td>$medal</td>";
                                    echo "<td class='text-start fw-bold'>" . htmlspecialchars($row['team_name']) . "</td>";
                                    echo "<td class='text-muted'>" . htmlspecialchars($row['username']) . "</td>";
                                    
                                    // Colunas de Vitórias e Derrotas com badges coloridos
                                    echo "<td><span class='win-badge'>" . $row['wins'] . "</span></td>";
                                    echo "<td><span class='loss-badge'>" . $row['losses'] . "</span></td>";
                                    
                                    echo "<td>⭐ " . $row['rating'] . "</td>";
                                    echo "</tr>";
                                    $pos++;
                                }
                            } else {
                                echo "<tr><td colspan='6' class='text-muted py-4'>" . $t['no_ranking'] . "</td></tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<?php $conn->close(); ?>
