<!doctype html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{fragments/layout :: head('보안 SQL 스크립트')}"></head>
<body>
<nav th:replace="~{fragments/layout :: nav}"></nav>
<main class="container py-4">
  <section class="page-title">
    <span class="badge text-bg-primary">형상 기준</span>
    <h1 class="mt-2">보안 SQL 스크립트</h1>
    <details class="explanation-details">
      <summary>도움말</summary>
      <p>ASO 마스킹, ORDS, Select AI 행 접근에 사용하는 Git 형상 기준 SQL을 읽기 전용으로 표시합니다. 이 화면은 SQL을 실행하거나 DB의 현재 source를 바꾸지 않습니다.</p>
    </details>
  </section>

  <section class="alert alert-danger" th:if="${errorMessage}" th:text="${errorMessage}">조회할 수 없습니다.</section>

  <section class="content-band">
    <div class="section-heading">
      <div>
        <h2>등록된 스크립트</h2>
        <p class="section-subtitle">배포 JAR에는 아래 Git 추적 파일만 포함됩니다.</p>
      </div>
      <span class="badge text-bg-secondary" th:text="${#lists.size(scripts)}">0</span>
    </div>
    <div class="table-responsive">
      <table class="table table-sm align-middle">
        <thead><tr><th>구분</th><th>파일</th><th>용도</th><th></th></tr></thead>
        <tbody>
        <tr th:each="item : ${scripts}" th:classappend="${selectedScript != null and item.scriptId() == selectedScript.scriptId()} ? ' table-primary'">
          <td><span class="badge text-bg-light" th:text="${item.category()}">ASO / 마스킹</span></td>
          <td><code th:text="${item.fileName()}">category/script.sql</code></td>
          <td>
            <strong th:text="${item.title()}">사용자·그룹·역할 관리 모델</strong>
            <div class="form-hint" th:text="${item.description()}">설명</div>
          </td>
          <td><a class="btn btn-sm rw-btn-secondary" th:href="@{/security-sql-scripts(script=${item.scriptId()})}">원문 보기</a></td>
        </tr>
        <tr th:if="${#lists.isEmpty(scripts)}"><td colspan="4" class="text-muted">표시할 스크립트가 없습니다.</td></tr>
        </tbody>
      </table>
    </div>
  </section>

  <section class="content-band" th:if="${selectedScript}">
    <div class="section-heading">
      <div>
        <span class="badge text-bg-secondary" th:text="${selectedScript.category()}">권한</span>
        <h2 class="mt-2" th:text="${selectedScript.title()}">사용자·그룹·역할 관리 모델</h2>
        <p class="section-subtitle" th:text="${selectedScript.description()}">설명</p>
      </div>
      <code th:text="${selectedScript.fileName()}">category/script.sql</code>
    </div>
    <p class="form-hint">Git source: <code th:text="${'sql/adb/' + selectedScript.fileName()}">sql/adb/category/script.sql</code>. 실제 DB 배포본은 <a href="/vpd-filter-runtime">행 접근 필터 구조</a> 및 DB 배포 이력과 함께 확인하세요.</p>
    <form hx-post="/security-sql-scripts/explanation" hx-target="#security-sql-explanation" hx-swap="innerHTML" class="mb-3">
      <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
      <input type="hidden" name="script" th:value="${selectedScript.scriptId()}">
      <button class="btn rw-btn-primary" type="submit">LLM 전체·블록·토큰 처리 설명 생성</button>
      <span class="form-hint ms-2">원문은 실행·수정하지 않고 설명을 만들 때만 AI에 전달합니다.</span>
    </form>
    <section id="security-sql-explanation" class="mb-3"></section>
    <section class="probe-exchange"><pre th:text="${selectedScript.source()}">-- SQL source</pre></section>
  </section>
</main>
</body>
</html>
