<!doctype html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{fragments/layout :: head('DB 준비 상태')}"></head>
<body>
<nav th:replace="~{fragments/layout :: nav}"></nav>
<main class="container py-4 settings-page">
  <div class="page-title">
    <a class="back-link" href="/settings">← 시스템 설정으로 돌아가기</a>
    <h1>DB 준비 상태</h1>
    <details class="explanation-details">
      <summary>도움말</summary>
      <p><strong>지원 테이블 생성/확인</strong>은 현재 연결 사용자의 운영 메타데이터를 생성하거나 보강합니다. VPD context, ORDS enablement, grant는 여기서 실행하지 않으며, 아래 sqlcl preview 순서를 따릅니다.</p>
    </details>
  </div>

  <div class="alert alert-success" th:if="${message}" th:text="${message}"></div>
  <div class="alert alert-danger" th:if="${error}" th:text="${error}"></div>
  <div class="alert alert-warning" th:if="${schemaPreflightErrorMessage}">
    <strong th:text="${schemaPreflightErrorTitle}">DB 준비 상태 확인 불가</strong>
    <div th:text="${schemaPreflightErrorMessage}">DB 연결을 확인하세요.</div>
  </div>

  <section class="content-band" th:if="${schemaPreflight}">
    <div class="section-heading">
      <div>
        <span class="architecture-kicker">읽기 전용 점검</span>
        <h2>현재 DB 준비 상태</h2>
        <p class="section-subtitle">현재 연결 사용자 기준으로 메타데이터, VPD runtime, ORDS runtime, grant 상태를 점검합니다.</p>
      </div>
      <a class="btn btn-sm rw-btn-secondary" href="/operation-status">운영 현황 보기</a>
    </div>
    <div class="schema-summary">
      <div><span>현재 연결 사용자</span><strong th:text="${schemaPreflight.currentUser()}">ADMIN</strong></div>
      <div><span>권장 ORDS schema</span><strong th:text="${schemaPreflight.recommendedOrdsSchema()}">CB_ORDS</strong></div>
      <div><span>OK</span><strong th:text="${schemaPreflight.okCount()}">0</strong></div>
      <div><span>WARN</span><strong th:text="${schemaPreflight.warnCount()}">0</strong></div>
      <div><span>MISSING</span><strong th:text="${schemaPreflight.missingCount()}">0</strong></div>
      <div><span>ERROR</span><strong th:text="${schemaPreflight.errorCount()}">0</strong></div>
    </div>
    <div class="table-responsive mt-3">
      <table class="table table-sm align-middle schema-preflight-table">
        <thead><tr><th>Layer</th><th>Item</th><th>Type</th><th>Status</th><th>Detail</th><th>Next Action</th></tr></thead>
        <tbody>
        <tr th:each="check : ${schemaPreflight.checks()}">
          <td th:text="${check.layer()}">운영 메타데이터</td>
          <td><code th:text="${check.itemName()}">CB_APP_USER</code></td>
          <td th:text="${check.objectType()}">TABLE</td>
          <td><span class="badge" th:classappend="${check.ok()} ? ' text-bg-success' : (${check.warning()} ? ' text-bg-warning' : (${check.missing()} ? ' text-bg-secondary' : ' text-bg-danger'))" th:text="${check.status()}">OK</span></td>
          <td th:text="${check.detail()}">존재합니다.</td>
          <td><span class="badge" th:classappend="${check.sqlclRequired()} ? ' text-bg-warning' : ' text-bg-info'" th:text="${check.sqlclRequired()} ? 'sqlcl 필요' : '앱 실행 가능'">앱 실행 가능</span><div class="mt-1" th:text="${check.nextAction()}">조치 없음</div></td>
        </tr>
        </tbody>
      </table>
    </div>
  </section>

  <section class="content-band" th:if="${schemaPreflight}">
    <h2>실행 전 SQL 확인</h2>
    <div class="schema-preview-grid mt-3">
      <details><summary>앱에서 실행하는 지원 DDL</summary><pre class="schema-sql-preview" th:text="${schemaPreflight.appDdlPreview()}">CREATE TABLE ...</pre></details>
      <details><summary>sqlcl로 실행할 VPD/ORDS runtime 순서</summary><pre class="schema-sql-preview" th:text="${schemaPreflight.sqlclScript()}">@sql/adb/...</pre></details>
    </div>
  </section>

  <section class="content-band settings-danger-zone">
    <div class="section-heading">
      <div>
        <span class="architecture-kicker">영향 있는 작업</span>
        <h2>지원 테이블 생성/확인</h2>
        <p class="section-subtitle">현재 연결 사용자에 운영 지원 테이블, 시퀀스, 컬럼을 생성하거나 보강합니다. VPD·ORDS runtime은 실행하지 않습니다.</p>
      </div>
    </div>
    <ul class="settings-impact-list">
      <li>실행 대상과 SQL은 위 preview에서 먼저 확인합니다.</li>
      <li>운영 데이터 백업과 DB 권한을 확인한 뒤 실행합니다.</li>
      <li>실행 결과는 이 화면에 남고, 실패한 SQL을 개별 확인할 수 있습니다.</li>
    </ul>
    <form method="post" action="/settings/database/initialize" class="form-grid settings-confirm-form">
      <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
      <label>
        확인 문구 입력
        <input class="form-control" name="confirmation" placeholder="DB 준비 실행" required autocomplete="off">
        <span class="form-hint">실행하려면 <code>DB 준비 실행</code>을 정확히 입력하세요.</span>
      </label>
      <button class="btn btn-outline-danger" type="submit">지원 DDL 실행</button>
    </form>
    <div class="table-responsive mt-3" th:if="${schemaResults}">
      <table class="table table-sm align-middle">
        <thead><tr><th>Object</th><th>Action</th><th>Status</th><th>Message</th><th>SQL</th></tr></thead>
        <tbody>
        <tr th:each="result : ${schemaResults}">
          <td><code th:text="${result.objectName()}">object</code></td><td th:text="${result.action()}">TABLE</td>
          <td><span class="badge" th:classappend="${result.status() == 'CREATED' || result.status() == 'MERGED' || result.status() == 'UPDATED'} ? ' text-bg-success' : (${result.status() == 'EXISTS'} ? ' text-bg-secondary' : ' text-bg-danger')" th:text="${result.status()}">CREATED</span></td>
          <td th:text="${result.message()}">생성했습니다.</td>
          <td><details th:if="${result.sqlText()}"><summary>보기</summary><pre class="table-pre" th:text="${result.sqlText()}">CREATE TABLE ...</pre></details><span class="text-muted" th:unless="${result.sqlText()}">-</span></td>
        </tr>
        </tbody>
      </table>
    </div>
  </section>
</main>
</body>
</html>
