<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class HomeController extends AbstractController
{
    private const ROLE_ROUTES = [
        'ROLE_EMPLOYE' => 'app_admin_dashboard',
        'ROLE_REVENDEUR' => 'app_espace_prestataire_index',
        'ROLE_CUSTOMER' => 'app_espace_client_index',
    ];

    #[Route('/', name: 'app_home')]
    public function index(): Response
    {
        if (null !== $this->getUser()) {
            return $this->redirectToRoute($this->resolveRedirectRoute());
        }

        return $this->render('home/index.html.twig', [
            'breadcrumbs' => [
                ['name' => 'Accueil'],
            ],
        ]);
    }

    private function resolveRedirectRoute(): string
    {
        foreach (self::ROLE_ROUTES as $role => $route) {
            if ($this->isGranted($role)) {
                return $route;
            }
        }

        return 'app_home';
    }
}
