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

<?= $this->section('content') ?>
    <div class="space-y-6">
        <section class="page-header">
            <div>
                <p class="page-kicker">Child Profile</p>
                <div class="mt-2 flex flex-wrap items-center gap-3">
                    <h2 class="section-title text-2xl"><?= esc($child['full_name']) ?></h2>
                    <?php if (! empty($child['level_name'])) : ?>
                        <span class="inline-flex rounded-full border px-3 py-1 text-xs font-semibold" style="<?= esc(level_chip_style($child['color_hex']), 'attr') ?>">
                            <?= esc($child['level_name']) ?>
                        </span>
                    <?php endif; ?>
                    <?= view('components/status_badge', ['status' => strtoupper((string) $child['status'])]) ?>
                </div>
                <p class="section-copy mt-2 max-w-3xl"><?= esc($pageDescription) ?></p>
            </div>
            <div class="flex flex-wrap gap-3">
                <?php if ($canEvaluate) : ?>
                    <a href="<?= esc($evaluationActions['full']) ?>" class="btn-primary">Adauga Full Evaluation</a>
                    <a href="<?= esc($evaluationActions['quick']) ?>" class="btn-secondary">Adauga Quick Check</a>
                <?php endif; ?>
                <?php if ($canManage) : ?>
                    <a href="<?= url_to('children.edit', $child['id']) ?>" class="btn-secondary">Editeaza profilul</a>
                <?php endif; ?>
                <a href="<?= url_to('children.index') ?>" class="btn-ghost">Inapoi la lista</a>
            </div>
        </section>

        <?= view('components/tab_nav', ['tabs' => $tabs]) ?>

        <?php if ($activeTab === 'overview') : ?>
            <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">Scor mediu</p>
                    <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc(format_score($overview['average_score'])) ?></p>
                </article>
                <article class="mini-stat">
                    <p class="text-sm font-medium text-slate-500">Prezenta</p>
                    <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc(format_percentage($overview['attendance_rate'], 1)) ?></p>
                </article>
                <article class="mini-stat">
                    <p class="text-sm font-medium text-slate-500">Nr. evaluari</p>
                    <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc((string) $overview['evaluations_count']) ?></p>
                </article>
                <article class="mini-stat">
                    <p class="text-sm font-medium text-slate-500">Status evaluare</p>
                    <div class="mt-4"><?= view('components/status_badge', ['status' => $overview['latest_status']]) ?></div>
                </article>
            </section>

            <section class="grid gap-6 xl:grid-cols-2">
                <article class="surface-card p-6">
                    <h3 class="section-title">Informatii copil</h3>
                    <dl class="info-list mt-5 grid gap-5 md:grid-cols-2">
                        <div>
                            <dt>Data nasterii</dt>
                            <dd><?= esc($child['birth_date'] ?: '-') ?></dd>
                        </div>
                        <div>
                            <dt>Gen</dt>
                            <dd><?= esc(gender_label($child['gender'])) ?></dd>
                        </div>
                        <div>
                            <dt>Grupa</dt>
                            <dd><?= esc($child['group_name'] ?: '-') ?></dd>
                        </div>
                        <div>
                            <dt>Trainer principal</dt>
                            <dd><?= esc($child['coach_name'] ?: '-') ?></dd>
                        </div>
                        <div>
                            <dt>Prima zi curs</dt>
                            <dd><?= esc($child['first_class_at'] ?: '-') ?></dd>
                        </div>
                        <div>
                            <dt>Frati / surori</dt>
                            <dd><?= esc(yes_no_label($child['has_siblings'])) ?></dd>
                        </div>
                        <div class="md:col-span-2">
                            <dt>Observatii</dt>
                            <dd><?= esc($child['notes'] ?: 'Nu exista observatii inregistrate.') ?></dd>
                        </div>
                    </dl>
                </article>

                <article class="surface-card p-6">
                    <h3 class="section-title">Informatii parinte</h3>
                    <dl class="info-list mt-5 grid gap-5 md:grid-cols-2">
                        <div>
                            <dt>Nume</dt>
                            <dd><?= esc($child['guardian_name'] ?: '-') ?></dd>
                        </div>
                        <div>
                            <dt>Telefon</dt>
                            <dd><?= esc($child['guardian_phone'] ?: '-') ?></dd>
                        </div>
                        <div class="md:col-span-2">
                            <dt>Email</dt>
                            <dd><?= esc($child['guardian_email'] ?: '-') ?></dd>
                        </div>
                    </dl>
                </article>
            </section>

            <section class="grid gap-6 xl:grid-cols-2">
                <article class="surface-card p-6">
                    <div class="mb-6">
                        <h3 class="section-title">Evolutia scorului</h3>
                        <p class="section-copy">Toate evaluarile disponibile pentru acest copil, afisate cronologic.</p>
                    </div>
                    <?php if ($overview['score_trend']['series'] === []) : ?>
                        <?= view('components/empty_state', ['title' => 'Nu exista evaluari', 'description' => 'Graficul se populeaza dupa primele evaluari sau quick checks.']) ?>
                    <?php else : ?>
                        <div data-chart="line" data-categories='<?= esc(json_encode($overview['score_trend']['categories']), 'attr') ?>' data-series='<?= esc(json_encode($overview['score_trend']['series']), 'attr') ?>' class="h-80"></div>
                    <?php endif; ?>
                </article>

                <article class="surface-card p-6">
                    <div class="mb-6">
                        <h3 class="section-title">Profil criterii</h3>
                        <p class="section-copy">Ultimul set de criterii din evaluarea relevanta pentru profilul copilului.</p>
                    </div>
                    <?php if ($overview['criteria_profile']['series'] === []) : ?>
                        <?= view('components/empty_state', ['title' => 'Nu exista scoruri pe criterii', 'description' => 'Profilul radar apare dupa prima evaluare completa.']) ?>
                    <?php else : ?>
                        <div data-chart="radar" data-categories='<?= esc(json_encode($overview['criteria_profile']['labels']), 'attr') ?>' data-series='<?= esc(json_encode($overview['criteria_profile']['series']), 'attr') ?>' class="h-80"></div>
                    <?php endif; ?>
                </article>
            </section>

            <section class="surface-card p-6">
                <div class="mb-4">
                    <h3 class="section-title">Ultima evaluare</h3>
                    <p class="section-copy">Rezumatul celei mai recente inregistrari din logul de evaluari.</p>
                </div>
                <?php if ($overview['latest_evaluation'] === null) : ?>
                    <?= view('components/empty_state', ['title' => 'Nu exista evaluari', 'description' => 'Acest copil nu are inca evaluari inregistrate.']) ?>
                <?php else : ?>
                    <div class="grid gap-5 md:grid-cols-5">
                        <div class="surface-muted px-4 py-4">
                            <p class="text-xs uppercase tracking-[0.16em] text-slate-400">Data</p>
                            <p class="mt-2 text-sm font-semibold text-slate-900"><?= esc($overview['latest_evaluation']['evaluation_date']) ?></p>
                        </div>
                        <div class="surface-muted px-4 py-4">
                            <p class="text-xs uppercase tracking-[0.16em] text-slate-400">Tip</p>
                            <p class="mt-2 text-sm font-semibold text-slate-900"><?= esc($overview['latest_evaluation']['type_name']) ?></p>
                        </div>
                        <div class="surface-muted px-4 py-4">
                            <p class="text-xs uppercase tracking-[0.16em] text-slate-400">Scor final</p>
                            <p class="mt-2 text-sm font-semibold text-slate-900"><?= esc(format_score($overview['latest_evaluation']['final_score'])) ?></p>
                        </div>
                        <div class="surface-muted px-4 py-4">
                            <p class="text-xs uppercase tracking-[0.16em] text-slate-400">Status</p>
                            <div class="mt-2"><?= view('components/status_badge', ['status' => $overview['latest_evaluation']['final_status']]) ?></div>
                        </div>
                        <div class="surface-muted px-4 py-4">
                            <p class="text-xs uppercase tracking-[0.16em] text-slate-400">Evaluator</p>
                            <p class="mt-2 text-sm font-semibold text-slate-900"><?= esc($overview['latest_evaluation']['evaluator_name'] ?: '-') ?></p>
                        </div>
                    </div>
                    <div class="mt-4 rounded-2xl bg-slate-50 px-4 py-4 text-sm text-slate-600">
                        <?= esc($overview['latest_evaluation']['notes'] ?: 'Fara note aditionale.') ?>
                    </div>
                    <div class="mt-4">
                        <a href="<?= url_to('evaluations.show', $overview['latest_evaluation']['id']) ?>" class="btn-ghost">Vezi evaluarea</a>
                    </div>
                <?php endif; ?>
            </section>
        <?php elseif ($activeTab === 'evaluations') : ?>
            <section class="surface-card p-6">
                <div class="mb-4 flex flex-wrap items-center justify-between gap-4">
                    <div>
                        <h3 class="section-title">Istoric Full Evaluation</h3>
                        <p class="section-copy">Evaluari complete cu scoruri ponderate, status calculat automat si semnal de promovare.</p>
                    </div>
                    <?php if ($canEvaluate) : ?>
                        <a href="<?= esc($evaluationActions['full']) ?>" class="btn-primary">Adauga Full Evaluation</a>
                    <?php endif; ?>
                </div>
                <?php if ($fullEvaluations === []) : ?>
                    <?= view('components/empty_state', ['title' => 'Nu exista Full Evaluations', 'description' => 'Copilul nu are inca evaluari complete inregistrate.']) ?>
                <?php else : ?>
                    <div class="table-shell">
                        <table>
                            <thead>
                                <tr>
                                    <th>Data</th>
                                    <th>Scor</th>
                                    <th>Status</th>
                                    <th>Evaluator</th>
                                    <th>Note</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($fullEvaluations as $evaluation) : ?>
                                    <tr>
                                        <td><?= esc($evaluation['evaluation_date']) ?></td>
                                        <td><?= esc(format_score($evaluation['final_score'])) ?></td>
                                        <td><?= view('components/status_badge', ['status' => $evaluation['final_status']]) ?></td>
                                        <td><?= esc($evaluation['evaluator_name'] ?: '-') ?></td>
                                        <td class="max-w-xs"><?= esc($evaluation['notes'] ?: '-') ?></td>
                                        <td><a href="<?= url_to('evaluations.show', $evaluation['id']) ?>" class="btn-ghost">Detaliu</a></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </section>
        <?php elseif ($activeTab === 'quick-checks') : ?>
            <section class="surface-card p-6">
                <div class="mb-4 flex flex-wrap items-center justify-between gap-4">
                    <div>
                        <h3 class="section-title">Quick Check history</h3>
                        <p class="section-copy">Quick Check-urile sunt separate clar de evaluarile complete si nu modifica automat nivelul copilului.</p>
                    </div>
                    <?php if ($canEvaluate) : ?>
                        <a href="<?= esc($evaluationActions['quick']) ?>" class="btn-primary">Adauga Quick Check</a>
                    <?php endif; ?>
                </div>
                <?php if ($quickChecks === []) : ?>
                    <?= view('components/empty_state', ['title' => 'Nu exista Quick Checks', 'description' => 'Lista se populeaza din evaluari de tip Quick Check.']) ?>
                <?php else : ?>
                    <div class="table-shell">
                        <table>
                            <thead>
                                <tr>
                                    <th>Data</th>
                                    <th>Scor</th>
                                    <th>Status</th>
                                    <th>Evaluator</th>
                                    <th>Note</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($quickChecks as $check) : ?>
                                    <tr>
                                        <td><?= esc($check['evaluation_date']) ?></td>
                                        <td><?= esc(format_score($check['final_score'])) ?></td>
                                        <td><?= view('components/status_badge', ['status' => $check['final_status']]) ?></td>
                                        <td><?= esc($check['evaluator_name'] ?: '-') ?></td>
                                        <td class="max-w-xs"><?= esc($check['notes'] ?: '-') ?></td>
                                        <td><a href="<?= url_to('evaluations.show', $check['id']) ?>" class="btn-ghost">Detaliu</a></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </section>
        <?php elseif ($activeTab === 'attendance') : ?>
            <section class="surface-card p-6">
                <div class="mb-4">
                    <h3 class="section-title">Prezenta recenta</h3>
                    <p class="section-copy">Sesiunile afisate exclud sedintele anulate si folosesc statusurile reale din attendance log.</p>
                </div>
                <?php if ($attendanceRows === []) : ?>
                    <?= view('components/empty_state', ['title' => 'Nu exista inregistrari de prezenta', 'description' => 'Prezenta apare dupa primele sesiuni inregistrate pentru copil.']) ?>
                <?php else : ?>
                    <div class="table-shell">
                        <table>
                            <thead>
                                <tr>
                                    <th>Data</th>
                                    <th>Status</th>
                                    <th>Grupa</th>
                                    <th>Trainer</th>
                                    <th>Interval</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($attendanceRows as $row) : ?>
                                    <tr>
                                        <td><?= esc($row['session_date']) ?></td>
                                        <td><?= view('components/status_badge', ['status' => strtoupper((string) $row['status'])]) ?></td>
                                        <td><?= esc($row['group_name'] ?: '-') ?></td>
                                        <td><?= esc($row['coach_name'] ?: '-') ?></td>
                                        <td><?= esc(substr((string) $row['start_time'], 0, 5) . ' - ' . substr((string) $row['end_time'], 0, 5)) ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </section>
        <?php else : ?>
            <section class="surface-card p-6">
                <div class="mb-4">
                    <h3 class="section-title">Journey timeline</h3>
                    <p class="section-copy">Event log real generat automat din modificarile importante din profil si din evaluari.</p>
                </div>
                <?php if ($journeyRows === []) : ?>
                    <?= view('components/empty_state', ['title' => 'Nu exista evenimente in journey', 'description' => 'Timeline-ul va afisa modificarile de profil si evenimentele operationale.']) ?>
                <?php else : ?>
                    <div class="space-y-4">
                        <?php foreach ($journeyRows as $event) : ?>
                            <?= view('components/timeline_item', [
                                'title' => $event['title'],
                                'description' => $event['description'] ?: null,
                                'date' => $event['created_at'],
                                'meta' => status_label($event['event_type']),
                            ]) ?>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </section>
        <?php endif; ?>
    </div>
<?= $this->endSection() ?>
