<?php

declare(strict_types=1);

namespace App\User\Framework\Controller\Web;

use App\User\Action\Handler\SaveUserDownloadPreferencesHandler;
use App\User\Action\Handler\SaveUserMediaPreferencesHandler;
use App\User\Action\Input\SaveUserDownloadPreferencesInput;
use App\User\Action\Input\SaveUserMediaPreferencesInput;
use App\User\Framework\Repository\PreferencesRepository;
use App\Util\Broadcaster;
use App\Util\CountryLanguages;
use App\Util\ProviderList;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
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
    {
        $mediaPreferences = $this->getUser()->getMediaPreferences();
        $downloadPreferences = $this->getUser()->getDownloadPreferences();
        $languages = CountryLanguages::$languages;
        sort($languages);

        return $this->render(
            'user/preferences.html.twig',
            [
                'preferences' => $this->preferencesRepository->findEnabled(),
                'languages' => $languages,
                'providers' => ProviderList::$providers,
                'mediaPreferences' => $mediaPreferences,
                'downloadPreferences' => $downloadPreferences,
            ]
        );
    }

    #[Route('/user/preferences/media', 'app_save_media_preferences', methods: ['POST'])]
    public function saveMediaPreferences(
        SaveUserMediaPreferencesInput $input,
        SaveUserMediaPreferencesHandler $saveUserMediaPreferencesHandler,
    ): Response
    {
        $saveUserMediaPreferencesHandler->handle($input->toCommand());
        $mediaPreferences = $this->getUser()->getMediaPreferences();
        $downloadPreferences = $this->getUser()->getDownloadPreferences();

        $languages = CountryLanguages::$languages;
        sort($languages);

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

        return $this->render(
            'user/preferences.html.twig',
            [
                'preferences' => $this->preferencesRepository->findEnabled(),
                'languages' => $languages,
                'providers' => ProviderList::$providers,
                'mediaPreferences' => $mediaPreferences,
                'downloadPreferences' => $downloadPreferences,
            ]
        );
    }

    #[Route('/user/preferences/download', 'app_save_download_preferences', methods: ['POST'])]
    public function saveDownloadPreferences(
        SaveUserDownloadPreferencesInput $input,
        SaveUserDownloadPreferencesHandler $saveUserDownloadPreferencesHandler,
    ): Response
    {
        $downloadPreferences = $saveUserDownloadPreferencesHandler->handle($input->toCommand())->downloadPreferences;
        $mediaPreferences = $this->getUser()->getMediaPreferences();

        $languages = CountryLanguages::$languages;
        sort($languages);

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

        return $this->render(
            'user/preferences.html.twig',
            [
                'preferences' => $this->preferencesRepository->findEnabled(),
                'languages' => $languages,
                'providers' => ProviderList::$providers,
                'mediaPreferences' => $mediaPreferences,
                'downloadPreferences' => $downloadPreferences,
            ]
        );
    }
}
