{% extends "base.html" %}

{% block content %}
  <section class="section">
    <div class="container">
      <h1 class="title is-4">Behavioral Signals</h1>
      <p class="subtitle is-6">Presence is only one slice. This page exposes the broader behavioral event surface used for timing, read, typing, and response analysis.</p>
      <form method="post" class="box">
        {% csrf_token %}
        <div class="columns is-multiline">
          <div class="column is-3"><label class="checkbox"><input type="checkbox" name="enabled" {% if settings_row.enabled %}checked{% endif %}> Enabled</label></div>
          <div class="column is-3"><label class="checkbox"><input type="checkbox" name="show_in_chat" {% if settings_row.show_in_chat %}checked{% endif %}> Show In Chat</label></div>
          <div class="column is-3"><label class="checkbox"><input type="checkbox" name="show_in_groups" {% if settings_row.show_in_groups %}checked{% endif %}> Show In Groups</label></div>
          <div class="column is-3"><label class="checkbox"><input type="checkbox" name="inference_enabled" {% if settings_row.inference_enabled %}checked{% endif %}> Inference Enabled</label></div>
          <div class="column is-3">
            <label class="label is-size-7">Retention Days</label>
            <input class="input is-small" type="number" min="1" name="retention_days" value="{{ settings_row.retention_days }}">
          </div>
          <div class="column is-3">
            <label class="label is-size-7">Fade Threshold (seconds)</label>
            <input class="input is-small" type="number" min="30" name="fade_threshold_seconds" value="{{ settings_row.fade_threshold_seconds }}">
          </div>
        </div>
        <button class="button is-link is-small" type="submit">Save</button>
      </form>

      <div class="box">
        <div class="level mb-3">
          <div class="level-left">
            <div>
              <h2 class="title is-6 mb-1">Behavioral Event Statistics</h2>
              <p class="help is-size-7">Primary source is `gia_events` in Manticore. When unavailable, this page falls back to Django `ConversationEvent` shadow rows.</p>
            </div>
          </div>
          <div class="level-right">
            <span class="tag is-light">Source: {{ behavioral_stats_source }}</span>
          </div>
        </div>

        <div class="tags mb-4">
          <span class="tag is-info is-light">Contacts: {{ behavioral_totals.contacts }}</span>
          <span class="tag is-light">Events: {{ behavioral_totals.total_events }}</span>
          <span class="tag is-light">Presence: {{ behavioral_totals.presence_events }}</span>
          <span class="tag is-light">Read: {{ behavioral_totals.read_events }}</span>
          <span class="tag is-light">Typing: {{ behavioral_totals.typing_events }}</span>
          <span class="tag is-light">Messages: {{ behavioral_totals.message_events }}</span>
          <span class="tag is-light">Abandoned: {{ behavioral_totals.abandoned_events }}</span>
        </div>

        <h3 class="title is-7">By Transport</h3>
        <table class="table is-fullwidth is-striped is-size-7 mb-5">
          <thead>
            <tr>
              <th>Service</th>
              <th>Contacts</th>
              <th>Total</th>
              <th>Presence</th>
              <th>Read</th>
              <th>Typing</th>
              <th>Messages</th>
              <th>Abandoned</th>
              <th>Last Event TS</th>
            </tr>
          </thead>
          <tbody>
            {% for row in transport_stats %}
              <tr>
                <td>{{ row.service }}</td>
                <td>{{ row.contacts }}</td>
                <td>{{ row.total_events }}</td>
                <td>{{ row.presence_events }}</td>
                <td>{{ row.read_events }}</td>
                <td>{{ row.typing_events }}</td>
                <td>{{ row.message_events }}</td>
                <td>{{ row.abandoned_events }}</td>
                <td>{{ row.last_event_ts }}</td>
              </tr>
            {% empty %}
              <tr><td colspan="9">No behavioral transport summaries available.</td></tr>
            {% endfor %}
          </tbody>
        </table>

        <h3 class="title is-7">By Contact</h3>
        <table class="table is-fullwidth is-striped is-size-7">
          <thead>
            <tr>
              <th>Contact</th>
              <th>Service</th>
              <th>Total</th>
              <th>Presence</th>
              <th>Read</th>
              <th>Typing</th>
              <th>Messages</th>
              <th>Abandoned</th>
              <th>Last Event TS</th>
            </tr>
          </thead>
          <tbody>
            {% for row in behavioral_stats %}
              <tr>
                <td>{{ row.person_name }}</td>
                <td>{{ row.service }}</td>
                <td>{{ row.total_events }}</td>
                <td>{{ row.presence_events }}</td>
                <td>{{ row.read_events }}</td>
                <td>{{ row.typing_events }}</td>
                <td>{{ row.message_events }}</td>
                <td>{{ row.abandoned_events }}</td>
                <td>{{ row.last_event_ts }}</td>
              </tr>
            {% empty %}
              <tr><td colspan="9">No behavioral events found in either Manticore or ConversationEvent shadow rows.</td></tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    </div>
  </section>
{% endblock %}
