<?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\RegistrationFormType;
use App\User\Framework\Pipeline\GettingStarted\AddPreferencesToDatabase;
use App\User\Framework\Pipeline\GettingStarted\GettingStartedInput;
use App\User\Framework\Pipeline\GettingStarted\MigrateDatabase;
use App\User\Framework\Repository\PreferencesRepository;
use App\User\Framework\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use League\Pipeline\Pipeline;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Attribute\Route;

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

    #[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, PreferencesRepository $preferencesRepository, KernelInterface $kernel, LoggerInterface $logger): 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);

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

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