{% extends "base.html" %}
{% block title %}{{ case.case_id }} — psyc{% endblock %}
{% block content %}
<section class="panel">
  <a class="back" href="/cases">← back to cases</a>
  <div class="panel-head">
    <h1>{{ case.case_id }}</h1>
    <a class="replay-btn" href="/cases/{{ case.case_id }}/journey">▶ case journey</a>
  </div>
  <p class="summary-lead">{{ case.summary }}</p>
  <p class="page-intro">The full record for one case — how Classifyline rated it, what Scoutline observed, the evidence Sealine encrypted, where Routeline may send it, and every ledger entry it produced.</p>
  <details class="page-help">
    <summary>how to use this view</summary>
    <div class="help-body">
      <p><b>How to use.</b> Read the cards top-to-bottom — classification, observables, sealed package, routes, ledger. Hit <b>▶ case journey</b> for the animated walk-through.</p>
      <p><b>What you're seeing.</b> Every worker line's output for this one case: how it was rated, what was observed, what was encrypted and to whom, where it would route, and the audit rows it produced.</p>
      <p><b>Why it matters.</b> Nothing sensitive leaves psyc without a human seeing the full reasoning chain — this page is that chain, for one case.</p>
    </div>
  </details>

  <div class="grid">
    <div class="card">
      <h2>Classification</h2>
      <dl>
        <dt>Severity</dt><dd>{% if case.classification.severity %}<span class="sev-badge sev-{{ case.classification.severity.value }}">{{ case.classification.severity.value }}</span>{% else %}—{% endif %}</dd>
        <dt>TLP</dt><dd><span class="tlp-badge tlp-{{ case.classification.tlp.value }}">{{ case.classification.tlp.value }}</span></dd>
        <dt>Incident type</dt><dd>{{ case.classification.incident_type.value if case.classification.incident_type else '—' }}</dd>
        <dt>Internal class</dt><dd>{{ case.classification.internal_class.value if case.classification.internal_class else '—' }}</dd>
      </dl>
    </div>

    <div class="card">
      <h2>Confidence</h2>
      <dl>
        <dt>Level</dt><dd>{{ case.confidence.level }}</dd>
        <dt>Source reliability</dt><dd>{{ case.confidence.source_reliability }}</dd>
        <dt>Information credibility</dt><dd>{{ case.confidence.information_credibility }}</dd>
      </dl>
    </div>

    <div class="card">
      <h2>Source</h2>
      <dl>
        <dt>Type</dt><dd>{{ case.source_type }}</dd>
        <dt>Reference</dt><dd>{% if case.source_ref %}<a href="{{ case.source_ref }}" target="_blank" rel="noopener">{{ case.source_ref }}</a>{% else %}—{% endif %}</dd>
        <dt>Observed</dt><dd class="muted">{{ case.observed_at.strftime('%Y-%m-%d %H:%M UTC') }}</dd>
        <dt>Ingested</dt><dd class="muted">{{ case.ingested_at.strftime('%Y-%m-%d %H:%M UTC') }}</dd>
      </dl>
    </div>

    <div class="card wide">
      <h2>Observables</h2>
      {% set obs = case.observables %}
      {% if obs.urls %}<h3>URLs</h3><ul>{% for u in obs.urls %}<li><code>{{ u }}</code></li>{% endfor %}</ul>{% endif %}
      {% if obs.domains %}<h3>Domains</h3><ul>{% for d in obs.domains %}<li><code>{{ d }}</code></li>{% endfor %}</ul>{% endif %}
      {% if obs.ips %}<h3>IPs</h3><ul>{% for i in obs.ips %}<li><code>{{ i }}</code></li>{% endfor %}</ul>{% endif %}
      {% if obs.hashes %}<h3>Hashes</h3><ul>{% for h in obs.hashes %}<li><code>{{ h }}</code></li>{% endfor %}</ul>{% endif %}
      {% if not (obs.urls or obs.domains or obs.ips or obs.hashes) %}<p class="muted">no observables</p>{% endif %}
    </div>
  </div>
</section>

{% if sealed_pkg %}
<section class="sealed-package-card" style="margin-top: 20px;">
  <header class="sp-head">
    <h2>SEALED PACKAGE</h2>
    <code class="sp-id">{{ sealed_pkg.package_id }}</code>
  </header>
  <dl class="sp-meta">
    <dt>Created</dt><dd>{{ sealed_pkg.created_at.strftime('%Y-%m-%d %H:%M UTC') }}</dd>
    <dt>Plaintext SHA-256</dt><dd class="sp-hash">{{ sealed_pkg.plaintext_hash }}</dd>
    <dt>Recipients</dt>
    <dd>
      <ul class="sp-recipients">
      {% for r in sealed_pkg.recipients %}
        <li>{{ r }}<span class="sp-size">{{ (sealed_pkg.ciphertext_per_recipient[r] | length) // 4 * 3 }} B</span></li>
      {% endfor %}
      </ul>
    </dd>
  </dl>
</section>
{% endif %}

{% if routes or blocked %}
<section class="panel" style="margin-top: 20px;">
  <div class="panel-head">
    <h1>Routes</h1>
    <span class="count">{{ routes|length }} allowed · {{ blocked|length }} blocked</span>
  </div>
  {% for r in routes %}
  <div class="route-allowed">
    <span class="route-dest">{{ r.destination_name }}</span>
    <span class="route-reason">priority {{ r.priority }} · {{ r.payload_kind }} · max_tlp {{ r.max_tlp_allowed.value }}</span>
  </div>
  {% endfor %}
  {% for b in blocked %}
  <div class="route-blocked">
    <span class="route-dest">{{ b.destination_name }}</span>
    <span class="route-reason">{{ b.reason }}</span>
  </div>
  {% endfor %}
</section>
{% endif %}

{% if ledger %}
<section class="panel" style="margin-top: 20px;">
  <div class="panel-head">
    <h1>Ledger (this case)</h1>
    <span class="count">{{ ledger|length }} entr{{ 'y' if ledger|length == 1 else 'ies' }}</span>
  </div>
  <table class="ledger">
    <thead>
      <tr>
        <th>Timestamp</th>
        <th>Destination</th>
        <th>Payload hash</th>
        <th>TLP</th>
        <th>Outcome</th>
      </tr>
    </thead>
    <tbody>
    {% for e in ledger %}
      <tr class="ledger-row{% if e.outcome.value == 'rejected' %} is-rejected{% elif e.outcome.value == 'actioned' %} is-actioned{% endif %}">
        <td class="lg-ts">{{ e.timestamp.strftime('%H:%M:%S') }}</td>
        <td class="lg-dest">{{ e.destination }}</td>
        <td class="lg-hash">{{ e.payload_hash or '—' }}</td>
        <td><span class="tlp-badge tlp-{{ e.tlp.value }}">{{ e.tlp.value }}</span></td>
        <td><span class="outcome-badge outcome-{{ e.outcome.value }}">{{ e.outcome.value }}</span></td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
</section>
{% endif %}
{% endblock %}
