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

<?= $this->section('content') ?>
<div style="font-size:16px; line-height:1.5;">

    <?php
    // Normalize $studentName into an array of names (accept array or a label like "A, B, and C")
    $names = [];
    if (isset($studentName)) {
        if (is_array($studentName)) {
            $names = array_values(array_filter(
                array_map(static fn($n) => trim((string)$n), $studentName),
                static fn($n) => $n !== ''
            ));
        } else {
            $s = trim((string)$studentName);
            if ($s !== '') {
                $parts = preg_split('/\s*,\s*|\s*,?\s+and\s+/i', $s);
                if ($parts !== false) {
                    $names = array_values(array_filter(array_map('trim', $parts), static fn($x) => $x !== ''));
                }
            }
        }
    }
    $count = count($names);

    // Non-possessive label: "A", "A and B", "A, B, and C"
    if ($count === 0) {
        $label = 'your student';
    } elseif ($count === 1) {
        $label = $names[0];
    } elseif ($count === 2) {
        $label = "{$names[0]} and {$names[1]}";
    } else {
        $label = implode(', ', array_slice($names, 0, -1)) . ', and ' . $names[$count - 1];
    }

    // Possessive label with possessive only on the last item: "A and B’s", "A, B, and C’s"
    $labelPoss = ($count === 0)
        ? 'your student&#8217;s'
        : ($count === 1
            ? $names[0] . '&#8217;s'
            : $label . '&#8217;s');

    $verb = ($count > 1) ? 'have' : 'has';
    ?>

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

    <?php if ($count > 1): ?>
        <p>
            Congratulations! We are pleased to inform you that the following students have been accepted
            for admission to our school:
        </p>

        <!-- Bullet list via table for email-client reliability -->
        <!-- left indent spacer -->

        <td>
            <table role="presentation" cellpadding="0" cellspacing="0" style="border-collapse:collapse; padding-left:0;">
                <?php foreach ($names as $n): ?>
                    <tr>
                        <td style="width:24px; font-size:0; line-height:0;">&nbsp;</td>
                        <td style="font-size:16px; line-height:1.5; padding:0 8px 2px 0; vertical-align:top;">&bull;</td>
                        <td style="font-size:16px; line-height:1.5; padding:0 0 2px 0; vertical-align:top;"><?= esc($n) ?></td>
                    </tr>
                <?php endforeach; ?>
            </table>
            <p>
                We are excited to welcome your family to our community and look forward to supporting
                your children's growth in both faith and learning.
            </p>
        <?php elseif ($count === 1): ?>
            <p>
                Congratulations! We are pleased to inform you that <?= esc($names[0]) ?> <?= $verb ?> been
                accepted for admission to our school.
            </p>
            <p>
                We are excited to welcome your family to our community and look forward to supporting
                your child's growth in both faith and learning.
            </p>
        <?php endif; ?>

        <p>
            <strong>Next steps:</strong> Please log in to the parent portal and visit the <em>Invoice</em> tab
            to review your invoice details. The balance must be paid in person on the first day of school
            to complete the enrollment process.
        </p>

        <p style="margin:16px 0;">
            <a href="<?= base_url() ?>" target="_blank" rel="noopener"
                style="display:inline-block; padding:10px 16px; text-decoration:none; border-radius:6px;
            background:#198754; color:#ffffff; font-weight:600; font-size:16px;">
                Access My Account
            </a>
        </p>

        <p>Best regards,<br>Admissions Committee</p>

</div>
<?= $this->endSection() ?>