<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.pandoli365.bibimbap.data.JamData" %>
<%@ page import="com.pandoli365.bibimbap.data.JamAwardData" %>
<%@ page import="org.springframework.web.util.HtmlUtils" %>
<%
    String ctx = request.getContextPath();
    JamData jam = (JamData) request.getAttribute("jam");
    Map<String, List<JamAwardData>> byTrack =
        (Map<String, List<JamAwardData>>) request.getAttribute("byTrack");
    List<JamAwardData> grand = (List<JamAwardData>) request.getAttribute("grand");
    boolean noResults = (byTrack == null || byTrack.isEmpty());
%>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <jsp:include page="/WEB-INF/views/theme-init.jsp"/>
    <title><%= jam == null ? "게임잼 결과" : HtmlUtils.htmlEscape(jam.getTitle() == null ? "게임잼 결과" : jam.getTitle()) %> 결과 | bibimbap</title>
    <style>
      html {
        color-scheme: light;
        --surface: #faf8f5;
        --card-bg: #fff;
        --text: #1a1a1a;
        --text-muted: #5c5c5c;
        --accent: #e8a54b;
        --accent-soft: rgba(232, 165, 75, 0.16);
        --border: rgba(0, 0, 0, 0.08);
        --shadow: rgba(0, 0, 0, 0.06);
      }
      html[data-theme="dark"] {
        color-scheme: dark;
        --surface: #121212;
        --card-bg: #1e1e1e;
        --text: #ece8e1;
        --text-muted: #a39e96;
        --border: rgba(255, 255, 255, 0.1);
        --shadow: rgba(0, 0, 0, 0.35);
      }
      body {
        margin: 0;
        min-height: 100vh;
        font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans KR", sans-serif;
        background: var(--surface);
        color: var(--text);
      }
      .detail-page {
        max-width: 64rem;
        margin: 0 auto;
        padding: 1.5rem max(1rem, env(safe-area-inset-left)) 3rem max(1rem, env(safe-area-inset-right));
      }
      .detail-back {
        display: inline-flex;
        align-items: center;
        gap: 0.4rem;
        min-height: 2.25rem;
        margin-bottom: 1rem;
        color: var(--text-muted);
        font-size: 0.875rem;
        font-weight: 800;
        text-decoration: none;
      }
      .detail-back:hover {
        color: var(--accent);
      }
      .detail-hero {
        margin-bottom: 1rem;
        padding: 1.5rem;
        border: 1px solid var(--border);
        border-radius: 12px;
        background: var(--card-bg);
        box-shadow: 0 2px 8px var(--shadow);
      }
      .detail-hero h1 {
        margin: 0;
        font-size: 2rem;
        line-height: 1.2;
        letter-spacing: 0;
      }
      .detail-section {
        margin-bottom: 1rem;
        padding: 1.25rem;
        border: 1px solid var(--border);
        border-radius: 12px;
        background: var(--card-bg);
        box-shadow: 0 2px 8px var(--shadow);
      }
      .detail-section h2 {
        margin: 0 0 0.9rem;
        font-size: 1.1rem;
        letter-spacing: 0;
      }
      .empty-note {
        color: var(--text-muted);
        font-size: 0.9rem;
      }
      .award-table {
        width: 100%;
        border-collapse: collapse;
        font-size: 0.9rem;
      }
      .award-table th,
      .award-table td {
        padding: 0.55rem 0.65rem;
        border-bottom: 1px solid var(--border);
        text-align: left;
        vertical-align: middle;
      }
      .award-table th {
        color: var(--text-muted);
        font-size: 0.78rem;
        font-weight: 800;
        text-transform: uppercase;
        letter-spacing: 0.02em;
      }
      .award-table td.award-rank {
        width: 3rem;
        font-weight: 900;
        color: var(--accent);
      }
      .award-table td.award-score {
        text-align: right;
        white-space: nowrap;
        color: var(--text-muted);
      }
      .award-thumb {
        width: 3rem;
        height: 1.7rem;
        object-fit: cover;
        border-radius: 4px;
        background: var(--accent-soft);
        vertical-align: middle;
        margin-right: 0.5rem;
      }
      .award-game {
        font-weight: 800;
        color: var(--text);
        text-decoration: none;
      }
      .award-game:hover {
        color: var(--accent);
      }
      .award-entrant {
        color: var(--text-muted);
        font-size: 0.82rem;
      }
    </style>
</head>
<body>
    <jsp:include page="/WEB-INF/views/header.jsp"/>
    <main class="detail-page">
        <a class="detail-back" href="<%= ctx %>/jams">← 게임잼 목록</a>
        <section class="detail-hero" aria-labelledby="jam-results-title">
            <h1 id="jam-results-title"><%= jam == null ? "게임잼 결과" : HtmlUtils.htmlEscape(jam.getTitle() == null ? "게임잼" : jam.getTitle()) %> 결과</h1>
        </section>
<%
    if (noResults) {
%>
        <section class="detail-section">
            <p class="empty-note">결과 준비 중입니다.</p>
        </section>
<%
    } else {
%>
        <section class="detail-section" aria-labelledby="jam-grand">
            <h2 id="jam-grand">종합 대상</h2>
<%
        if (grand == null || grand.isEmpty()) {
%>
            <p class="empty-note">해당 트랙 결과 없음</p>
<%
        } else {
%>
            <table class="award-table">
                <thead>
                    <tr><th>순위</th><th>게임</th><th>출품자</th><th class="award-score">점수</th></tr>
                </thead>
                <tbody>
<%
            for (JamAwardData award : grand) {
                if (award == null) {
                    continue;
                }
                Integer rank = award.getRank();
                Long gameId = award.getGameId();
                String gameName = award.getGameName() == null || award.getGameName().isBlank() ? "이름 없는 게임" : award.getGameName();
                String thumbnailUrl = award.getThumbnailUrl();
                String entrantName = award.getEntrantName();
                java.math.BigDecimal scoreValue = award.getScoreValue();
%>
                    <tr>
                        <td class="award-rank"><%= rank == null ? "-" : rank.intValue() %></td>
                        <td>
<%
                if (thumbnailUrl != null && !thumbnailUrl.isBlank()) {
%>
                            <img class="award-thumb" src="<%= HtmlUtils.htmlEscape(thumbnailUrl) %>" alt="">
<%
                }
                if (gameId != null) {
%>
                            <a class="award-game" href="<%= ctx %>/game/<%= gameId %>"><%= HtmlUtils.htmlEscape(gameName) %></a>
<%
                } else {
%>
                            <span class="award-game"><%= HtmlUtils.htmlEscape(gameName) %></span>
<%
                }
%>
                        </td>
                        <td class="award-entrant"><%= entrantName == null || entrantName.isBlank() ? "" : HtmlUtils.htmlEscape(entrantName) %></td>
                        <td class="award-score"><%= scoreValue == null ? "" : HtmlUtils.htmlEscape(String.valueOf(scoreValue)) %></td>
                    </tr>
<%
            }
%>
                </tbody>
            </table>
<%
        }
%>
        </section>
<%
        String[] trackKeys = { "JUDGE", "USER_RATING", "POPULAR" };
        String[] trackLabels = { "심사상", "유저 평점상", "인기상" };
        for (int t = 0; t < trackKeys.length; t++) {
            String trackKey = trackKeys[t];
            String trackLabel = trackLabels[t];
            List<JamAwardData> rows = byTrack.get(trackKey);
%>
        <section class="detail-section" aria-labelledby="jam-track-<%= trackKey %>">
            <h2 id="jam-track-<%= trackKey %>"><%= trackLabel %></h2>
<%
            if (rows == null || rows.isEmpty()) {
%>
            <p class="empty-note">해당 트랙 결과 없음</p>
<%
            } else {
%>
            <table class="award-table">
                <thead>
                    <tr><th>순위</th><th>게임</th><th>출품자</th><th class="award-score">점수</th></tr>
                </thead>
                <tbody>
<%
                for (JamAwardData award : rows) {
                    if (award == null) {
                        continue;
                    }
                    Integer rank = award.getRank();
                    Long gameId = award.getGameId();
                    String gameName = award.getGameName() == null || award.getGameName().isBlank() ? "이름 없는 게임" : award.getGameName();
                    String thumbnailUrl = award.getThumbnailUrl();
                    String entrantName = award.getEntrantName();
                    java.math.BigDecimal scoreValue = award.getScoreValue();
%>
                    <tr>
                        <td class="award-rank"><%= rank == null ? "-" : rank.intValue() %></td>
                        <td>
<%
                    if (thumbnailUrl != null && !thumbnailUrl.isBlank()) {
%>
                            <img class="award-thumb" src="<%= HtmlUtils.htmlEscape(thumbnailUrl) %>" alt="">
<%
                    }
                    if (gameId != null) {
%>
                            <a class="award-game" href="<%= ctx %>/game/<%= gameId %>"><%= HtmlUtils.htmlEscape(gameName) %></a>
<%
                    } else {
%>
                            <span class="award-game"><%= HtmlUtils.htmlEscape(gameName) %></span>
<%
                    }
%>
                        </td>
                        <td class="award-entrant"><%= entrantName == null || entrantName.isBlank() ? "" : HtmlUtils.htmlEscape(entrantName) %></td>
                        <td class="award-score"><%= scoreValue == null ? "" : HtmlUtils.htmlEscape(String.valueOf(scoreValue)) %></td>
                    </tr>
<%
                }
%>
                </tbody>
            </table>
<%
            }
%>
        </section>
<%
        }
%>
<%
    }
%>
    </main>
    <jsp:include page="/WEB-INF/views/footer.jsp"/>
</body>
</html>
