<?php

namespace App\User\Framework\Controller\Web;

use App\User\Action\Command\RegisterUserCommand;
use App\User\Action\Handler\RegisterUserHandler;
use App\User\Framework\Entity\User;
use App\User\Framework\Form\GettingStartedFilterForm;
use App\User\Framework\Form\RegistrationFormType;
use App\User\Framework\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class RegistrationController extends AbstractController
{
    public function __construct(private readonly RegisterUserHandler $registerUserHandler,
        private readonly RequestStack $requestStack,
    ) {}

    #[Route('/register', name: 'app_register')]
    public function register(
        Request $request,
        Security $security,
    ): Response {
        $form = $this->createForm(RegistrationFormType::class, new User());
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $user = $this->registerUserHandler->handle(new RegisterUserCommand(
                name: $form->get('name')->getData(),
                email: $form->get('email')->getData(),
                username: $form->get('username')->getData(),
                password: $form->get('plainPassword')->getData(),
            ));

            $security->login($user->user);

            return $this->redirectToRoute('app_index');
        }

        return $this->render('user/register.html.twig', [
            'registrationForm' => $form,
        ]);
    }

    #[Route(path: '/getting-started', name: 'app_getting_started')]
    public function gettingStarted(Request $request, Security $security, UserRepository $userRepository): Response
    {
        if ((new ArrayCollection($userRepository->findAll()))->count() !== 0) {
            return $this->redirectToRoute('app_index');
        }

        $form = $this->createForm(RegistrationFormType::class, new User());
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $user = $this->registerUserHandler->handle(new RegisterUserCommand(
                name: $form->get('name')->getData(),
                email: $form->get('email')->getData(),
                password: $form->get('plainPassword')->getData(),
            ));

            $security->login($user->user, 'form_login');
            $this->requestStack->getCurrentRequest()->getSession()->set('mercure_alert_topic', 'alerts_' . uniqid());

            return $this->redirectToRoute('app_getting_started_filter');
        }

        return $this->render('user/getting_started/register-user.html.twig', [
            'registrationForm' => $form,
        ]);
    }

    #[Route(path: '/getting-started/filter', name: 'app_getting_started_filter')]
    public function gettingStartedPreferences(Request $request, UserRepository $userRepository): Response
    {
        if ((new ArrayCollection($userRepository->findAll()))->count() !== 0) {
            return $this->redirectToRoute('app_index');
        }

        $form = $this->createForm(GettingStartedFilterForm::class);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            foreach ($form->getData() as $preference => $value) {
                if (null !== $value) {
                    $this->getUser()->updateUserPreference($preference, $value);
                }
            }
            $userRepository->getEntityManager()->flush();
            return $this->redirectToRoute('app_index');
        }

        return $this->render(
            'user/getting_started/filter.html.twig',
            [
                'form' => $form,
            ]
        );
    }
}
