{% extends "base.html" %}
{% block content %}
<h1>Memory</h1>
<h2 class="section-label">By type</h2>
<div class="metric-grid">
{% for type_, n in type_counts.items() %}
<div class="metric-card"><span class="n">{{ n }}</span><span class="label">{{ type_ }}</span></div>
{% else %}
<p class="empty">No memory records yet.</p>
{% endfor %}
</div>
{% if type_counts %}
<div id="chart-memory-types" class="chart-section"></div>
<script id="memory-type-counts-data" type="application/json">{{ type_counts | tojson }}</script>
<script>
(function () {
  var counts = JSON.parse(document.getElementById("memory-type-counts-data").textContent);
  var items = Object.keys(counts).map(function (type_) {
    return { label: type_, value: counts[type_] };
  });
  ACRCharts.barChart(document.getElementById("chart-memory-types"), items);
})();
</script>
{% endif %}
<h2 class="section-label">By status</h2>
<div class="metric-grid">
{% for status, n in status_counts.items() %}
<div class="metric-card {{ status | pill_class }}"><span class="n">{{ n }}</span><span class="label">{{ status }}</span></div>
{% endfor %}
</div>

<h2 class="section-label">Confidence calibration</h2>
<p class="lede">Does a memory's stored confidence actually predict how often it turns out useful? A fixed-bin reliability curve over records with at least one recorded use (<code>successful_uses + failed_uses</code>) -- {{ calibration.records_considered }} record(s) considered, {{ calibration.records_excluded_no_evidence }} excluded for having no recorded outcome yet.{% if calibration.brier_score is not none %} Brier score: <code>{{ "%.3f" | format(calibration.brier_score) }}</code> (lower is better-calibrated).{% endif %}</p>
<div id="chart-calibration"></div>
<script>
(function () {
  var bins = [
    {% for b in calibration.bins %}
    { label: "{{ "%.1f" | format(b.lower) }}-{{ "%.1f" | format(b.upper) }} (n={{ b.count }})",
      a: {{ b.mean_confidence }}, b: {{ b.empirical_success_rate }} },
    {% endfor %}
  ];
  ACRCharts.pairedBarChart(document.getElementById("chart-calibration"), bins, {
    aLabel: "predicted confidence", bLabel: "actual success rate", max: 1,
    emptyMessage: "Not enough recorded outcomes yet to compute calibration.",
  });
})();
</script>

<h2 class="section-label">Recently updated</h2>
<div class="table-wrap">
<table>
<tr><th>Subject</th><th>Type</th><th>Status</th><th>Scope</th><th class="num">Confidence</th><th class="num">Utility</th><th>Updated</th></tr>
{% for m in records %}
<tr>
  <td>{{ m.subject }}</td>
  <td>{{ m.type.value }}</td>
  <td><span class="pill {{ m.status.value | pill_class }}">{{ m.status.value }}</span></td>
  <td>{{ m.scope.value }}</td>
  <td class="num">{{ "%.2f" | format(m.confidence) }}</td>
  <td class="num">{{ "%.2f" | format(m.utility_score) }}</td>
  <td>{{ m.updated_at }}</td>
</tr>
{% else %}
<tr><td colspan="7" class="empty">No memory records yet.</td></tr>
{% endfor %}
</table>
</div>
{% endblock %}
