<?php
/**
 * Bracket partial view for knockout tournaments.
 * Included from show.php.
 * Variables: $bracket (array of rounds), $tournament
 */
$totalRounds = count($bracket);
$roundLabels = [];
if ($totalRounds >= 1) $roundLabels[1] = 'الدور الأول';
if ($totalRounds >= 2) $roundLabels[2] = 'الدور الثاني';
if ($totalRounds >= 3) $roundLabels[$totalRounds - 1] = 'نصف النهائي';
if ($totalRounds >= 2) $roundLabels[$totalRounds] = 'النهائي';
if ($totalRounds === 1) $roundLabels[1] = 'النهائي';
?>

<div class="card" style="padding:20px;overflow-x:auto;">
    <h3 style="margin:0 0 15px;font-size:16px;color:#1A1A2E;">
        <i data-lucide="git-branch" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i>
        شجرة المباريات
    </h3>

    <div class="bracket-container" style="display:flex;gap:30px;align-items:center;min-width:fit-content;padding:20px 0;">
        <?php foreach ($bracket as $round => $roundMatches):
            $roundLabel = $roundLabels[$round] ?? 'الجولة ' . $round;
            $matchHeight = (int) pow(2, $round - 1) * 80;
        ?>
        <div class="bracket-round" style="display:flex;flex-direction:column;gap:<?= $matchHeight ?>px;align-items:center;">
            <div style="font-size:12px;font-weight:600;color:#6B7280;margin-bottom:10px;white-space:nowrap;"><?= e($roundLabel) ?></div>
            <?php foreach ($roundMatches as $match):
                $isCompleted = ($match['status'] ?? '') === 'completed';
                $winnerId = (int) ($match['winner_participant_id'] ?? 0);
                $aId = (int) ($match['participant_a_id'] ?? 0);
                $bId = (int) ($match['participant_b_id'] ?? 0);
            ?>
            <div class="bracket-match" style="border:1px solid #E5E7EB;border-radius:8px;overflow:hidden;width:200px;background:#fff;box-shadow:0 1px 3px rgba(0,0,0,0.05);">
                <!-- Participant A -->
                <div style="padding:8px 12px;display:flex;justify-content:space-between;align-items:center;border-bottom:1px solid #F3F4F6;<?= $isCompleted && $winnerId === $aId ? 'background:#ECFDF5;font-weight:700;' : '' ?>">
                    <span style="font-size:13px;color:<?= $aId ? '#1A1A2E' : '#9CA3AF' ?>;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:140px;">
                        <?php if (!empty($match['seed_a'])): ?><small style="color:#9CA3AF;">[<?= (int) $match['seed_a'] ?>]</small> <?php endif; ?>
                        <?= e($match['participant_a_name'] ?? 'يُحدد لاحقاً') ?>
                    </span>
                    <?php if ($isCompleted): ?>
                    <span style="font-size:12px;font-weight:600;color:<?= $winnerId === $aId ? '#059669' : '#6B7280' ?>;"><?= e($match['score_a'] ?? '') ?></span>
                    <?php endif; ?>
                </div>
                <!-- Participant B -->
                <div style="padding:8px 12px;display:flex;justify-content:space-between;align-items:center;<?= $isCompleted && $winnerId === $bId ? 'background:#ECFDF5;font-weight:700;' : '' ?>">
                    <span style="font-size:13px;color:<?= $bId ? '#1A1A2E' : '#9CA3AF' ?>;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:140px;">
                        <?php if (!empty($match['seed_b'])): ?><small style="color:#9CA3AF;">[<?= (int) $match['seed_b'] ?>]</small> <?php endif; ?>
                        <?= e($match['participant_b_name'] ?? 'يُحدد لاحقاً') ?>
                    </span>
                    <?php if ($isCompleted): ?>
                    <span style="font-size:12px;font-weight:600;color:<?= $winnerId === $bId ? '#059669' : '#6B7280' ?>;"><?= e($match['score_b'] ?? '') ?></span>
                    <?php endif; ?>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endforeach; ?>

        <!-- Winner display -->
        <?php
        $lastRound = end($bracket);
        $finalMatch = $lastRound ? end($lastRound) : null;
        if ($finalMatch && !empty($finalMatch['winner_name'])):
        ?>
        <div style="display:flex;flex-direction:column;align-items:center;">
            <div style="font-size:12px;font-weight:600;color:#6B7280;margin-bottom:10px;">الفائز</div>
            <div style="padding:15px 20px;background:linear-gradient(135deg, #7C3AED15, #0D737715);border:2px solid #7C3AED;border-radius:12px;text-align:center;">
                <i data-lucide="trophy" style="width:24px;height:24px;color:#7C3AED;display:block;margin:0 auto 6px;"></i>
                <div style="font-size:14px;font-weight:700;color:#1A1A2E;"><?= e($finalMatch['winner_name']) ?></div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>
