<?= $this->extend('layout/management_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']) ?> → <?= esc($row['week_end']) ?>
      </div>
    </div>
    <a href="<?= base_url('admin/progress') ?>" class="btn btn-outline-secondary">Back</a>
  </div>

  <?php
    $flagLabels = [
      'homework_assigned' => 'Homework assigned',
      'quiz_done' => 'Quiz / check-in completed',
      'materials_missing' => 'Materials needed',
    ];
    $weeklyReports = $weeklyReports ?? [];
    $subjectSections = $subjectSections ?? [];
    $reportsBySubject = [];
    $attachments = [];
    foreach ($weeklyReports as $report) {
      $reportsBySubject[$report['subject']] = $report;
      foreach (($report['attachments'] ?? []) as $attachment) {
        $attachments[] = [
          'subject' => $report['subject'] ?? 'Attachment',
          'attachment' => $attachment,
          'report_id' => $report['id'] ?? null,
        ];
      }
    }
  ?>

  <div class="row g-3">
    <div class="col-lg-8">
      <?php foreach ($subjectSections as $slug => $section): ?>
        <?php
          $subjectName = $section['db_subject'] ?? $section['label'] ?? $slug;
          $isQuran = $subjectName === 'Quran/Arabic';
          $homeworkLabel = $isQuran ? 'Arabic Practice / Homework' : 'Assigned Homework';
          $report = $reportsBySubject[$subjectName] ?? null;
        ?>
        <div class="card shadow-sm mb-3">
          <div class="card-header bg-white d-flex align-items-center justify-content-between">
            <strong class="mb-0"><?= 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">
                <?= view('admin/partials/class_progress_unit_display', [
                  'unitTitle' => (string) ($report['unit_title'] ?? ''),
                  'isQuran' => $isQuran,
                  'compact' => false,
                ]) ?>
              </div>
              <?php if (!empty($report['materials'])): ?>
                <div class="mb-2"><strong>Materials:</strong> <?= esc($report['materials']) ?></div>
              <?php endif; ?>
              <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('admin/progress/attachment/' . $linkId)
                          : base_url('admin/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>
      <?php endforeach; ?>
    </div>

    <div class="col-lg-4">
      <div class="card shadow-sm mb-3">
        <div class="card-header bg-white"><strong>Summary</strong></div>
        <div class="card-body">
          <div class="mb-2"><strong>Teacher:</strong> <?= esc(trim($row['teacher_name'] ?? '')) ?: '-' ?></div>
          <div class="mb-2"><strong>Submitted:</strong> <?= esc($row['created_at'] ? date('M d, Y g:i A', strtotime($row['created_at'])) : '-') ?></div>
          <div><strong>Status:</strong> <?= esc($row['status_label'] ?? 'Unknown') ?></div>
        </div>
      </div>

      <?php if (!empty($row['flags'])): ?>
        <div class="card shadow-sm mb-3">
          <div class="card-header bg-white"><strong>Checklist</strong></div>
          <div class="card-body">
            <div class="d-flex flex-wrap gap-2">
              <?php foreach ($row['flags'] as $flag): ?>
                <span class="badge bg-light text-dark border"><?= esc($flagLabels[$flag] ?? str_replace('_', ' ', $flag)) ?></span>
              <?php endforeach; ?>
            </div>
          </div>
        </div>
      <?php endif; ?>

      <?php if (!empty($attachments)): ?>
        <div class="card shadow-sm">
          <div class="card-header bg-white"><strong>Attachments</strong></div>
          <div class="card-body">
            <?php foreach ($attachments as $entry): ?>
              <?php
                $attachment = $entry['attachment'] ?? [];
                $linkId = $attachment['id'] ?? ($entry['report_id'] ?? null);
                if (!$linkId) {
                  continue;
                }
                $link = !empty($attachment['legacy'])
                  ? base_url('admin/progress/attachment/' . $linkId)
                  : base_url('admin/progress/attachment-file/' . $linkId);
                $label = trim(($entry['subject'] ?? 'Attachment') . ' • ' . ($attachment['name'] ?? 'Attachment'));
              ?>
              <a class="btn btn-outline-primary w-100 text-start mb-2" href="<?= $link ?>" target="_blank" rel="noreferrer">
                <?= esc($label) ?>
              </a>
            <?php endforeach; ?>
          </div>
        </div>
      <?php endif; ?>
    </div>
  </div>
</div>
<?= $this->endSection() ?>
