<?php

declare(strict_types=1);

namespace App\Controller\Platform;

use App\Repository\EcoleRepository;
use App\Repository\ThemeRepository;
use App\Repository\UserRepository;
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('/platform', name: 'platform_')]
#[IsGranted('ROLE_SUPER_ADMIN')]
final class DashboardController extends AbstractController
{
    #[Route('', name: 'dashboard', methods: ['GET'])]
    public function index(EcoleRepository $ecoleRepository, ThemeRepository $themeRepository, UserRepository $userRepository): Response
    {
        $totalEcoles = $ecoleRepository->count([]);
        $ecolesActives = $ecoleRepository->countActives();
        $themesActifs = $themeRepository->countActifs();
        $adminsEcole = $userRepository->countAdminsEcole();

        $schools = array_map(static fn ($ecole): array => [
            'name' => $ecole->getNom(),
            'status' => $ecole->isActive() ? 'Actif' : 'Inactif',
            'theme' => $ecole->getThemePublic()?->getNom() ?? 'À définir',
            'students' => '0',
            'location' => $ecole->getDisplayLocation(),
            'updatedAt' => $ecole->getUpdatedAt()?->format('d/m/Y') ?? 'Non renseigné',
        ], $ecoleRepository->findRecent(5));

        return $this->render('platform/dashboard/index.html.twig', [
            'stats' => [
                ['label' => 'Écoles actives', 'value' => (string) $ecolesActives, 'trend' => sprintf('%d madrassa(s) au total', $totalEcoles)],
                ['label' => 'Admins école', 'value' => (string) $adminsEcole, 'trend' => 'Comptes rattachés aux madrassas'],
                ['label' => 'Écoles en configuration', 'value' => (string) max(0, $totalEcoles - $ecolesActives), 'trend' => 'À finaliser ou réactiver'],
                ['label' => 'Thèmes actifs', 'value' => (string) $themesActifs, 'trend' => 'Qamar et Daroul-Ilm pour le MVP'],
            ],
            'schools' => $schools,
            'bars' => [42, 58, 66, 74, 71, 82, 88],
            'queue' => [
                ['title' => 'Nouvelle demande de thème', 'meta' => 'Équipe Daroul-Ilm', 'state' => 'À revoir'],
                ['title' => 'Activation école en attente', 'meta' => 'Sous-domaine à vérifier', 'state' => 'En cours'],
                ['title' => 'Invitation admin non acceptée', 'meta' => 'Relance automatique demain', 'state' => 'Planifiée'],
            ],
        ]);
    }
}
