<?php

namespace App\Base\Framework\Command;

use App\User\Framework\Entity\Preference;
use App\User\Framework\Entity\UserPreference;
use App\User\Framework\Repository\PreferenceOptionRepository;
use App\User\Framework\Repository\PreferencesRepository;
use App\User\Framework\Repository\UserRepository;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
    name: 'db:seed',
    description: 'Seeds the database with required data. This command is run every time a new container is created from the torsearch-app image and is part of the init process.',
)]
class SeedDatabaseCommand extends Command
{
    private PreferencesRepository $preferenceRepository;

    private UserRepository $userRepository;

    public function __construct(
        PreferencesRepository $preferenceRepository,
        UserRepository $userRepository,
    ) {
        parent::__construct();
        $this->preferenceRepository = $preferenceRepository;
        $this->userRepository = $userRepository;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        $this->seedPreferences($io);
        $this->updateUserPreferences($io);

        return Command::SUCCESS;
    }

    private function seedPreferences(SymfonyStyle $io)
    {
        $io->info('[SeedDatabaseCommand] > Seeding preferences...');
        $preferences = $this->getPreferences();

        foreach ($preferences as $preference) {
            $isNewRecord = false;
            $preferenceRecord = $this->preferenceRepository->findOneBy(['id' => $preference['id']]);
            if (null === $preferenceRecord) {
                $isNewRecord = true;
                $preferenceRecord = new Preference();
            }

            $preferenceRecord
                ->setId($preference['id'])
                ->setName($preference['name'])
                ->setDescription($preference['description'])
                ->setEnabled($preference['enabled'])
                ->setType($preference['type']);

            if (true === $isNewRecord) {
                $this->preferenceRepository->getEntityManager()->persist($preferenceRecord);
            }
        }

        $this->preferenceRepository->getEntityManager()->flush();
    }

    private function updateUserPreferences(SymfonyStyle $io)
    {
        $io->info('[SeedDatabaseCommand] > Updating user preferences...');
        $users = $this->userRepository->findAll();
        $preferences = $this->preferenceRepository->findAll();
        foreach ($users as $user) {
            foreach ($preferences as $preference) {
                if (false === $user->hasUserPreference($preference->getId())) {
                    $user->addUserPreference(
                        new UserPreference()
                            ->setPreference($preference)
                            ->setUser($user)
                            ->setPreferenceValue(null)
                    );
                }
            }
        }
        $this->userRepository->getEntityManager()->flush();
    }

    private function getPreferences(): array
    {
        return [
            [
                'id' => 'codec',
                'name' => 'Codec',
                'description' => null,
                'enabled' => true,
                'type' => 'media',
            ],
            [
                'id' => 'resolution',
                'name' => 'Resolution',
                'description' => null,
                'enabled' => true,
                'type' => 'media',
            ],
            [
                'id' => 'language',
                'name' => 'Language',
                'description' => null,
                'enabled' => true,
                'type' => 'media',
            ],
            [
                'id' => 'provider',
                'name' => 'Provider',
                'description' => null,
                'enabled' => true,
                'type' => 'media',
            ],
            [
                'id' => 'quality',
                'name' => 'Quality',
                'description' => null,
                'enabled' => true,
                'type' => 'media',
            ],
            [
                'id' => 'movie_folder',
                'name' => 'Create new folder for Movies',
                'description' => 'When downloading a movie, store it in a new folder in your base \'movies\' folder. (e.g.: .../movies/Inception/Inception.2160p.h265.mkv)',
                'enabled' => true,
                'type' => 'download'
            ],
            [
                'id' => 'enable_ical_up_ep',
                'name' => 'Enable a publicly available iCal calendar?',
                'description' => 'Enable a publicly accessible iCal URL for your upcoming episodes.',
                'enabled' => false,
                'type' => 'calendar'
            ],
        ];
    }
}
