<?php
$star = static function ($n): string {
    $n = (int)$n;
    if ($n <= 0) {
        return '<span class="text-muted">Sem avaliação</span>';
    }
    return '<span class="rating-stars">' . str_repeat('★', $n) . str_repeat('☆', 5 - $n) . '</span> <strong>' . $n . '/5</strong>';
};
?>
<main class="container-fluid py-4">
    <div class="row g-4">
        <div class="col-lg-3"><?php require APP_PATH . '/Views/admin/_sidebar.php'; ?></div>
        <div class="col-lg-9">
            <h1 class="h3 fw-bold text-cfc-dark mb-4">Avaliações e Pontuação</h1>
                <div class="col-md-3"><div class="card border-0 shadow-soft rounded-4"><div class="card-body p-4"><span class="text-muted">Avaliações dos Profissionais</span><h2 class="h4 fw-bold text-primary mb-0"><?= (int)($stats['total_professional_reviews'] ?? 0) ?></h2></div></div></div>
                <div class="col-md-3"><div class="card border-0 shadow-soft rounded-4"><div class="card-body p-4"><span class="text-muted">Média Alunos</span><h2 class="h4 fw-bold text-primary mb-0"><?= e((string)($stats['avg_student_score'] ?? '0')) ?>/5</h2></div></div></div>
            </div>

            <div class="alert alert-warning rounded-4 shadow-soft border-0">
                Está pontuação é privada e aparece somente para o administrador. Intrutores apenas enviam a avaliação.
            </div>

            <div class="card border-0 shadow-soft rounded-4 mb-4"><div class="card-body p-4">
                <form class="row g-3">
                    <input type="hidden" name="r" value="admin/ratings">
                    <div class="col-md-3"><label class="form-label">De</label><input type="date" class="form-control" name="date_from" value="<?= e($filters['date_from']) ?>"></div>
                    <div class="col-md-3"><label class="form-label">Até</label><input type="date" class="form-control" name="date_to" value="<?= e($filters['date_to']) ?>"></div>
                    <div class="col-md-4"><label class="form-label">Profissional</label><select name="professional_id" class="form-select"><option value="">Todos</option><?php foreach ($professionals as $p): ?><option value="<?= (int)$p['id'] ?>" <?= ((string)$filters['professional_id']===(string)$p['id'])?'selected':'' ?>><?= e($p['name']) ?></option><?php endforeach; ?></select></div>
                    <div class="col-md-2 d-flex align-items-end"><button class="btn btn-primary w-100"><i class="bi bi-search"></i> Filtrar</button></div>
                </form>
            </div></div>

            <div class="card border-0 shadow-soft rounded-4"><div class="card-body p-4">
                <div class="table-responsive"><table class="table align-middle"><thead><tr><th>Data</th><th>Aluno</th><th>Profissional</th><th>Aluno avaliou profissional</th><th>Profissional avaliou aluno</th></tr></thead><tbody>
                <?php foreach ($ratings as $a): ?>
                    <tr>
                        <td><?= e(date('d/m/Y H:i', strtotime($a['start_at']))) ?></td>
                        <td><strong><?= e($a['student_name']) ?></strong><small class="d-block text-muted"><?= e($a['student_email']) ?></small></td>
                        <td><?= e($a['professional_name']) ?></td>
                        <td>
                            <?= $star($a['student_rating'] ?? 0) ?>
                            <?php if (!empty($a['student_rating_comment'])): ?><small class="d-block text-muted mt-1"><?= e($a['student_rating_comment']) ?></small><?php endif; ?>
                            <?php if (!empty($a['student_rated_at'])): ?><small class="d-block text-muted">Enviado em <?= e(date('d/m/Y H:i', strtotime($a['student_rated_at']))) ?></small><?php endif; ?>
                        </td>
                        <td>
                            <?= $star($a['professional_rating'] ?? 0) ?>
                            <?php if (!empty($a['professional_rating_comment'])): ?><small class="d-block text-muted mt-1"><?= e($a['professional_rating_comment']) ?></small><?php endif; ?>
                            <?php if (!empty($a['professional_rated_at'])): ?><small class="d-block text-muted">Enviado em <?= e(date('d/m/Y H:i', strtotime($a['professional_rated_at']))) ?></small><?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$ratings): ?><tr><td colspan="5" class="text-center text-muted py-4">Nenhuma Avaliação Encontrada.</td></tr><?php endif; ?>
                </tbody></table></div>
            </div></div>
        </div>
    </div>
</main>
