<%@ 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" %>
<%@ page import="com.pandoli365.bibimbap.data.PostCategoryData" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.time.OffsetDateTime" %>
<%@ page import="java.util.Collections" %>
<%@ page import="java.util.List" %>
<%
    String ctx = request.getContextPath();
    List<PostData> posts = Collections.emptyList();
    Object postsAttr = request.getAttribute("posts");
    if (postsAttr instanceof List<?>) { posts = (List<PostData>) postsAttr; }
    List<PostCategoryData> categories = Collections.emptyList();
    Object categoriesAttr = request.getAttribute("categories");
    if (categoriesAttr instanceof List<?>) { categories = (List<PostCategoryData>) categoriesAttr; }
    Long categoryId = (Long) request.getAttribute("categoryId");
    boolean hasNext = Boolean.TRUE.equals(request.getAttribute("hasNext"));
    OffsetDateTime nextCursorCreatedAt = (OffsetDateTime) request.getAttribute("nextCursorCreatedAt");
    Long nextCursorId = (Long) request.getAttribute("nextCursorId");
%>
<!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>포스팅 | 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);
      }
      .posts-page {
        max-width: 72rem;
        margin: 0 auto;
        padding: 1.5rem max(1rem, env(safe-area-inset-left)) 3rem max(1rem, env(safe-area-inset-right));
      }
      .posts-hero {
        display: flex;
        align-items: flex-end;
        justify-content: space-between;
        gap: 1rem;
        margin-bottom: 1rem;
      }
      .posts-hero__eyebrow {
        margin: 0 0 0.35rem;
        color: var(--accent);
        font-size: 0.75rem;
        font-weight: 900;
      }
      .posts-hero h1 {
        margin: 0;
        font-size: 1.9rem;
        line-height: 1.2;
        letter-spacing: 0;
      }
      .posts-write {
        min-height: 2.75rem;
        padding: 0 1rem;
        border-radius: 10px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        background: var(--accent);
        color: #1a1a1a;
        font-size: 0.9375rem;
        font-weight: 900;
        text-decoration: none;
        white-space: nowrap;
      }
      .posts-tabs {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        margin-bottom: 1.25rem;
      }
      .posts-tab {
        min-height: 2.25rem;
        border: 1px solid var(--border);
        border-radius: 999px;
        padding: 0 0.95rem;
        display: inline-flex;
        align-items: center;
        background: var(--card-bg);
        color: var(--text);
        font-size: 0.875rem;
        font-weight: 800;
        text-decoration: none;
      }
      .posts-tab.is-active {
        border-color: rgba(232, 165, 75, 0.65);
        background: var(--accent-soft);
        color: var(--accent);
      }
      .posts-grid {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 1rem;
      }
      .post-card {
        border: 1px solid var(--border);
        border-radius: 12px;
        display: flex;
        flex-direction: column;
        background: var(--card-bg);
        color: inherit;
        text-decoration: none;
        overflow: hidden;
        box-shadow: 0 2px 8px var(--shadow);
      }
      .post-card:hover {
        border-color: rgba(232, 165, 75, 0.45);
        box-shadow: 0 8px 20px var(--shadow);
        transform: translateY(-2px);
      }
      .post-card__thumb {
        width: 100%;
        aspect-ratio: 16 / 9;
        object-fit: cover;
        display: block;
        background: var(--surface);
      }
      .post-card__body {
        padding: 1.05rem;
        display: flex;
        flex-direction: column;
        gap: 0.55rem;
      }
      .post-card__category {
        align-self: flex-start;
        min-height: 1.6rem;
        padding: 0 0.55rem;
        border-radius: 8px;
        display: inline-flex;
        align-items: center;
        background: var(--accent-soft);
        color: var(--accent);
        font-size: 0.7rem;
        font-weight: 900;
      }
      .post-card h2 {
        margin: 0;
        font-size: 1.1rem;
        line-height: 1.4;
        letter-spacing: 0;
        word-break: break-word;
      }
      .post-card__author {
        color: var(--text-muted);
        font-size: 0.8125rem;
      }
      .posts-empty {
        min-height: 10rem;
        border: 1px dashed var(--border);
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: var(--card-bg);
        color: var(--text-muted);
        text-align: center;
      }
      .posts-more {
        margin: 1.5rem auto 0;
        max-width: 16rem;
        min-height: 2.85rem;
        border: 1px solid var(--border);
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: var(--card-bg);
        color: var(--text);
        font-size: 0.9375rem;
        font-weight: 900;
        text-decoration: none;
      }
      @media (max-width: 900px) {
        .posts-grid {
          grid-template-columns: repeat(2, minmax(0, 1fr));
        }
      }
      @media (max-width: 640px) {
        .posts-hero {
          align-items: stretch;
          flex-direction: column;
        }
        .posts-write {
          width: 100%;
          box-sizing: border-box;
        }
        .posts-grid {
          grid-template-columns: 1fr;
        }
      }
    </style>
</head>
<body>
    <jsp:include page="/WEB-INF/views/header.jsp"/>
    <main class="posts-page">
        <section class="posts-hero" aria-labelledby="posts-title">
            <div>
                <p class="posts-hero__eyebrow">POSTING</p>
                <h1 id="posts-title">포스팅</h1>
            </div>
            <a class="posts-write" href="<%= ctx %>/posts/new">글쓰기</a>
        </section>

        <nav class="posts-tabs" aria-label="카테고리">
            <a class="posts-tab <%= categoryId == null ? "is-active" : "" %>"
               href="<%= ctx %>/posts">전체</a>
            <% for (PostCategoryData cat : categories) { %>
                <a class="posts-tab <%= categoryId != null && categoryId.equals(cat.getId()) ? "is-active" : "" %>"
                   href="<%= ctx %>/posts?categoryId=<%= cat.getId() %>"><%= HtmlUtils.htmlEscape(cat.getName() == null ? "" : cat.getName()) %></a>
            <% } %>
        </nav>

        <% if (posts.isEmpty()) { %>
            <%@ include file="fragments/posts-empty.jspf" %>
        <% } else { %>
            <section class="posts-grid" aria-label="포스트 목록">
                <% for (PostData post : posts) { %>
                    <a class="post-card" href="<%= ctx %>/posts/<%= post.getId() %>">
                        <% if (post.getOgImageUrl() != null && !post.getOgImageUrl().isBlank()) { %>
                            <img class="post-card__thumb" src="<%= HtmlUtils.htmlEscape(post.getOgImageUrl()) %>" alt="" loading="lazy">
                        <% } %>
                        <div class="post-card__body">
                            <% if (post.getCategoryName() != null && !post.getCategoryName().isBlank()) { %>
                                <span class="post-card__category"><%= HtmlUtils.htmlEscape(post.getCategoryName()) %></span>
                            <% } %>
                            <h2><%= HtmlUtils.htmlEscape(post.getTitle() == null ? "" : post.getTitle()) %></h2>
                            <span class="post-card__author"><%= HtmlUtils.htmlEscape(post.getAuthorDisplayName() == null ? "" : post.getAuthorDisplayName()) %></span>
                        </div>
                    </a>
                <% } %>
            </section>

            <% if (hasNext) {
                StringBuilder moreUrl = new StringBuilder(ctx).append("/posts?");
                moreUrl.append("cursorCreatedAt=").append(URLEncoder.encode(String.valueOf(nextCursorCreatedAt), "UTF-8"));
                moreUrl.append("&cursorId=").append(nextCursorId);
                if (categoryId != null) {
                    moreUrl.append("&categoryId=").append(categoryId);
                }
            %>
                <a class="posts-more" href="<%= HtmlUtils.htmlEscape(moreUrl.toString()) %>">더보기</a>
            <% } %>
        <% } %>
    </main>
    <jsp:include page="/WEB-INF/views/footer.jsp"/>
</body>
</html>
