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

<?= $this->section('content') ?>

<?php
    $activeId = (int)($class_section_id ?? 0);
    $jobCount = (int)($jobCount ?? 0);
    $scoreSummary = $scoreSummary ?? [
        'completionPct' => 0,
        'missing' => 0,
        'expected' => 0,
        'filled' => 0,
        'fieldLabel' => 'Score',
        'types' => [],
    ];
    $commentSummary = $commentSummary ?? [
        'total' => 0,
        'pendingReview' => 0,
        'reviewed' => 0,
        'missing' => 0,
        'missingDetail' => [],
        'types' => [],
    ];
    $attendanceSummary = $attendanceSummary ?? [
        'recorded' => 0,
        'students' => 0,
        'completionPct' => 0,
        'avgAbsences' => 0,
    ];
    $deadlineEvents = $deadlineEvents ?? [];
    $scoreLabel = $scoreSummary['fieldLabel'] ?? 'Score';
    $normalizedSemester = ucfirst(strtolower(trim((string)($semester ?? ''))));
    $dashboardNotifications = $dashboardNotifications ?? [];
    $participationSummary = $participationSummary ?? [
        'filled' => 0,
        'missing' => 0,
        'completionPct' => 0,
        'expected' => 0,
    ];
    $attendanceStatus = $attendanceStatus ?? [
        'date' => '',
        'missingSections' => [],
        'missingNames' => [],
        'submitted' => true,
    ];
    $today = (new \DateTimeImmutable('now'))->format('Y-m-d');
?>

<div class="container py-3 pt-2">
    <?php if (!empty($classes)): ?>
        <div class="alert alert-info">
            <strong>Current class:</strong>
            <?= $activeId ? esc($active_class_name ?? 'Not selected') : 'None selected'; ?>
            <?php if (!empty($school_year)): ?>
                <span class="ms-2 text-muted">(<?= esc($school_year) ?>, <?= esc($semester ?? '') ?>)</span>
            <?php endif; ?>
        </div>
    <?php else: ?>
        <div class="alert alert-warning">
            No classes assigned to you for the current term.
        </div>
    <?php endif; ?>
</div>

<?php if (!empty($dashboardNotifications)): ?>
    <div class="container py-2">
        <?php foreach ($dashboardNotifications as $notification): ?>
            <?php
                $level = in_array($notification['type'] ?? '', ['warning', 'info', 'success', 'danger'], true)
                    ? $notification['type']
                    : 'info';
            ?>
            <div class="alert alert-<?= esc($level) ?> d-flex justify-content-between align-items-start mb-2">
                <div>
                    <?= esc($notification['message'] ?? '') ?>
                </div>
                <?php if (!empty($notification['link'] ?? '')): ?>
                    <a href="<?= esc($notification['link']) ?>" class="btn btn-sm btn-outline-dark ms-3">
                        <?= esc($notification['linkText'] ?? 'View') ?>
                    </a>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

<div class="container py-3">
    <div class="row g-3">
        <div class="col-lg-4">
            <div class="card h-100 shadow-sm border-0">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-2">
                        <h5 class="card-title mb-0">Score submission</h5>
                        <?php if (!empty($scoreSummary['fieldLabel'])): ?>
                            <span class="badge bg-secondary ms-3"><?= esc('Active: ' . $scoreSummary['fieldLabel']) ?></span>
                        <?php endif; ?>
                    </div>
                    <div class="d-flex align-items-baseline mb-2">
                        <h3 class="mb-0"><?= esc($scoreSummary['completionPct'] ?? 0) ?>%</h3>
                        <span class="text-muted ms-2 small">completed</span>
                    </div>
                    <?php if (!empty($scoreSummary['expected'])): ?>
                        <p class="mb-3 small text-muted">
                            <?= esc($scoreSummary['missing'] ?? 0) ?> missing out of <?= esc($scoreSummary['expected']) ?> <?= esc($scoreSummary['fieldLabel']) ?> entries.
                        </p>
                    <?php else: ?>
                        <p class="mb-3 small text-muted">No score entries expected yet.</p>
                    <?php endif; ?>
                    <?php foreach ($scoreSummary['types'] ?? [] as $typeKey => $typeData): ?>
                        <?php if (empty($typeData['expected']) && empty($typeData['filled'])): ?>
                            <?php continue; ?>
                        <?php endif; ?>
                        <?php
                            $progressClass = $typeKey === 'participation'
                                ? 'bg-info'
                                : ($typeData['completionPct'] >= 100 ? 'bg-success' : 'bg-warning');
                        ?>
                        <div class="d-flex justify-content-between align-items-baseline">
                            <span class="small text-muted"><?= esc($typeData['label']) ?></span>
                            <span class="small text-muted"><?= esc($typeData['completionPct']) ?>% complete</span>
                        </div>
                        <div class="progress mb-1" style="height: 6px;">
                            <div class="progress-bar <?= esc($progressClass) ?>" role="progressbar"
                                 style="width: <?= esc($typeData['completionPct']) ?>%;"
                                 aria-valuenow="<?= esc($typeData['completionPct']) ?>"
                                 aria-valuemin="0" aria-valuemax="100"></div>
                        </div>
                        <p class="mb-2 small <?= (int)($typeData['missing'] ?? 0) > 0 ? 'text-danger' : 'text-success' ?>">
                            <?= esc($typeData['missing'] ?? 0) ?> missing of <?= esc($typeData['expected'] ?? 0) ?>.
                        </p>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <div class="col-lg-4">
            <div class="card h-100 shadow-sm border-0">
                <div class="card-body">
                    <h5 class="card-title mb-0">Comments & reviews</h5>
                    <div class="d-flex align-items-baseline mb-2">
                        <h3 class="mb-0"><?= esc($commentSummary['completionPct'] ?? 0) ?>%</h3>
                        <span class="text-muted ms-2 small">completed</span>
                    </div>
                    <div class="d-flex justify-content-between align-items-baseline">
                        <span class="small text-muted">Reviewed</span>
                        <span class="small text-muted"><?= esc($commentSummary['reviewed'] ?? 0) ?> reviewed</span>
                    </div>
                    <div class="d-flex justify-content-between align-items-baseline mb-2">
                        <span class="small text-muted">Pending review</span>
                        <span class="small text-muted"><?= esc($commentSummary['pendingReview'] ?? 0) ?> pending</span>
                    </div>
                    <div class="d-flex flex-column">
                        <?php foreach ($commentSummary['types'] ?? [] as $typeData): ?>
                            <?php if (empty($typeData['expected']) && empty($typeData['filled'])): ?>
                                <?php continue; ?>
                            <?php endif; ?>
                            <?php
                                $progressClass = $typeData['completionPct'] >= 100 ? 'bg-success' : 'bg-warning';
                            ?>
                            <div class="mt-3 d-flex justify-content-between align-items-baseline">
                                <span class="small text-muted"><?= esc($typeData['label']) ?></span>
                                <span class="small text-muted"><?= esc($typeData['completionPct']) ?>% complete</span>
                            </div>
                            <div class="progress mb-1" style="height: 6px;">
                                <div class="progress-bar <?= esc($progressClass) ?>" role="progressbar"
                                     style="width: <?= esc($typeData['completionPct']) ?>%;"
                                     aria-valuenow="<?= esc($typeData['completionPct']) ?>"
                                     aria-valuemin="0" aria-valuemax="100"></div>
                            </div>
                            <p class="mb-2 small <?= (int)($typeData['missing'] ?? 0) > 0 ? 'text-danger' : 'text-success' ?>">
                                <?= esc($typeData['missing'] ?? 0) ?> missing · <?= esc($typeData['pending'] ?? 0) ?> pending review
                            </p>
                        <?php endforeach; ?>
                        <?php if (empty($commentSummary['types'])): ?>
                            <p class="mb-0 small text-muted">No comments recorded yet.</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-4">
            <div class="card h-100 shadow-sm border-0">
                <div class="card-body">
                    <h5 class="card-title mb-0">Attendance tracking</h5>
                    <div class="d-flex justify-content-between align-items-baseline mt-2 mb-2">
                        <span class="small text-muted">Students recorded</span>
                        <span class="small text-muted"><?= esc($attendanceSummary['completionPct'] ?? 0) ?>% complete</span>
                    </div>
                    <div class="progress" style="height: 6px;">
                        <div class="progress-bar bg-warning" role="progressbar"
                             style="width: <?= esc($attendanceSummary['completionPct'] ?? 0) ?>%;"
                             aria-valuenow="<?= esc($attendanceSummary['completionPct'] ?? 0) ?>"
                             aria-valuemin="0" aria-valuemax="100"></div>
                    </div>
                    <p class="mb-2 mt-2 small <?= (int)($attendanceSummary['students'] ?? 0) - (int)($attendanceSummary['recorded'] ?? 0) > 0 ? 'text-danger' : 'text-success' ?>">
                        <?= esc($attendanceSummary['recorded'] ?? 0) ?> of <?= esc($attendanceSummary['students'] ?? 0) ?> students recorded
                    </p>
                    <p class="mb-1 small text-muted">
                        Average absences: <?= esc($attendanceSummary['avgAbsences'] ?? 0) ?>
                    </p>
                    <?php if (!empty($attendanceStatus['missingSections'])): ?>
                        <p class="text-danger small mt-2 mb-0">
                            Today's attendance still open for <?= esc(implode(', ', array_filter($attendanceStatus['missingNames'] ?? []))) ?>.
                        </p>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Jobs section hidden per request -->

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