<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<%@ page import="org.springframework.web.util.HtmlUtils" %>
<%@ page import="com.pandoli365.bibimbap.data.PostData" %>
<%
    String ctx = request.getContextPath();
    PostData post = (PostData) request.getAttribute("post");
%>
<!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><%= HtmlUtils.htmlEscape(post.getTitle() == null ? "" : post.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);
      }
      .post-page {
        max-width: 48rem;
        margin: 0 auto;
        padding: 1.5rem max(1rem, env(safe-area-inset-left)) 3rem max(1rem, env(safe-area-inset-right));
      }
      .post-back {
        display: inline-flex;
        align-items: center;
        margin-bottom: 1rem;
        color: var(--text-muted);
        font-size: 0.875rem;
        font-weight: 800;
        text-decoration: none;
      }
      .post-back:hover {
        color: var(--accent);
      }
      .post-category {
        display: inline-flex;
        align-items: center;
        min-height: 1.7rem;
        padding: 0 0.6rem;
        border-radius: 8px;
        background: var(--accent-soft);
        color: var(--accent);
        font-size: 0.75rem;
        font-weight: 900;
      }
      .post-title {
        margin: 0.7rem 0 0.45rem;
        font-size: 1.85rem;
        line-height: 1.25;
        letter-spacing: 0;
        word-break: break-word;
      }
      .post-author {
        color: var(--text-muted);
        font-size: 0.875rem;
      }
      .post-body {
        margin-top: 1.5rem;
        line-height: 1.75;
        word-break: break-word;
      }
      .post-body img {
        max-width: 100%;
        height: auto;
      }
      .post-body pre {
        overflow-x: auto;
        padding: 0.9rem;
        border-radius: 10px;
        background: var(--card-bg);
        border: 1px solid var(--border);
      }
      .og-card {
        margin-top: 1.75rem;
        display: flex;
        gap: 0.9rem;
        border: 1px solid var(--border);
        border-radius: 12px;
        overflow: hidden;
        background: var(--card-bg);
        color: inherit;
        text-decoration: none;
        box-shadow: 0 2px 8px var(--shadow);
      }
      .og-card:hover {
        border-color: rgba(232, 165, 75, 0.45);
      }
      .og-card__img {
        width: 11rem;
        flex-shrink: 0;
        object-fit: cover;
        background: var(--surface);
      }
      .og-card__body {
        padding: 0.95rem 1rem;
        display: flex;
        flex-direction: column;
        gap: 0.35rem;
        min-width: 0;
      }
      .og-card__site {
        color: var(--text-muted);
        font-size: 0.75rem;
        font-weight: 800;
      }
      .og-card__title {
        font-size: 1rem;
        font-weight: 900;
        line-height: 1.4;
        word-break: break-word;
      }
      .og-card__desc {
        color: var(--text-muted);
        font-size: 0.8125rem;
        line-height: 1.5;
        word-break: break-word;
      }
      @media (max-width: 560px) {
        .og-card {
          flex-direction: column;
        }
        .og-card__img {
          width: 100%;
          aspect-ratio: 16 / 9;
        }
      }
    </style>
</head>
<body>
    <jsp:include page="/WEB-INF/views/header.jsp"/>
    <main class="post-page">
        <a class="post-back" href="<%= ctx %>/posts">목록으로</a>

        <% if (post.getCategoryName() != null && !post.getCategoryName().isBlank()) { %>
            <span class="post-category"><%= HtmlUtils.htmlEscape(post.getCategoryName()) %></span>
        <% } %>
        <h1 class="post-title"><%= HtmlUtils.htmlEscape(post.getTitle() == null ? "" : post.getTitle()) %></h1>
        <p class="post-author"><%= HtmlUtils.htmlEscape(post.getAuthorDisplayName() == null ? "" : post.getAuthorDisplayName()) %></p>

        <%-- 본문은 PostMarkdownService 가 sanitize 한 신뢰 HTML 을 직접 출력한다 (유일한 신뢰 원천). --%>
        <article class="post-body"><%= post.getBodySanitizedHtml() == null ? "" : post.getBodySanitizedHtml() %></article>

        <% if (post.getOgTitle() != null && !post.getOgTitle().isBlank()) { %>
            <a class="og-card" href="<%= HtmlUtils.htmlEscape(post.getLinkUrl() == null ? "" : post.getLinkUrl()) %>" rel="nofollow noopener" target="_blank">
                <% if (post.getOgImageUrl() != null && !post.getOgImageUrl().isBlank()) { %>
                    <img class="og-card__img" src="<%= HtmlUtils.htmlEscape(post.getOgImageUrl()) %>" alt="">
                <% } %>
                <span class="og-card__body">
                    <% if (post.getOgSiteName() != null && !post.getOgSiteName().isBlank()) { %>
                        <span class="og-card__site"><%= HtmlUtils.htmlEscape(post.getOgSiteName()) %></span>
                    <% } %>
                    <span class="og-card__title"><%= HtmlUtils.htmlEscape(post.getOgTitle()) %></span>
                    <% if (post.getOgDescription() != null && !post.getOgDescription().isBlank()) { %>
                        <span class="og-card__desc"><%= HtmlUtils.htmlEscape(post.getOgDescription()) %></span>
                    <% } %>
                </span>
            </a>
        <% } %>
    </main>
    <jsp:include page="/WEB-INF/views/footer.jsp"/>
</body>
</html>
