{% extends "base.html" %}
{% block title %}psyc — operations cockpit{% endblock %}
{% block content %}

<section class="hero">
  <div class="hero-text">
    <h1 class="hero-title">Defensive CTI in motion</h1>
    <p class="hero-sub">What psyc has seen and done — at a glance.</p>
  </div>
  <a class="hero-cta" href="/cases">All cases →</a>
</section>

<section class="kpis">
  <a class="kpi" href="/cases">
    <div class="kpi-num">{{ kpis.cases }}</div>
    <div class="kpi-label">cases tracked</div>
  </a>
  <a class="kpi" href="/lookup">
    <div class="kpi-num">{{ kpis.iocs }}</div>
    <div class="kpi-label">IOCs indexed</div>
  </a>
  <div class="kpi">
    <div class="kpi-num">+{{ kpis.new_24h }}</div>
    <div class="kpi-label">new in 24 h</div>
  </div>
  <div class="kpi kpi-warn">
    <div class="kpi-num">{{ kpis.high_total }}</div>
    <div class="kpi-label">high / critical</div>
  </div>
  <a class="kpi kpi-accent" href="/response">
    <div class="kpi-num">⚡ {{ kpis.enforcements_24h }}</div>
    <div class="kpi-label">enforced 24 h</div>
  </a>
  <a class="kpi" href="/ledger">
    <div class="kpi-num">{{ kpis.ledger_total }}</div>
    <div class="kpi-label">ledger entries</div>
  </a>
</section>

{% if featured %}
<section class="featured-section">
  <header class="featured-section-head sev-{{ featured.classification.severity.value }}">
    <div class="featured-section-title">
      <span class="featured-bracket">⌖</span>
      <span class="featured-section-label">Featured Threat</span>
      <span class="featured-section-pulse"></span>
    </div>
    <div class="featured-section-meta">
      <span class="sev-badge">{{ featured.classification.severity.value if featured.classification.severity else '—' }}</span>
      <span class="tlp-badge tlp-{{ featured.classification.tlp.value }}">{{ featured.classification.tlp.value }}</span>
      <span class="muted">{{ featured.source_metadata.feed or 'unknown' }} · {{ featured.ingested_at.strftime('%Y-%m-%d %H:%M') }}</span>
    </div>
  </header>
  <a class="featured sev-{{ featured.classification.severity.value }}" href="/cases/{{ featured.case_id }}">
    <div class="featured-hero">
      <div class="featured-grid" aria-hidden="true"></div>
      <div class="featured-particles" aria-hidden="true">{{ featured_hero|safe }}</div>
      <svg class="featured-radar" viewBox="0 0 100 100" aria-hidden="true">
        <defs>
          <linearGradient id="radar-sweep" x1="0" y1="50" x2="50" y2="50" gradientUnits="userSpaceOnUse">
            <stop offset="0%" stop-color="currentColor" stop-opacity="0"/>
            <stop offset="100%" stop-color="currentColor" stop-opacity="0.55"/>
          </linearGradient>
        </defs>
        <circle cx="50" cy="50" r="46" fill="none" stroke="currentColor" stroke-opacity="0.35" stroke-width="0.6"/>
        <circle cx="50" cy="50" r="32" fill="none" stroke="currentColor" stroke-opacity="0.25" stroke-width="0.5"/>
        <circle cx="50" cy="50" r="18" fill="none" stroke="currentColor" stroke-opacity="0.18" stroke-width="0.4"/>
        <line x1="50" y1="4" x2="50" y2="96" stroke="currentColor" stroke-opacity="0.10" stroke-width="0.4"/>
        <line x1="4" y1="50" x2="96" y2="50" stroke="currentColor" stroke-opacity="0.10" stroke-width="0.4"/>
        <g class="radar-sweep-arm">
          <path d="M50,50 L50,4 A46,46 0 0,1 96,50 Z" fill="url(#radar-sweep)"/>
        </g>
        <circle cx="50" cy="50" r="2.5" fill="currentColor" fill-opacity="0.85"/>
      </svg>
      <div class="featured-overlay">
        <h3 class="featured-title">{{ featured.summary }}</h3>
        <div class="featured-stats">
          {% if featured.classification.incident_type %}
          <span class="stat-chip stat-incident">⌧ {{ featured.classification.incident_type.value }}</span>
          {% endif %}
          {% set obs = featured.observables %}
          {% set total_iocs = (obs.urls|length) + (obs.domains|length) + (obs.ips|length) + (obs.hashes|length) + (obs.cves|length) %}
          {% if total_iocs %}
          <span class="stat-chip stat-iocs" title="URLs/Domains/IPs/Hashes/CVEs">⛛ {{ total_iocs }} IOCs · {{ obs.urls|length }}U/{{ obs.domains|length }}D/{{ obs.ips|length }}I/{{ obs.hashes|length }}H/{{ obs.cves|length }}C</span>
          {% endif %}
          {% if featured.victim.country %}
          <span class="stat-chip stat-country">📍 {{ featured.victim.country }}</span>
          {% endif %}
          {% if featured.confidence and featured.confidence.level %}
          <span class="stat-chip stat-confidence">⊞ {{ featured.confidence.level }} confidence</span>
          {% endif %}
          {% if featured.source_metadata.malware %}
          <span class="stat-chip stat-family">⌬ {{ featured.source_metadata.malware }}</span>
          {% endif %}
        </div>
        <div class="featured-cta">Open case <span class="featured-arrow">→</span></div>
      </div>
    </div>
  </a>
</section>
{% endif %}

<div class="home-grid">
  <section class="panel home-news">
    <div class="panel-head">
      <h2>Recent activity</h2>
      <span class="count">{{ total_items }} items</span>
    </div>
    <p class="page-intro">Live feed of what psyc has detected and what it has done about it.</p>

    {% if not buckets %}
      <p class="empty">Nothing recent yet — start with <code>psyc fetch-all</code>.</p>
    {% endif %}

    {% for b in buckets %}
      <h3 class="bucket-head">{{ b.label }} <span class="bucket-count">{{ b.items|length }}</span></h3>
      <ol class="news-list">
        {% for i in b.items %}
        {% set has_link = i.case_id %}
        <li class="news-item news-kind-{{ i.kind }}{% if i.severity %} sev-{{ i.severity }}{% endif %}{% if has_link %} is-link{% endif %}">
          {% if has_link %}<a class="news-cardlink" href="/cases/{{ i.case_id }}" aria-label="Open {{ i.case_id }}"></a>{% endif %}
          <div class="news-icon">
            {% if i.kind == 'case' and i.case_id and case_glyphs.get(i.case_id) %}
              {{ case_glyphs[i.case_id]|safe }}
            {% else %}
              <span class="news-icon-glyph">{{ i.icon }}</span>
            {% endif %}
          </div>
          <div class="news-body">
            <div class="news-head">
              <span class="news-headline">{{ i.headline }}</span>
              {% if i.severity %}<span class="sev-badge">{{ i.severity }}</span>{% endif %}
            </div>
            <div class="news-sub">{{ i.body }}</div>
            <div class="news-meta">
              <time>{{ i.timestamp.strftime('%H:%M' if b.label == 'Today' else '%Y-%m-%d %H:%M') }}</time>
              {% if i.case_id %} · <span class="news-case-id">{{ i.case_id }}</span>{% endif %}
            </div>
          </div>
          {% if has_link %}<span class="news-arrow" aria-hidden="true">→</span>{% endif %}
        </li>
        {% endfor %}
      </ol>
    {% endfor %}
  </section>

  <aside class="panel home-side">
    <div class="panel-head"><h2>Feed health</h2></div>
    <p class="page-intro">Where psyc's data is coming from.</p>
    <ul class="feed-health">
      {% for f in feeds %}
      <li class="feed-row">
        <span class="feed-name">{{ f.feed }}</span>
        <span class="feed-count">{{ f.count }}</span>
        <span class="feed-time">{{ f.latest.strftime('%m-%d %H:%M') if f.latest else '—' }}</span>
      </li>
      {% endfor %}
    </ul>
  </aside>
</div>
{% endblock %}
