<?php
// =============================================================================
// /api/webview/sports/dashboard.php — sports overview card the WebView shows
// when the user picks the "Sports Dashboard" widget. Combines live, upcoming
// and a league sidebar.
// =============================================================================
require_once __DIR__ . '/../_wv_common.php';

wv_maybe_bootstrap_sports();

$leagues = db_all('SELECT id, name, logo_url FROM sports_leagues WHERE enabled=1 ORDER BY sort ASC, name ASC LIMIT 12');
$live    = db_all(
    'SELECT e.*, l.name AS league_name, l.logo_url AS league_logo_url
     FROM sports_events e LEFT JOIN sports_leagues l ON l.id = e.league_id
     WHERE e.status = "LIVE" ORDER BY e.kickoff_ts ASC LIMIT 8');
$upcoming= db_all(
    'SELECT e.*, l.name AS league_name, l.logo_url AS league_logo_url
     FROM sports_events e LEFT JOIN sports_leagues l ON l.id = e.league_id
     WHERE e.status = "UPCOMING" AND e.kickoff_ts >= ? ORDER BY e.kickoff_ts ASC LIMIT 12',
    [time()]);
// Fallback: if no live/upcoming, show recent FT so the dashboard isn't empty.
if (!$live && !$upcoming) {
    $upcoming = db_all(
        'SELECT e.*, l.name AS league_name, l.logo_url AS league_logo_url
         FROM sports_events e LEFT JOIN sports_leagues l ON l.id = e.league_id
         WHERE e.status = "FT" ORDER BY e.kickoff_ts DESC LIMIT 12');
}
$live     = array_map('wv_event_view', $live);
$upcoming = array_map('wv_event_view', $upcoming);

wv_html_head('Sports Dashboard', '<style>
.wrap{display:grid;grid-template-columns:220px 1fr;height:100vh}
.side{background:#0e0e0e;border-right:1px solid #2a2a2a;padding:14px;overflow-y:auto;}
.side h4{font-size:11px;font-weight:700;color:var(--accent);text-transform:uppercase;letter-spacing:.06em;margin-bottom:10px}
.lg{display:flex;align-items:center;gap:8px;padding:8px 10px;border-radius:6px;margin-bottom:2px;cursor:pointer;color:#ccc;font-size:13px;}
.lg:hover{background:#1a1a1a;color:#fff;}
.lg img{width:18px;height:18px;border-radius:3px;background:#222;}
.main{padding:18px;overflow-y:auto;}
.section{margin-bottom:24px}
.section h2{font-size:14px;font-weight:800;color:var(--accent);text-transform:uppercase;letter-spacing:.06em;margin-bottom:10px}
.grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:10px}
.card{background:var(--card);border:1px solid #2a2a2a;border-radius:8px;padding:10px;font-size:12px;}
.card .lg-name{color:var(--muted);font-size:10px;margin-bottom:6px;text-transform:uppercase}
.card .teams{display:grid;grid-template-columns:1fr auto 1fr;align-items:center;gap:6px;}
.card .t{display:flex;align-items:center;gap:6px;font-weight:600;}
.card .t.away{justify-content:flex-end;}
.card .t img{width:22px;height:22px;border-radius:4px;background:#222;object-fit:contain;}
.card .score{font-weight:800;color:#fff;text-align:center;min-width:50px;}
.card .live{display:inline-block;font-size:9px;color:#f87171;background:rgba(239,68,68,.15);padding:2px 6px;border-radius:99px;margin-left:6px}
.empty{color:var(--muted);font-size:12px;padding:8px 0;}
</style>');
?>
<div class="wrap">
    <aside class="side">
        <h4>Leagues</h4>
        <?php foreach ($leagues as $l): ?>
            <a class="lg" href="sport_l_table.php?league_id=<?php echo (int)$l['id']; ?>">
                <?php if ($l['logo_url']): ?><img src="<?php echo htmlspecialchars($l['logo_url'], ENT_QUOTES, 'UTF-8'); ?>" alt=""><?php else: ?><span style="width:18px;height:18px;background:#222;border-radius:3px"></span><?php endif; ?>
                <span><?php echo htmlspecialchars($l['name'], ENT_QUOTES, 'UTF-8'); ?></span>
            </a>
        <?php endforeach; ?>
        <?php if (!$leagues): ?><div class="empty">Loading leagues…</div><?php endif; ?>
    </aside>
    <main class="main">
        <div class="section">
            <h2>Live now</h2>
            <?php if (!$live): ?><div class="empty">Nothing live right now — check back soon.</div><?php else: ?>
            <div class="grid">
                <?php foreach ($live as $e): ?>
                    <div class="card">
                        <div class="lg-name"><?php echo htmlspecialchars($e['league_name'] ?: '—', ENT_QUOTES, 'UTF-8'); ?><span class="live">LIVE</span></div>
                        <div class="teams">
                            <span class="t"><?php if ($e['home_badge_url']): ?><img src="<?php echo htmlspecialchars($e['home_badge_url'], ENT_QUOTES, 'UTF-8'); ?>" alt=""><?php endif; ?><?php echo htmlspecialchars($e['home_team'], ENT_QUOTES, 'UTF-8'); ?></span>
                            <span class="score"><?php echo (int)$e['home_score']; ?> - <?php echo (int)$e['away_score']; ?></span>
                            <span class="t away"><?php echo htmlspecialchars($e['away_team'], ENT_QUOTES, 'UTF-8'); ?><?php if ($e['away_badge_url']): ?><img src="<?php echo htmlspecialchars($e['away_badge_url'], ENT_QUOTES, 'UTF-8'); ?>" alt=""><?php endif; ?></span>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
        <div class="section">
            <h2>Upcoming</h2>
            <?php if (!$upcoming): ?><div class="empty">No upcoming fixtures right now.</div><?php else: ?>
            <div class="grid">
                <?php foreach ($upcoming as $e): ?>
                    <div class="card">
                        <div class="lg-name"><?php echo htmlspecialchars($e['league_name'] ?: '—', ENT_QUOTES, 'UTF-8'); ?> · <?php echo $e['kickoff_ts'] ? date('D H:i', $e['kickoff_ts']) : ''; ?></div>
                        <div class="teams">
                            <span class="t"><?php if ($e['home_badge_url']): ?><img src="<?php echo htmlspecialchars($e['home_badge_url'], ENT_QUOTES, 'UTF-8'); ?>" alt=""><?php endif; ?><?php echo htmlspecialchars($e['home_team'], ENT_QUOTES, 'UTF-8'); ?></span>
                            <span class="score">vs</span>
                            <span class="t away"><?php echo htmlspecialchars($e['away_team'], ENT_QUOTES, 'UTF-8'); ?><?php if ($e['away_badge_url']): ?><img src="<?php echo htmlspecialchars($e['away_badge_url'], ENT_QUOTES, 'UTF-8'); ?>" alt=""><?php endif; ?></span>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </main>
</div>
<?php wv_html_foot();
