<?php 
// src/Controller/KeycloakController.php
namespace App\Controller;

use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class KeycloakController extends AbstractController
{
    #[Route('/login/keycloak', name: 'app_login_keycloak_connect')]
    public function connect(ClientRegistry $clientRegistry): Response
    {
        // Si Keycloak est désactivé, retour en page d'accueil, sinon..
        if ($_ENV['KEYCLOAK_ENABLED']=='true') {
            return $clientRegistry->getClient('keycloak')->redirect(['openid', 'profile', 'email']);
        } else {
            $this->addFlash('danger', 'Keycloak non configuré.');
            return $this->redirectToRoute('app_login');
        }
    }

    #[Route('/login/keycloak/check', name: 'app_login_keycloak_connect_check')]
    public function connectCheck(): Response
    {
        // Si Keycloak est désactivé, retour en page d'accueil, sinon..
        if ($_ENV['KEYCLOAK_ENABLED']=='true') {
            return $this->redirectToRoute('app_main');
        } else {
            $this->addFlash('danger', 'Keycloak non configuré.');
            return $this->redirectToRoute('app_login');
        }
    }
}