{% extends "base.html" %}

{% block content %}
  <div class="grid two-col">
    <section class="card">
      <div class="kicker">Notes Controls</div>
      <h2>Generate / Update Notes</h2>
      <div class="muted mono">engagement={{ engagement.id }} · path={{ notes_path }}</div>
      <div class="muted" id="notes-updated-label" data-ts="{{ notes_updated_ts if notes_updated_ts else '' }}">
        {% if notes_exists %}Notes file exists.{% else %}No notes file yet.{% endif %}
      </div>

      <form action="/dashboard/notes/{{ engagement.id }}/generate" method="post" class="field-grid" style="margin-top: 12px;">
        <label class="toggle-row">
          <input class="toggle-control" type="checkbox" name="include_vault" {% if include_vault %}checked{% endif %} />
          <span class="toggle-label">Include Vault Snippets</span>
        </label>
        <label>
          Vault Query (optional)
          <textarea name="vault_query" placeholder="kerberos ticket abuse">{{ vault_query }}</textarea>
        </label>
        <div class="btn-row">
          <button class="btn" type="submit">Generate / Append Notes</button>
          <a class="btn secondary" href="/dashboard/report/{{ engagement.id }}">Open Report</a>
          <a class="btn secondary" href="/dashboard/engagement/{{ engagement.id }}">Back To Engagement</a>
        </div>
      </form>
    </section>

    <section class="card">
      <div class="kicker">Raw Markdown</div>
      <h3>Notes Source</h3>
      {% if notes_exists %}
        <pre class="mono" style="margin: 0; max-height: 420px; overflow: auto; white-space: pre-wrap;">{{ notes_markdown }}</pre>
      {% else %}
        <p class="muted">Generate notes to create the initial document.</p>
      {% endif %}
    </section>
  </div>

  <section class="card" style="margin-top: 14px;">
    <div class="kicker">Rendered Notes</div>
    <h3>Preview</h3>
    <div class="report-html">
      {{ notes_html | safe }}
    </div>
  </section>
{% endblock %}

{% block page_scripts %}
  <script>
    (() => {
      const label = document.getElementById("notes-updated-label")
      if (!label) return
      const raw = label.getAttribute("data-ts")
      if (!raw) return
      const ts = Number(raw)
      if (!Number.isFinite(ts)) return
      const dt = new Date(ts * 1000)
      if (Number.isNaN(dt.getTime())) return
      label.textContent = `Last updated: ${dt.toLocaleString()}`
    })()
  </script>
{% endblock %}
