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

<?= $this->section('email_content') ?>
<div style="font-size:16px; line-height:1.5;">
    <h2>Admission Decision</h2>

    <?php
    // Normalize: $studentName may be string or array
    $isMulti = isset($studentName) && is_array($studentName) && !empty($studentName);

    // Build a human-friendly label for singular sentences when needed
    $studentLabel = '';
    if ($isMulti) {
        $names = array_map(static fn($n) => trim($n), $studentName);
        if (count($names) === 1) {
            $studentLabel = $names[0];
            $isMulti = false;
        } elseif (count($names) === 2) {
            $studentLabel = $names[0] . ' and ' . $names[1];
        } else {
            $last = array_pop($names);
            $studentLabel = implode(', ', $names) . ', and ' . $last;
        }
    } else {
        $studentLabel = $studentName ?? 'your student';
    }
    ?>

    <p>Dear <?= esc($parentName ?? 'Parent') ?>,</p>

    <?php if ($isMulti): ?>
        <p>Thank you for your interest in our school and for applying for admission for the following <?= count($studentName) ?> students:</p>
        <ul style="padding-left:20px;">
            <?php foreach ($studentName as $name): ?>
                <li><?= esc($name) ?></li>
            <?php endforeach; ?>
        </ul>
        <p>After careful consideration, we regret to inform you that we are unable to grant admission at this time.</p>
    <?php else: ?>
        <p>Thank you for your interest in our school and for applying for <?= esc($studentLabel) ?>'s admission.</p>
        <p>After careful consideration, we regret to inform you that we are unable to grant admission at this time.</p>
    <?php endif; ?>

    <?php if (!empty($students) && is_array($students)): ?>
        <?php
        // If reasons were provided by the handler, show them
        $hasReasons = array_reduce($students, fn($c, $s) => $c || !empty($s['reason']), false);
        ?>
        <?php if ($hasReasons): ?>
            <p>Where available, we’ve included brief context for your reference:</p>
            <ul style="padding-left:20px;">
                <?php foreach ($students as $s): ?>
                    <li>
                        <strong><?= esc($s['name'] ?? '') ?>:</strong>
                        <?= esc($s['reason'] ?? 'No additional details provided.') ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    <?php endif; ?>

    <p>This decision was made after a thorough review of all applications received, and we acknowledge that it may be disappointing.</p>
    <p>We encourage you to consider applying again in the future as circumstances may change.</p>
    <p>If you would like feedback on your application, please contact us at <a href="mailto:alrahma.isgl@gmail.com">alrahma.isgl@gmail.com</a>.</p>

    <p>Best regards,<br>Admissions&nbsp;Committee</p>
</div>
<?= $this->endSection() ?>
