<?php

namespace AutoAdmin\Bundle\Content\Command;

use AutoAdmin\Bundle\Content\Entity\Domain;
use AutoAdmin\Bundle\Content\Entity\Locale;
use AutoAdmin\Bundle\Content\Entity\Site;
use Doctrine\Persistence\ManagerRegistry;
use Exception;
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\Question\Question;

#[AsCommand(name: 'aa:content:create-site')]
class CreateSiteCommand extends Command
{
    private ManagerRegistry $doctrine;

    public function __construct(ManagerRegistry $doctrine)
    {
        $this->doctrine = $doctrine;

        parent::__construct();
    }

    public function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->writeln('<info>' . 'Site/Domain/Locale created with success!' . '</info>');

        $helper = $this->getHelper('question');

        $em = $this->doctrine->getManager();

        try {
            $siteSlug = $helper->ask(
                $input,
                $output,
                new Question('Website slug ?')
            );

            $siteName = $helper->ask(
                $input,
                $output,
                new Question('Website name ? (label, only used if site does not exists)', 'default')
            );

            $siteDomain = $helper->ask(
                $input,
                $output,
                new Question('Website domain ?')
            );

            $siteLocale = $helper->ask(
                $input,
                $output,
                new Question('Website locale ? (2 letter code)')
            );

            if (
                empty($siteSlug)
                || empty($siteName)
                || empty($siteDomain)
                || empty($siteLocale)
            ) {
                $output->writeln('<error>' . 'You must provide all options. Aborting.' . '</error>');

                return self::FAILURE;
            }

            $site = $em->getRepository(Site::class)->findOneBy(['slug' => $siteSlug,])
                ?? new Site($siteSlug, $siteSlug);

            $domain = $em->getRepository(Domain::class)->findOneBy(['domain' => $siteDomain,])
                ?? new Domain($siteDomain);

            $locale = $em->getRepository(Locale::class)->findOneBy(['code' => $siteLocale,])
                ?? new Locale($siteLocale);

            if (!$domain->getLocales()->contains($locale)) {
                $domain->addLocale($locale);
            }

            if (!$site->getDomains()->contains($domain)) {
                $site->addDomain($domain);
            }

            foreach ([$site, $domain, $locale] as $item) {
                if (empty($item->getId())) {
                    $em->persist($item);
                }
            }

            $em->flush();

            $output->writeln('<info>' . 'Site/Domain/Locale created with success!' . '</info>');

        } catch (Exception $e) {
            $output->writeln('<error>' . $e->getMessage() . '</error>');

            return self::FAILURE;
        }

        return self::SUCCESS;
    }

    protected function configure(): void
    {
        $this->setDescription('Create a new site with its domains.');
    }
}
