<?php

declare(strict_types=1);

namespace App\Controller\School;

use App\Service\School\SchoolContext;
use App\Repository\AnneeScolaireRepository;
use App\Repository\ClasseRepository;
use App\Repository\EtudiantRepository;
use App\Repository\InscriptionRepository;
use App\Repository\NiveauRepository;
use App\Repository\SectionRepository;
use App\Entity\Inscription;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[Route('/school', name: 'school_')]
#[IsGranted('ROLE_ECOLE_ADMIN')]
final class DashboardController extends AbstractController
{
    public function __construct(private readonly SchoolContext $schoolContext)
    {
    }

    #[Route('', name: 'dashboard', methods: ['GET'])]
    public function index(
        SectionRepository $sectionRepository,
        NiveauRepository $niveauRepository,
        ClasseRepository $classeRepository,
        AnneeScolaireRepository $anneeRepository,
        InscriptionRepository $inscriptionRepository,
        EtudiantRepository $etudiantRepository,
    ): Response {
        $ecole = $this->schoolContext->getEcole();
        $currentAnnee = $anneeRepository->findCurrentForEcole($ecole);

        return $this->render('school/dashboard/index.html.twig', [
            'ecole' => $ecole,
            'currentAnnee' => $currentAnnee,
            'stats' => [
                ['label' => 'Sections actives', 'value' => (string) $sectionRepository->countForEcole($ecole), 'trend' => 'Programmes configurés', 'icon' => 'category'],
                ['label' => 'Demandes en attente', 'value' => (string) $inscriptionRepository->countForEcole($ecole, Inscription::STATUT_SOUMISE, $currentAnnee), 'trend' => 'À traiter', 'icon' => 'fact_check'],
                ['label' => 'À tester', 'value' => (string) $inscriptionRepository->countForEcole($ecole, Inscription::STATUT_A_TESTER, $currentAnnee), 'trend' => 'Orientation pédagogique', 'icon' => 'rule'],
                ['label' => 'Étudiants', 'value' => (string) $etudiantRepository->countForEcole($ecole, $currentAnnee), 'trend' => 'Année en cours', 'icon' => 'school'],
                ['label' => 'Classes ouvertes', 'value' => (string) $classeRepository->countForEcole($ecole), 'trend' => 'Groupes disponibles', 'icon' => 'meeting_room'],
            ],
            'sections' => $sectionRepository->findForEcole($ecole, true),
            'classes' => $classeRepository->findForEcole($ecole, true),
            'levels' => $niveauRepository->findForEcole($ecole, true),
            'latestInscriptions' => $inscriptionRepository->findLatestForEcole($ecole, 5, $currentAnnee),
            'latestEtudiants' => $etudiantRepository->findLatestForEcole($ecole, 5, $currentAnnee),
            'anneeCount' => $anneeRepository->countForEcole($ecole),
            'bars' => [28, 44, 39, 62, 54, 76, 70],
        ]);
    }
}
