<?php
/**
 * Live Scores Mini Widget - Include in any dashboard
 * Usage: include CRICKET_EXCHANGE_ROOT . '/includes/scores_widget.php';
 */
require_once dirname(__DIR__) . '/config.php';
require_once dirname(__DIR__) . '/includes/api.php';

$widget_live = get_live_fixtures();
$widget_upcoming = get_upcoming_fixtures();
$all_matches = array_merge(
    array_map(fn($m) => array_merge($m, ['_widget_type' => 'live']), $widget_live),
    array_slice(array_map(fn($m) => array_merge($m, ['_widget_type' => 'upcoming']), $widget_upcoming), 0, 5)
);
?>
<div class="card" style="margin-top:24px;">
    <div class="card-header" style="padding:16px 20px;">
        <div style="display:flex; align-items:center; gap:10px;">
            <span style="display:inline-block; width:8px; height:8px; background:#ff4d6d; border-radius:50%; animation:pulse-dot 1.5s infinite;"></span>
            <h3 style="margin:0;">Live Cricket Scores</h3>
            <?php if (!empty($widget_live)): ?>
            <span style="background:rgba(255,77,109,0.15); color:#ff4d6d; font-size:11px; font-weight:700; padding:2px 8px; border-radius:10px;">LIVE</span>
            <?php endif; ?>
        </div>
        <a href="<?= SITE_URL ?>/cricket-exchange/scores.php" target="_blank" class="btn btn-secondary btn-sm" style="font-size:12px;">
            View All →
        </a>
    </div>

    <?php if (empty($all_matches)): ?>
    <div style="padding:30px; text-align:center; color:var(--text-muted);">
        <div style="font-size:32px; margin-bottom:8px;">🏏</div>
        <p style="font-size:13px;">No live matches. Check <a href="<?= SITE_URL ?>/cricket-exchange/scores.php" style="color:var(--primary);">upcoming fixtures</a></p>
    </div>
    <?php else: ?>
    <div style="max-height:400px; overflow-y:auto;">
        <?php foreach ($all_matches as $m):
            $local = $m['localteam'] ?? $m['localTeam'] ?? [];
            $visitor = $m['visitorteam'] ?? $m['visitorTeam'] ?? [];
            $league = $m['league'] ?? [];
            $is_live = $m['_widget_type'] === 'live';
            $local_score = $is_live ? get_score_string($m, $local['id'] ?? 0) : null;
            $visitor_score = $is_live ? get_score_string($m, $visitor['id'] ?? 0) : null;
        ?>
        <a href="<?= SITE_URL ?>/cricket-exchange/match.php?id=<?= $m['id'] ?>" target="_blank" style="text-decoration:none;">
        <div style="padding:14px 20px; border-bottom:1px solid var(--border); transition:background 0.2s; display:block;" onmouseover="this.style.background='rgba(255,255,255,0.03)'" onmouseout="this.style.background=''">
            <!-- League + Status -->
            <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:8px;">
                <span style="font-size:11px; color:var(--text-muted); font-weight:500; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; max-width:60%;"><?= htmlspecialchars($league['name'] ?? 'Cricket') ?></span>
                <?php if ($is_live): ?>
                <span style="background:rgba(255,77,109,0.15); color:#ff4d6d; font-size:10px; font-weight:700; padding:2px 8px; border-radius:10px; border:1px solid rgba(255,77,109,0.3); white-space:nowrap;">● LIVE</span>
                <?php else: ?>
                <span style="background:rgba(0,212,170,0.1); color:var(--primary); font-size:10px; font-weight:700; padding:2px 8px; border-radius:10px; white-space:nowrap;">📅 <?= $m['starting_at'] ? date('d M H:i', strtotime($m['starting_at'])) : 'Upcoming' ?></span>
                <?php endif; ?>
            </div>
            <!-- Teams & Scores -->
            <div style="display:flex; align-items:center; gap:12px;">
                <!-- Team A -->
                <div style="flex:1; min-width:0;">
                    <div style="font-size:13px; font-weight:700; color:var(--text); overflow:hidden; text-overflow:ellipsis; white-space:nowrap;"><?= htmlspecialchars($local['name'] ?? 'Team A') ?></div>
                    <?php if ($is_live && $local_score !== '-'): ?>
                    <div style="font-size:15px; font-weight:800; color:var(--text); line-height:1.2; margin-top:2px;"><?= $local_score ?></div>
                    <?php endif; ?>
                </div>
                <!-- VS -->
                <div style="font-size:11px; color:var(--text-dim); font-weight:700; background:var(--bg-card2); padding:4px 10px; border-radius:6px; flex-shrink:0;">VS</div>
                <!-- Team B -->
                <div style="flex:1; min-width:0; text-align:right;">
                    <div style="font-size:13px; font-weight:700; color:var(--text); overflow:hidden; text-overflow:ellipsis; white-space:nowrap;"><?= htmlspecialchars($visitor['name'] ?? 'Team B') ?></div>
                    <?php if ($is_live && $visitor_score !== '-'): ?>
                    <div style="font-size:15px; font-weight:800; color:var(--text); line-height:1.2; margin-top:2px;"><?= $visitor_score ?></div>
                    <?php endif; ?>
                </div>
            </div>
            <?php if ($is_live && !empty($m['note'])): ?>
            <div style="margin-top:8px; font-size:11px; color:var(--primary); font-weight:500;"><?= htmlspecialchars($m['note']) ?></div>
            <?php endif; ?>
        </div>
        </a>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
    <style>
    @keyframes pulse-dot { 0%,100%{opacity:1} 50%{opacity:0.3} }
    </style>
</div>
