<?php

declare(strict_types=1);

namespace App\Controller;

use App\Entity\Device;
use App\Entity\User;
use App\Enum\Orientation;
use App\Form\RegistrationFormType;
use App\Service\DeviceService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[Route('/setup/{mac}', requirements: ['mac' => '[0-9A-Fa-f]{2}(:[0-9A-Fa-f]{2}){5}'])]
class SetupController extends AbstractController
{
    #[Route('', name: 'setup_index', methods: ['GET', 'POST'])]
    public function index(
        string $mac,
        Request $request,
        UserPasswordHasherInterface $hasher,
        EntityManagerInterface $em,
        Security $security,
        DeviceService $deviceService,
    ): Response {
        // If already authenticated, link device and proceed to configure
        if ($this->getUser()) {
            /** @var User $user */
            $user = $this->getUser();
            $device = $deviceService->linkToUser($mac, $user);
            if (empty($device->getName())) {
                return $this->redirectToRoute('setup_configure', ['mac' => $mac]);
            }
            return $this->redirectToRoute('spa');
        }

        $regForm  = $this->createForm(RegistrationFormType::class, new User(), [
            'action' => $this->generateUrl('setup_register', ['mac' => $mac]),
        ]);
        $loginError = $request->getSession()->get('_setup_login_error');
        $request->getSession()->remove('_setup_login_error');

        return $this->render('setup/index.html.twig', [
            'mac'        => $mac,
            'reg_form'   => $regForm,
            'login_error' => $loginError,
        ]);
    }

    #[Route('/register', name: 'setup_register', methods: ['POST'])]
    public function register(
        string $mac,
        Request $request,
        UserPasswordHasherInterface $hasher,
        EntityManagerInterface $em,
        Security $security,
        DeviceService $deviceService,
    ): Response {
        $user = new User();
        $form = $this->createForm(RegistrationFormType::class, $user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var string $plain */
            $plain = $form->get('plainPassword')->getData();
            $user->setPassword($hasher->hashPassword($user, $plain));
            $em->persist($user);
            $em->flush();

            $security->login($user, 'form_login', 'main');
            $deviceService->linkToUser($mac, $user);

            return $this->redirectToRoute('setup_configure', ['mac' => $mac]);
        }

        return $this->render('setup/index.html.twig', [
            'mac'         => $mac,
            'reg_form'    => $form,
            'login_error' => null,
        ]);
    }

    #[Route('/login', name: 'setup_login', methods: ['POST'])]
    public function login(
        string $mac,
        Request $request,
        UserPasswordHasherInterface $hasher,
        EntityManagerInterface $em,
        Security $security,
        DeviceService $deviceService,
    ): Response {
        $email    = trim((string) $request->request->get('_username', ''));
        $password = (string) $request->request->get('_password', '');

        $user = $em->getRepository(User::class)->findOneBy(['email' => $email]);
        if ($user && $hasher->isPasswordValid($user, $password)) {
            $security->login($user, 'form_login', 'main');
            $deviceService->linkToUser($mac, $user);
            return $this->redirectToRoute('setup_configure', ['mac' => $mac]);
        }

        $request->getSession()->set('_setup_login_error', 'Incorrect email or password');
        return $this->redirectToRoute('setup_index', ['mac' => $mac]);
    }

    #[Route('/configure', name: 'setup_configure', methods: ['GET', 'POST'])]
    #[IsGranted('ROLE_USER')]
    public function configure(
        string $mac,
        Request $request,
        EntityManagerInterface $em,
        DeviceService $deviceService,
    ): Response {
        /** @var User $user */
        $user   = $this->getUser();
        $device = $deviceService->linkToUser($mac, $user);

        if ($request->isMethod('POST')) {
            $name   = trim((string) $request->request->get('name', ''));
            $orient = $request->request->get('orientation', Orientation::Landscape->value);
            $interval  = (int) $request->request->get('rotation_interval_minutes', 1440);
            $window    = (int) $request->request->get('uniqueness_window', 10);

            if (empty($name)) {
                return $this->render('setup/configure.html.twig', [
                    'device' => $device,
                    'error'  => 'Please enter a name for your frame.',
                ]);
            }

            $device->setName($name);
            $device->setOrientation(Orientation::from($orient));
            $device->setRotationIntervalMinutes(max(1, $interval));
            $device->setUniquenessWindow(max(1, $window));
            $em->flush();

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

        return $this->render('setup/configure.html.twig', [
            'device' => $device,
            'error'  => null,
        ]);
    }
}
