<?php

declare(strict_types=1);

namespace App\User\Framework\Controller\Web;

use App\Base\Service\Broadcaster;
use App\User\Action\Command\SaveUserMediaPreferencesCommand;
use App\User\Action\Handler\SaveUserCalendarPreferencesHandler;
use App\User\Action\Handler\SaveUserDownloadPreferencesHandler;
use App\User\Action\Handler\SaveUserMediaPreferencesHandler;
use App\User\Action\Input\SaveUserCalendarPreferencesInput;
use App\User\Action\Input\SaveUserDownloadPreferencesInput;
use App\User\Action\Input\SaveUserMediaPreferencesInput;
use App\User\Database\CountryLanguages;
use App\User\Database\ProviderList;
use App\User\Database\QualityList;
use App\User\Dto\UserPreferencesFactory;
use App\User\Framework\Form\GettingStartedFilterForm;
use App\User\Framework\Form\UserMediaPreferencesForm;
use App\User\Framework\Repository\PreferencesRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class PreferencesController extends AbstractController
{
    public function __construct(
        private readonly PreferencesRepository $preferencesRepository,
        private readonly Broadcaster $broadcaster,
    ) {}

    #[Route('/user/preferences', 'app_user_preferences', methods: ['GET'])]
    public function mediaPreferences(): Response
    {
        $downloadPreferences = $this->getUser()->getDownloadPreferences();
        $calendarPreferences = $this->getUser()->getCalendarPreferences();
        $formData = (array) UserPreferencesFactory::createFromUser($this->getUser());
        $form = $this->createForm(UserMediaPreferencesForm::class, $formData);

        return $this->render(
            'user/preferences.html.twig',
            [
                'downloadPreferences' => $downloadPreferences,
                'calendarPreferences' => $calendarPreferences,
                'preferences_form' => $form,
            ]
        );
    }

    #[Route('/user/preferences/media', 'app_user_media_preferences_submit', methods: ['POST'])]
    public function mediaPreferencesSubmit(
        Request $request,
        SaveUserMediaPreferencesHandler $saveUserMediaPreferencesHandler
    ): Response
    {
        $downloadPreferences = $this->getUser()->getDownloadPreferences();
        $calendarPreferences = $this->getUser()->getCalendarPreferences();
        $form = $this->createForm(UserMediaPreferencesForm::class);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $saveUserMediaPreferencesHandler->handle(
                SaveUserMediaPreferencesCommand::fromUserMediaPreferencesForm($form)
            );
            $this->broadcaster->alert('Success', 'Your media preferences have been saved.');
        }

        return $this->render(
            'user/preferences.html.twig',
            [
                'downloadPreferences' => $downloadPreferences,
                'calendarPreferences' => $calendarPreferences,
                'preferences_form' => $form,
            ]
        );
    }

    #[Route('/user/preferences/download', 'app_save_download_preferences', methods: ['POST'])]
    public function saveDownloadPreferences(
        SaveUserDownloadPreferencesInput $input,
        SaveUserDownloadPreferencesHandler $saveUserDownloadPreferencesHandler,
    ): Response
    {
        $downloadPreferences = $this->getUser()->getDownloadPreferences();
        $calendarPreferences = $this->getUser()->getCalendarPreferences();
        $formData = (array) UserPreferencesFactory::createFromUser($this->getUser());
        $form = $this->createForm(UserMediaPreferencesForm::class, $formData);

        $saveUserDownloadPreferencesHandler->handle($input->toCommand());

        $this->broadcaster->alert(
            title: 'Success',
            message: 'Your download preferences have been saved.'
        );

        return $this->render(
            'user/preferences.html.twig',
            [
                'downloadPreferences' => $downloadPreferences,
                'calendarPreferences' => $calendarPreferences,
                'preferences_form' => $form,
            ]
        );
    }

    #[Route('/user/preferences/calendar', 'app.save.calendar-preferences', methods: ['POST'])]
    public function saveCalendarPreferences(
        SaveUserCalendarPreferencesInput $input,
        SaveUserCalendarPreferencesHandler $handler,
    ): Response
    {
        $calendarPreferences = $this->getUser()->getCalendarPreferences();
        $formData = (array) UserPreferencesFactory::createFromUser($this->getUser());
        $form = $this->createForm(UserMediaPreferencesForm::class, $formData);

        $handler->handle($input->toCommand());

        $this->broadcaster->alert(
            title: 'Success',
            message: 'Your calendar preferences have been saved.'
        );

        return $this->render(
            'user/preferences.html.twig',
            [
                'downloadPreferences' => $this->getUser()->getDownloadPreferences(),
                'calendarPreferences' => $calendarPreferences,
                'preferences_form' => $form,
            ]
        );
    }
}
