<?= $this->extend('layouts/dashboard') ?>

<?= $this->section('content') ?>
    <div class="space-y-6">
        <section class="page-header">
            <div>
                <p class="page-kicker">Profil trainer</p>
                <div class="mt-2 flex flex-wrap items-center gap-3">
                    <h2 class="section-title text-2xl"><?= esc($coach['full_name']) ?></h2>
                    <span class="rounded-full bg-slate-100 px-3 py-1 text-xs font-semibold uppercase tracking-[0.16em] text-slate-500"><?= esc($coach['display_role']) ?></span>
                    <?= view('components/status_badge', ['status' => (int) $coach['is_active'] === 1 ? 'ACTIVE' : 'INACTIVE']) ?>
                </div>
                <p class="section-copy mt-2 max-w-3xl"><?= esc($pageDescription) ?></p>
            </div>
            <div class="flex flex-wrap gap-3">
                <?php if ($canManage) : ?>
                    <a href="<?= url_to('coaches.edit', $coach['id']) ?>" class="btn-secondary">Editeaza</a>
                <?php endif; ?>
                <a href="<?= url_to('coaches.index') ?>" class="btn-ghost">Inapoi la lista</a>
            </div>
        </section>

        <section class="grid gap-4 sm:grid-cols-2 xl:grid-cols-4">
            <article class="mini-stat">
                <p class="text-sm font-medium text-slate-500">Copii activi</p>
                <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc((string) count(array_filter($children, static fn (array $child): bool => $child['status'] === 'active'))) ?></p>
            </article>
            <article class="mini-stat">
                <p class="text-sm font-medium text-slate-500">Evaluari recente</p>
                <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc((string) count($recentEvaluations)) ?></p>
            </article>
            <article class="mini-stat">
                <p class="text-sm font-medium text-slate-500">Quick checks</p>
                <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc((string) count($recentQuickChecks)) ?></p>
            </article>
            <article class="mini-stat">
                <p class="text-sm font-medium text-slate-500">Zile disponibile</p>
                <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc((string) count($availability)) ?></p>
            </article>
        </section>

        <section class="grid gap-6 xl:grid-cols-2">
            <article class="surface-card p-6">
                <h3 class="section-title">Date contact</h3>
                <dl class="info-list mt-5 grid gap-5 md:grid-cols-2">
                    <div>
                        <dt>Email</dt>
                        <dd><?= esc($coach['email']) ?></dd>
                    </div>
                    <div>
                        <dt>Telefon</dt>
                        <dd><?= esc($coach['phone'] ?: '—') ?></dd>
                    </div>
                    <div>
                        <dt>Username</dt>
                        <dd>@<?= esc($coach['username'] ?: 'coach') ?></dd>
                    </div>
                    <div>
                        <dt>Avatar path</dt>
                        <dd><?= esc($coach['avatar_path'] ?: '—') ?></dd>
                    </div>
                    <div class="md:col-span-2">
                        <dt>Observatii</dt>
                        <dd><?= esc($coach['notes'] ?: 'Fara observatii operationale.') ?></dd>
                    </div>
                </dl>
            </article>

            <article class="surface-card p-6">
                <h3 class="section-title">Disponibilitate</h3>
                <?php if ($availability === []) : ?>
                    <?= view('components/empty_state', ['title' => 'Program neconfigurat', 'description' => 'Seteaza disponibilitatea saptamanala din formularul de editare.']) ?>
                <?php else : ?>
                    <div class="mt-5 grid gap-3 sm:grid-cols-2">
                        <?php foreach ($availability as $slot) : ?>
                            <div class="surface-muted px-4 py-4">
                                <p class="text-sm font-semibold text-slate-900"><?= esc(day_of_week_label((int) $slot['day_of_week'])) ?></p>
                                <p class="mt-1 text-sm text-slate-500"><?= esc(substr((string) $slot['start_time'], 0, 5) . ' - ' . substr((string) $slot['end_time'], 0, 5)) ?></p>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </article>
        </section>

        <section class="grid gap-6 xl:grid-cols-[1fr_1fr]">
            <article class="surface-card p-6">
                <div class="mb-4">
                    <h3 class="section-title">Copii asignati</h3>
                    <p class="section-copy">Lista copiilor pentru care acest trainer este setat ca trainer principal.</p>
                </div>
                <?php if ($children === []) : ?>
                    <?= view('components/empty_state', ['title' => 'Nu exista copii asignati', 'description' => 'Copiii vor aparea aici dupa asignare in profilul lor.']) ?>
                <?php else : ?>
                    <div class="table-shell">
                        <table>
                            <thead>
                                <tr>
                                    <th>Copil</th>
                                    <th>Data nasterii</th>
                                    <th>Status</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($children as $child) : ?>
                                    <tr>
                                        <td class="font-medium text-slate-900"><?= esc($child['full_name']) ?></td>
                                        <td><?= esc($child['birth_date'] ?: '—') ?></td>
                                        <td><?= view('components/status_badge', ['status' => strtoupper((string) $child['status'])]) ?></td>
                                        <td><a href="<?= url_to('children.show', $child['id']) ?>" class="btn-ghost">Profil</a></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </article>

            <article class="surface-card p-6">
                <div class="mb-4">
                    <h3 class="section-title">Evaluari recente</h3>
                    <p class="section-copy">Atat quick checks, cat si full evaluations efectuate de acest trainer.</p>
                </div>
                <?php if ($recentEvaluations === []) : ?>
                    <?= view('components/empty_state', ['title' => 'Nu exista evaluari', 'description' => 'Logul se populeaza dupa primele evaluari efectuate de trainer.']) ?>
                <?php else : ?>
                    <div class="space-y-3">
                        <?php foreach ($recentEvaluations as $evaluation) : ?>
                            <article class="surface-muted px-4 py-4">
                                <div class="flex items-start justify-between gap-4">
                                    <div>
                                        <p class="text-sm font-semibold text-slate-900"><?= esc($evaluation['child_name']) ?></p>
                                        <p class="mt-1 text-sm text-slate-500"><?= esc($evaluation['type_name']) ?> · <?= esc($evaluation['evaluation_date']) ?></p>
                                        <p class="mt-2 text-sm text-slate-600">Scor final: <?= esc(format_score($evaluation['final_score'])) ?></p>
                                    </div>
                                    <?= view('components/status_badge', ['status' => $evaluation['final_status']]) ?>
                                </div>
                            </article>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </article>
        </section>
    </div>
<?= $this->endSection() ?>
