{% extends 'base.html' %}

{% block content %}
<section id="body-metrics" class="grid cols-2 evidence-board dossier-grid">
  <article class="card evidence-card">
    <div class="case-legend">
      <span class="section-label">Training floor board</span>
      <span class="pill">Training Dossier</span>
    </div>
    <h2>Current body metrics</h2>
    <div class="grid dossier-grid">
      {% for user in summary.users %}
      <section class="evidence-slab">
        <h3>{{ user.display_name }} {% if user.is_stub %}<span class="badge">stub</span>{% endif %}</h3>
        <p class="metric-value">{{ user.current_weight_lbs if user.current_weight_lbs is not none else '—' }}{% if user.current_weight_lbs is not none %} lb{% endif %}</p>
        <p class="muted">BMI: {{ '%.1f'|format(user.current_bmi) if user.current_bmi is not none else 'Not set yet' }}</p>
        <p class="muted">System: {{ user.measurement_system }}</p>
        {% if user.latest_measurements %}
        <p class="muted">
          Waist {{ user.latest_measurements.waist_inches or '—' }} in ·
          Hips {{ user.latest_measurements.hip_inches or '—' }} in ·
          Chest {{ user.latest_measurements.chest_inches or '—' }} in ·
          Neck {{ user.latest_measurements.neck_inches or '—' }} in
        </p>
        {% else %}
        <p class="muted">No body measurements logged yet.</p>
        {% endif %}
      </section>
      {% endfor %}
    </div>
  </article>

  <article class="card evidence-card">
    <div class="case-legend">
      <span class="section-label">Case priorities</span>
      <span class="pill">Operator notes</span>
    </div>
    <h2>V1 focus</h2>
    <ul class="list case-list">
      <li>Structured workout logging with routines and exercises.</li>
      <li>Body-weight, BMI, waist/hip/chest/neck history in imperial units.</li>
      <li>Recent workouts, volume trends, and simple PR tracking.</li>
    </ul>
  </article>
</section>

<section id="weight-log" class="grid cols-2 dossier-grid">
  <article class="card evidence-board">
    <div class="case-legend"><span class="section-label">Intake docket</span><span class="pill">Weight log</span></div>
    <h2>Log weight</h2>
    <form class="stack-form intake-docket" method="post" action="/log/weight">
      <label>User
        <select name="user_id">
          {% for user in summary.users %}
          <option value="{{ user.id }}" {% if user.id == 'john' %}selected{% endif %}>{{ user.display_name }}</option>
          {% endfor %}
        </select>
      </label>
      <label>Recorded at
        <input type="datetime-local" name="recorded_at" required>
      </label>
      <div class="grid cols-2 compact-grid">
        <label>Weight (lb)
          <input type="number" name="weight_lbs" min="0" step="0.1" required>
        </label>
        <label>Body fat %
          <input type="number" name="body_fat_percent" min="0" max="100" step="0.1">
        </label>
      </div>
      <label>Notes
        <textarea name="notes" rows="2" placeholder="Morning weigh-in, post-workout, etc."></textarea>
      </label>
      <button type="submit">Save weight entry</button>
    </form>
  </article>

  <article class="card evidence-board">
    <div class="case-legend"><span class="section-label">Intake docket</span><span class="pill">Measurements</span></div>
    <h2>Log body measurements</h2>
    <form class="stack-form intake-docket" method="post" action="/log/measurements">
      <input type="hidden" name="user_id" value="john">
      <label>Recorded at
        <input type="datetime-local" name="recorded_at" required>
      </label>
      <div class="grid cols-2 compact-grid">
        <label>Waist (in)
          <input type="number" name="waist_inches" min="0" step="0.1">
        </label>
        <label>Hips (in)
          <input type="number" name="hip_inches" min="0" step="0.1">
        </label>
        <label>Chest (in)
          <input type="number" name="chest_inches" min="0" step="0.1">
        </label>
        <label>Neck (in)
          <input type="number" name="neck_inches" min="0" step="0.1">
        </label>
      </div>
      <label>Notes
        <textarea name="notes" rows="2" placeholder="Flexed, relaxed, baseline, etc."></textarea>
      </label>
      <button type="submit">Save measurement entry</button>
    </form>
  </article>
</section>

<section class="grid cols-2 dossier-grid">
  <article class="card evidence-board">
    <div class="case-legend"><span class="section-label">Exercise library</span><span class="pill">Template intake</span></div>
    <h2>Add exercise template</h2>
    <form class="stack-form intake-docket" method="post" action="/templates">
      <label>Name
        <input type="text" name="name" placeholder="Bench Press" required>
      </label>
      <div class="grid cols-2 compact-grid">
        <label>Primary muscle
          <input type="text" name="primary_muscle" placeholder="Chest">
        </label>
        <label>Equipment
          <input type="text" name="equipment" placeholder="Barbell">
        </label>
      </div>
      <label>Notes
        <textarea name="notes" rows="3" placeholder="Technique cue, setup reminder, variation notes."></textarea>
      </label>
      <button type="submit">Add template</button>
    </form>
  </article>

  <article class="card evidence-board">
    <div class="case-legend"><span class="section-label">Routine intake</span><span class="pill">Draft split</span></div>
    <h2>Build routine draft</h2>
    <form class="stack-form intake-docket" method="post" action="/routines">
      <input type="hidden" name="user_id" value="john">
      <label>Routine name
        <input type="text" name="name" placeholder="Push Day Draft" required>
      </label>
      <label>Exercise lines
        <textarea name="exercise_lines" rows="6" placeholder="Bench Press|4|6-8&#10;Incline Dumbbell Press|3|8-10&#10;Triceps Pushdown|3|12-15" required></textarea>
      </label>
      <p class="hint">Format: exercise | target sets | target reps | optional notes</p>
      <label>Notes
        <textarea name="notes" rows="2" placeholder="Temporary until the real split gets entered."></textarea>
      </label>
      <button type="submit">Save routine draft</button>
    </form>
  </article>
</section>

<section id="recent-workouts" class="grid cols-2 dossier-grid">
  <article class="card evidence-board">
    <div class="case-legend"><span class="section-label">Workout report</span><span class="pill">Quick log</span></div>
    <h2>Quick log workout</h2>
    <form class="stack-form intake-docket" method="post" action="/workouts/quick-log">
      <input type="hidden" name="user_id" value="john">
      <label>Workout name
        <input type="text" name="routine_name" placeholder="Push Day" required>
      </label>
      <label>Performed at
        <input type="datetime-local" name="performed_at" required>
      </label>
      <label>Exercise lines
        <textarea name="exercise_lines" rows="6" placeholder="Bench Press|8@135, 8@145&#10;Incline Dumbbell Press|10@50, 8@55|Last set close to failure" required></textarea>
      </label>
      <p class="hint">Format: exercise | reps@weight, reps@weight | optional notes</p>
      <label>Notes
        <textarea name="notes" rows="2" placeholder="Energy, pain, wins, misses."></textarea>
      </label>
      <button type="submit">Save workout</button>
    </form>
  </article>

  <article id="routine-board" class="card evidence-board">
    <div class="case-legend"><span class="section-label">Current split</span><span class="pill">Routine board</span></div>
    <h2>Current routine board</h2>
    <p class="muted">John's current five-day split is now loaded as the default routine library with a default prescription of 3 sets of 10 reps. Weights are still TBD.</p>
    <ul class="list case-list">
      <li>Exercise templates: {{ summary.counts.exercise_templates }}</li>
      <li>Routines: {{ summary.counts.routines }}</li>
    </ul>
    {% if summary.routines %}
    <div class="history-block">
      <h3>Loaded routine days</h3>
      <ul class="list">
        {% for routine in summary.routines %}
        <li>
          <strong>{{ routine.name }}</strong>
          <p class="muted">{{ routine.exercises|length }} exercises{% if routine.notes %} · {{ routine.notes }}{% endif %}</p>
        </li>
        {% endfor %}
      </ul>
    </div>
    {% endif %}
    {% if summary.exercise_templates %}
    <div class="history-block">
      <h3>Template snapshot</h3>
      <ul class="list">
        {% for template in summary.exercise_templates %}
        <li>{{ template.name }}{% if template.equipment %} · {{ template.equipment }}{% endif %}</li>
        {% endfor %}
      </ul>
    </div>
    {% endif %}
  </article>
</section>

<section class="grid cols-2 dossier-grid">
  <article class="card evidence-board">
    <div class="case-legend"><span class="section-label">Recent logs</span><span class="pill">Session reports</span></div>
    <h2>Recent workouts</h2>
    {% if john_workout_history %}
    <ul class="list">
      {% for workout in john_workout_history %}
      <li>
        <strong>{{ workout.routine_name }}</strong>
        <p class="muted">{{ workout.performed_at }} · {{ workout.set_count }} sets · {{ workout.total_volume_lbs }} lb volume</p>
      </li>
      {% endfor %}
    </ul>
    {% else %}
    <p class="muted">No workouts logged yet.</p>
    {% endif %}
  </article>

  <article class="card evidence-board">
    <div class="case-legend"><span class="section-label">History snapshot</span><span class="pill">John file</span></div>
    <h2>John history snapshot</h2>
    <div class="history-block">
      <h3>Recent weights</h3>
      {% if john_weight_history %}
      <ul class="list">
        {% for entry in john_weight_history %}
        <li>{{ entry.recorded_at }} · {{ entry.weight_lbs }} lb{% if entry.bmi is not none %} · BMI {{ '%.1f'|format(entry.bmi) }}{% endif %}</li>
        {% endfor %}
      </ul>
      {% else %}
      <p class="muted">No weight entries yet.</p>
      {% endif %}
    </div>
    <div class="history-block">
      <h3>Recent measurements</h3>
      {% if john_measurement_history %}
      <ul class="list">
        {% for entry in john_measurement_history %}
        <li>{{ entry.recorded_at }} · Waist {{ entry.waist_inches or '—' }} · Chest {{ entry.chest_inches or '—' }} · Hips {{ entry.hip_inches or '—' }} · Neck {{ entry.neck_inches or '—' }}</li>
        {% endfor %}
      </ul>
      {% else %}
      <p class="muted">No measurement entries yet.</p>
      {% endif %}
    </div>
  </article>
</section>

<section class="grid cols-2 dossier-grid">
  <article class="card evidence-board">
    <div class="case-legend"><span class="section-label">Current counts</span><span class="pill">Inventory</span></div>
    <h2>Current counts</h2>
    <ul class="list case-list">
      <li>Workout sessions: {{ summary.counts.workout_sessions }}</li>
      <li>Weight entries: {{ summary.counts.weight_entries }}</li>
      <li>Measurement entries: {{ summary.counts.measurement_entries }}</li>
      <li>Exercise templates: {{ summary.counts.exercise_templates }}</li>
      <li>Routines: {{ summary.counts.routines }}</li>
    </ul>
  </article>

  <article class="card evidence-board">
    <div class="case-legend"><span class="section-label">Exercise bests</span><span class="pill">PR board</span></div>
    <h2>Exercise bests</h2>
    {% if john_progression %}
    <ul class="list">
      {% for entry in john_progression %}
      <li>
        <strong>{{ entry.exercise_name }}</strong>
        <p class="muted">{{ entry.sessions }} sessions · max {{ entry.max_weight_lbs }} lb · est 1RM {{ entry.best_set_estimated_1rm }} lb</p>
      </li>
      {% endfor %}
    </ul>
    {% else %}
    <p class="muted">No exercise progression data yet.</p>
    {% endif %}
  </article>
</section>

<section class="grid cols-2 dossier-grid">
  <article class="card evidence-board">
    <div class="case-legend"><span class="section-label">Next likely build</span><span class="pill">Open casework</span></div>
    <h2>Next likely build</h2>
    <ul class="list case-list">
      <li>Replace placeholder routines with John’s real day-by-day program tomorrow.</li>
      <li>Routine-specific workout entry flow driven directly from the saved split.</li>
      <li>Exercise progression and PR views.</li>
    </ul>
  </article>
</section>
{% endblock %}
