<?php

/*
 * This file is part of the Serenity package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Command;

use CpCreation\VitiCore\Contact\Repository\ContactRepository;
use Fidry\AliceDataFixtures\LoaderInterface;
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;
use Symfony\Component\Finder\Finder;

#[AsCommand(name: 'viti-core:fixture-prod:create')]
class FixtureProdCommand
{
    public function __construct(private ContactRepository $contactRepository, private readonly LoaderInterface $fixturesLoader)
    {
    }

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

        $contact = $this->contactRepository->findOne();

        if (null !== $contact) {
            $io->success('Fixtures déjà chargées (Contact trouvé).');

            return Command::SUCCESS;
        }

        $fixturesDir = \dirname(__DIR__, 4).'/fixtures/prod';

        $finder = Finder::create()
            ->files()
            ->in($fixturesDir);

        $files = [];
        foreach ($finder as $file) {
            $files[] = $file->getRealPath();
        }

        $this->fixturesLoader->load($files);

        $io->success('First launch Prod: fixtures chargées.');

        return Command::SUCCESS;
    }
}
