<?= $this->extend('layout/email_layout') ?>

<?= $this->section('content') ?>
<div style="font-size:16px; font-family:Arial, Helvetica, sans-serif; color:#333;">
  <p style="margin:0 0 12px 0;line-height:1.5;">
    Dear <?= esc($parent_name ?? 'Parent/Guardian') ?>,
  </p>

  <p style="margin:0 0 12px 0;line-height:1.5;">
    We are writing to share the school's decision regarding
    <strong><?= esc($student_name ?? 'your student') ?></strong>
    <?php if (!empty($class_section_name)): ?>(<?= esc($class_section_name) ?>)<?php endif; ?>
    for the <?= esc(trim(($school_year ?? ''))) ?> school year.
  </p>

  <?php if (!empty($decision)): ?>
  <table style="width:100%; border-collapse:collapse; margin:0 0 16px; font-size:15px;">
    <tr>
      <td style="border:1px solid #ddd; padding:10px; font-weight:bold; background:#f8f9fa; width:35%;">Decision</td>
      <td style="border:1px solid #ddd; padding:10px; font-weight:bold; color:#2c5282;"><?= esc($decision) ?></td>
    </tr>
  </table>
  <?php endif; ?>

  <?php if (!empty($notes)): ?>
  <p style="margin:0 0 6px 0;line-height:1.5;"><strong>Comments:</strong></p>
  <p style="margin:0 0 16px 0;line-height:1.6;background:#f8f9fa;padding:10px 14px;border-left:4px solid #4a90d9;">
    <?= nl2br(esc($notes)) ?>
  </p>
  <?php endif; ?>

  <?php
  $scoreLabels = [
    'homework_avg'        => 'Homework Avg',
    'project_avg'         => 'Project Avg',
    'participation_score' => 'Participation',
    'test_avg'            => 'Test Avg',
    'ptap_score'          => 'PTAP Score',
    'attendance_score'    => 'Attendance',
    'midterm_exam_score'  => 'Midterm Score',
    'final_exam_score'    => 'Final Exam',
    'semester_score'      => 'Semester Score',
  ];

  $allSemesters = is_array($all_semesters ?? null) ? $all_semesters : [];
  $hasSemesters = !empty($allSemesters);
  ?>

  <?php if ($hasSemesters): ?>
  <p style="margin:0 0 8px 0;line-height:1.5;"><strong>Score Summary — <?= esc($school_year ?? '') ?></strong></p>

  <?php foreach ($allSemesters as $sem): ?>
    <?php
      $semLabel = (string)($sem['semester'] ?? '');
      $hasAnyScore = false;
      foreach ($scoreLabels as $key => $_) {
          $v = $sem[$key] ?? null;
          if ($v !== null && $v !== '') { $hasAnyScore = true; break; }
      }
    ?>
    <p style="margin:0 0 4px 0;font-weight:bold;font-size:14px;"><?= esc($semLabel) ?> Semester</p>
    <table style="width:100%; border-collapse:collapse; margin:0 0 12px; font-size:14px;">
      <thead>
        <tr style="background:#f8f9fa;">
          <th style="border:1px solid #ddd; padding:6px; text-align:left;">Item</th>
          <th style="border:1px solid #ddd; padding:6px; text-align:left;">Score</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($scoreLabels as $key => $label): ?>
          <?php $val = $sem[$key] ?? null; if ($val === null || $val === '') continue; ?>
          <tr>
            <td style="border:1px solid #ddd; padding:6px;"><?= esc($label) ?></td>
            <td style="border:1px solid #ddd; padding:6px;<?= $key === 'semester_score' ? ' font-weight:bold;' : '' ?>">
              <?= esc(number_format((float)$val, 2, '.', '')) ?>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (!$hasAnyScore): ?>
          <tr><td colspan="2" style="border:1px solid #ddd; padding:6px; color:#999;">No scores recorded.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
    <?php
      $semComments = is_array($sem['comments'] ?? null) ? $sem['comments'] : [];
      $commentTypeLabels = [
          'general'            => 'General',
          'attendance'         => 'Attendance',
          'attendance_comment' => 'Attendance',
          'midterm'            => 'Midterm',
          'final'              => 'Final Exam',
          'ptap'               => 'PTAP',
      ];
      // Deduplicate by label+text
      $seenCmt = [];
      $dedupedCmt = [];
      foreach ($semComments as $ctype => $ctext) {
          $ctext = trim((string)$ctext);
          if ($ctext === '') continue;
          $clabel = $commentTypeLabels[$ctype] ?? ucfirst($ctype);
          $key = $clabel . '|' . $ctext;
          if (!isset($seenCmt[$key])) { $seenCmt[$key] = true; $dedupedCmt[] = [$clabel, $ctext]; }
      }
    ?>
    <?php if (!empty($dedupedCmt)): ?>
    <p style="margin:0 0 4px 0;font-size:13px;font-weight:bold;">Comments</p>
    <?php foreach ($dedupedCmt as [$clabel, $ctext]): ?>
    <p style="margin:0 0 8px 0;font-size:13px;color:#444;line-height:1.5;background:#f8f9fa;padding:6px 10px;border-left:3px solid #aaa;">
      <strong><?= esc($clabel) ?>:</strong> <?= nl2br(esc($ctext)) ?>
    </p>
    <?php endforeach; ?>
    <?php endif; ?>
  <?php endforeach; ?>

  <?php else: ?>
  <?php
    // Fallback: single semester scores block
    $scores = is_array($scores ?? null) ? $scores : [];
    $hasScores = (bool) array_filter($scores, static fn($v) => $v !== null && $v !== '');
  ?>
  <?php if ($hasScores): ?>
  <p style="margin:0 0 6px 0;line-height:1.5;"><strong>Score Summary:</strong></p>
  <table style="width:100%; border-collapse:collapse; margin:0 0 16px; font-size:14px;">
    <thead>
      <tr style="background:#f8f9fa;">
        <th style="border:1px solid #ddd; padding:6px; text-align:left;">Item</th>
        <th style="border:1px solid #ddd; padding:6px; text-align:left;">Score</th>
      </tr>
    </thead>
    <tbody>
      <?php foreach ($scoreLabels as $key => $label): ?>
        <?php $val = $scores[$key] ?? null; if ($val === null || $val === '') continue; ?>
        <tr>
          <td style="border:1px solid #ddd; padding:6px;"><?= esc($label) ?></td>
          <td style="border:1px solid #ddd; padding:6px;"><?= esc(number_format((float)$val, 2, '.', '')) ?></td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
  <?php endif; ?>
  <?php endif; ?>

  <p style="margin:0 0 12px 0;line-height:1.5;">
    Please do not hesitate to contact the school if you have any questions or concerns.
  </p>

  <p style="margin:0;line-height:1.5;">
    Thank you,<br>
    Al Rahma Sunday School
  </p>
</div>
<?= $this->endSection() ?>
