<?php

namespace App\Command;

use App\User\Framework\Repository\PreferenceOptionRepository;
use App\User\Framework\Repository\PreferencesRepository;
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: 'Seed the database with required data.',
)]
class SeedDatabaseCommand extends Command
{
    private PreferencesRepository $preferenceRepository;
    private PreferenceOptionRepository $preferenceOptionRepository;

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

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

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

        return Command::SUCCESS;
    }

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

        foreach ($preferences as $preference) {
            if ($this->preferenceRepository->find($preference['id'])) {
                continue;
            }

            $this->preferenceRepository->getEntityManager()->persist((new \App\User\Framework\Entity\Preference())
                ->setId($preference['id'])
                ->setName($preference['name'])
                ->setDescription($preference['description'])
                ->setEnabled($preference['enabled'])
                ->setType($preference['type'])
            );
        }

        $this->preferenceRepository->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' => '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'
            ],
        ];
    }

    private function seedPreferenceOptions(SymfonyStyle $io)
    {
        $io->info('[SeedDatabaseCommand] > Seeding preference options...');
        $options = $this->getPreferenceOptions();

        foreach ($options as $option) {
            if ($this->preferenceOptionRepository->findBy([
                'preference' => $option['preference_id'],
                'name' => $option['name'],
                'value' => $option['value'],
                'enabled' => $option['enabled'],
            ])) {
                continue;
            }
            $this->preferenceOptionRepository->getEntityManager()->persist(
                (new \App\User\Framework\Entity\PreferenceOption())
                    ->setPreference($this->preferenceRepository->find($option['preference_id']))
                    ->setName($option['name'])
                    ->setValue($option['value'])
                    ->setEnabled($option['enabled'])
            );
        }

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

    private function getPreferenceOptions(): array
    {
        return [
            [
                'preference_id' => 'resolution',
                'name' => '720p',
                'value' => '720p',
                'enabled' => true
            ],
            [
                'preference_id' => 'resolution',
                'name' => '1080p',
                'value' => '1080p',
                'enabled' => true
            ],
            [
                'preference_id' => 'resolution',
                'name' => '2160p',
                'value' => '2160p',
                'enabled' => true
            ],
            [
                'preference_id' => 'codec',
                'name' => '-',
                'value' => '-',
                'enabled' => true
            ],
            [
                'preference_id' => 'codec',
                'name' => 'h264',
                'value' => 'h264',
                'enabled' => true
            ],
            [
                'preference_id' => 'codec',
                'name' => 'h265/HEVC',
                'value' => 'h265',
                'enabled' => true
            ]
        ];
    }
}
