<?= $this->extend('layout/main_layout') ?>
<?= $this->section('content') ?>
<div class="container py-4">
  <div class="d-flex align-items-center justify-content-between mb-3">
    <div>
      <h3 class="mb-0">Progress Report</h3>
      <div class="text-muted">
        <?= esc($row['class_section_name'] ?? '-') ?> • <?= esc($row['week_start']) ?>
      </div>
    </div>
    <a href="<?= base_url('teacher/progress/history') ?>" class="btn btn-outline-secondary">Back</a>
  </div>

  <div class="row g-4">
    <?php
      $weeklyReports = $weeklyReports ?? [];
      $subjectSections = $subjectSections ?? [];
      $reportsBySubject = [];
      foreach ($weeklyReports as $report) {
        $reportsBySubject[$report['subject']] = $report;
      }
    ?>
    <?php foreach ($subjectSections as $slug => $section): ?>
      <?php
        $subjectName = $section['db_subject'] ?? $section['label'] ?? $slug;
        $isQuran = $subjectName === 'Quran/Arabic';
        $unitLabel = $isQuran ? 'Surah / Custom Arabic' : 'Unit / Chapter';
        $homeworkLabel = $isQuran ? 'Arabic Practice / Homework' : 'Assigned Homework';
        $report = $reportsBySubject[$subjectName] ?? null;
      ?>
      <div class="col-md-6">
        <div class="card shadow-sm mb-3 h-100">
          <div class="card-header bg-white">
            <strong><?= esc($section['label'] ?? $subjectName) ?></strong>
          </div>
          <div class="card-body">
            <?php if (! $report): ?>
              <div class="text-muted">No entry submitted for this subject this week.</div>
            <?php else: ?>
              <div class="mb-2"><strong>Submitted by:</strong> <?= esc(trim((string) ($report['teacher_name'] ?? '')) ?: 'Unknown') ?></div>
              <div class="mb-2"><strong><?= $unitLabel ?>:</strong> <?= esc($report['unit_title'] ?: '-') ?></div>
              <div class="mb-3"><strong>Activities</strong><div class="mt-1"><?= nl2br(esc($report['covered'] ?? '-')) ?></div></div>
              <div class="mb-3">
                <strong><?= $homeworkLabel ?>:</strong>
                <div class="mt-1"><?= nl2br(esc($report['homework'] ?: '-')) ?></div>
              </div>
              <?php if (!empty($report['attachments'])): ?>
                <div class="mt-3">
                  <strong>Attachments:</strong>
                  <div class="d-flex flex-column gap-2 mt-2">
                    <?php foreach ($report['attachments'] as $attachment): ?>
                      <?php
                        $linkId = $attachment['id'] ?? $report['id'];
                        $link = !empty($attachment['legacy'])
                          ? base_url('teacher/progress/attachment/' . $linkId)
                          : base_url('teacher/progress/attachment-file/' . $linkId);
                      ?>
                      <a class="btn btn-sm btn-outline-secondary text-start" href="<?= $link ?>" target="_blank" rel="noreferrer">
                        <?= esc($attachment['name'] ?? 'Attachment') ?>
                      </a>
                    <?php endforeach; ?>
                  </div>
                </div>
              <?php endif; ?>
            <?php endif; ?>
          </div>
        </div>
      </div>
    <?php endforeach; ?>
  </div>
</div>
<?= $this->endSection() ?>
