<?php
declare(strict_types=1);

/**
 * @var \App\View\AppView $this
 * @var \App\Model\Entity\TeacherApplication $application
 */
$specs = array_filter(array_map('trim', explode(',', (string)($application->specialties ?? ''))));
$submittedAt = $application->created?->format('Y-m-d H:i:s') ?? '—';
$promotedTeacher = $application->teacher ?? null;
?>
<?php $this->assign('title', 'Application · ' . h($application->full_name)); ?>

<section class="cc-admin-page">
    <a class="cc-msg-back" href="<?= $this->Url->build(['_name' => 'admin_applications']) ?>">← Back to applications</a>

    <div class="cc-msg-grid">
        <div class="cc-reveal">
            <div class="cc-msg-meta">
                <span class="cc-badge cc-badge--<?= h((string)$application->status) ?>"><?= h(ucfirst((string)$application->status)) ?></span>
                <span class="cc-chip"><?= h(ucfirst((string)$application->craft)) ?></span>
                <span class="cc-muted" style="font-size: 13px;">Submitted · <?= h($submittedAt) ?> · #<?= sprintf('%04d', (int)$application->id) ?></span>
            </div>
            <div class="cc-msg-title"><?= h($application->full_name) ?></div>

            <div class="cc-card">
                <div class="cc-eyebrow" style="margin-bottom: 18px;">Applicant details</div>
                <div class="cc-msg-kv">
                    <div><div class="cc-msg-kv__k">Email</div><div class="cc-msg-kv__v"><a href="mailto:<?= h($application->email) ?>"><?= h($application->email) ?></a></div></div>
                    <div><div class="cc-msg-kv__k">Phone</div><div class="cc-msg-kv__v"><?= h((string)($application->phone ?: '—')) ?></div></div>
                    <div><div class="cc-msg-kv__k">Craft</div><div class="cc-msg-kv__v"><?= h(ucfirst((string)$application->craft)) ?></div></div>
                    <div><div class="cc-msg-kv__k">Years experience</div><div class="cc-msg-kv__v"><?= $application->years_experience !== null ? (int)$application->years_experience : '—' ?></div></div>
                    <div>
                        <div class="cc-msg-kv__k">Specialties</div>
                        <div class="cc-msg-kv__v">
                            <?php if ($specs): ?>
                                <div class="cc-tag-row">
                                    <?php foreach ($specs as $s): ?><span class="cc-chip"><?= h($s) ?></span><?php endforeach; ?>
                                </div>
                            <?php else: ?>—<?php endif; ?>
                        </div>
                    </div>
                    <div>
                        <div class="cc-msg-kv__k">Portfolio</div>
                        <div class="cc-msg-kv__v">
                            <?php if (!empty($application->portfolio_url)): ?>
                                <a href="<?= h($application->portfolio_url) ?>" target="_blank" rel="noopener"><?= h($application->portfolio_url) ?></a>
                            <?php else: ?>—<?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>

            <div class="cc-msg-body">
                <div class="cc-eyebrow" style="margin-bottom: 14px;">About</div>
                <?php foreach (preg_split('/\r?\n\r?\n/', trim((string)$application->about)) ?: [] as $para): ?>
                    <p><?= nl2br(h(trim($para))) ?></p>
                <?php endforeach; ?>
            </div>

            <?php if (!empty($application->availability_summary)): ?>
                <div class="cc-msg-body">
                    <div class="cc-eyebrow" style="margin-bottom: 14px;">Availability</div>
                    <p><?= nl2br(h((string)$application->availability_summary)) ?></p>
                </div>
            <?php endif; ?>
        </div>

        <aside class="cc-reveal" style="--cc-reveal-delay: 100ms;">
            <?php if ($application->status !== 'approved' && $application->status !== 'rejected'): ?>
                <div class="cc-card" style="margin-bottom: 16px;">
                    <div class="cc-eyebrow" style="margin-bottom: 14px;">Decide</div>
                    <p class="cc-muted" style="font-size: 13.5px; margin-bottom: 14px;">
                        Approving creates a new <strong>Teacher</strong> entry from this application's details. They'll appear on the public site and in the booking flow.
                    </p>
                    <?= $this->Form->postLink('✓ Approve & promote to teacher',
                        ['_name' => 'admin_application_view', 'id' => $application->id],
                        ['data' => ['action' => 'approve'], 'class' => 'cc-btn cc-btn--primary cc-btn--block', 'confirm' => 'Approve this application and create a teacher? An approval email will be sent.']) ?>
                    <div style="height: 8px;"></div>
                    <?= $this->Form->postLink('✕ Reject',
                        ['_name' => 'admin_application_view', 'id' => $application->id],
                        ['data' => ['action' => 'reject'], 'class' => 'cc-btn cc-btn--outline cc-btn--block', 'confirm' => 'Reject this application? A polite rejection email will be sent.']) ?>
                </div>
            <?php endif; ?>

            <?php if ($promotedTeacher): ?>
                <div class="cc-card" style="margin-bottom: 16px; background: var(--cc-moss-tint); border-color: transparent;">
                    <div class="cc-eyebrow" style="margin-bottom: 10px;">Promoted</div>
                    <div style="font-size: 14px;">This applicant became <strong><?= h($promotedTeacher->full_name) ?></strong>.</div>
                    <a class="cc-btn cc-btn--ghost cc-btn--sm" style="margin-top: 10px;" href="<?= $this->Url->build(['_name' => 'admin_teacher_edit', 'id' => $promotedTeacher->id]) ?>">Edit teacher →</a>
                </div>
            <?php endif; ?>

            <?= $this->Form->create($application) ?>
                <div class="cc-card">
                    <div class="cc-eyebrow" style="margin-bottom: 14px;">Status</div>
                    <select class="cc-input" name="status">
                        <?php foreach (['new' => 'New', 'reviewing' => 'Reviewing', 'approved' => 'Approved', 'rejected' => 'Rejected'] as $k => $l): ?>
                            <option value="<?= h($k) ?>" <?= $application->status === $k ? 'selected' : '' ?>><?= h($l) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="cc-card" style="margin-top: 16px;">
                    <div class="cc-eyebrow" style="margin-bottom: 14px;">Internal notes</div>
                    <?= $this->Form->control('admin_notes', ['label' => false, 'class' => 'cc-textarea', 'type' => 'textarea', 'rows' => 4]) ?>
                </div>

                <div style="display: flex; gap: 10px; margin-top: 16px;">
                    <button class="cc-btn cc-btn--dark" type="submit">Save</button>
                    <a class="cc-btn cc-btn--ghost" href="<?= $this->Url->build(['_name' => 'admin_applications']) ?>">Cancel</a>
                </div>
            <?= $this->Form->end() ?>
        </aside>
    </div>
</section>
