{% extends "base.html" %}

{% block content %}
  <div class="grid two-col">
    <section class="card">
      <div class="kicker">Reporting Controls</div>
      <h2>Generate Report</h2>
      <div class="muted mono">engagement={{ engagement.id }} · generated={{ generated_at.strftime("%Y-%m-%d %H:%M:%S") }}</div>

      <form action="/dashboard/report/{{ 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 Evidence</span>
        </label>
        <label class="toggle-row">
          <input class="toggle-control" type="checkbox" name="include_ai" {% if include_ai != false %}checked{% endif %} />
          <span class="toggle-label">Include AI Sections</span>
        </label>
        <label>
          Vault Query (optional)
          <textarea name="vault_query" placeholder="credential reuse lateral movement">{{ vault_query }}</textarea>
        </label>
        <div class="btn-row">
          <button class="btn" type="submit">Refresh Preview</button>
          <button
            class="btn secondary"
            id="download-report-btn"
            type="button"
            data-engagement-id="{{ engagement.id }}"
            data-include-vault="{{ 1 if include_vault else 0 }}"
            data-include-ai="{% if include_ai is none %}none{% elif include_ai %}1{% else %}0{% endif %}"
            data-vault-query="{{ vault_query }}"
          >
            Download Markdown
          </button>
          <a class="btn secondary" href="/dashboard/notes/{{ engagement.id }}">Open Notes</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>Preview Source</h3>
      <pre class="mono" style="margin: 0; max-height: 420px; overflow: auto; white-space: pre-wrap;">{{ report_markdown }}</pre>
    </section>
  </div>

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

{% block page_scripts %}
  <script>
    (() => {
      const button = document.getElementById("download-report-btn")
      if (!button) return

      button.addEventListener("click", () => {
        const engagementId = button.getAttribute("data-engagement-id")
        if (!engagementId) return
        const params = new URLSearchParams()
        params.set("include_vault", button.getAttribute("data-include-vault") || "0")
        const includeAi = button.getAttribute("data-include-ai")
        if (includeAi && includeAi !== "none") {
          params.set("include_ai", includeAi === "1" ? "1" : "0")
        }
        const vaultQuery = button.getAttribute("data-vault-query") || ""
        if (vaultQuery.trim()) {
          params.set("vault_query", vaultQuery)
        }
        window.location.href = `/dashboard/report/${encodeURIComponent(engagementId)}/download?${params.toString()}`
      })
    })()
  </script>
{% endblock %}
