<?php

declare(strict_types=1);

namespace App\Command\Import;

use App\Entity\ShippingCompany;
use App\Entity\Vessel;
use Doctrine\ORM\EntityManagerInterface;
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: 'app:create-shipping-test-data',
    description: 'Creates test data for shipping companies and vessels with unique codes'
)]
class CreateShippingTestDataCommand extends Command
{
    private array $companyData = [
        ['name' => 'Arctic Shipping Lines', 'code' => 'ARCTIC-SL'],
        ['name' => 'Greenland Marine Transport', 'code' => 'GRE-MAR-TRANS'],
        ['name' => 'Nordic Sea Carriers', 'code' => 'NORDIC-SEA'],
        ['name' => 'Polar Route Navigation', 'code' => 'POLAR-ROUTE'],
        ['name' => 'Royal Arctic Line', 'code' => 'RAL'],
        ['name' => 'North Atlantic Shipping', 'code' => 'NORTH-ATL'],
        ['name' => 'Viking Ocean Transport', 'code' => 'VIKING-OT'],
        ['name' => 'Ice Route Maritime', 'code' => 'ICE-ROUTE'],
        ['name' => 'Aurora Vessel Operations', 'code' => 'AURORA-VO'],
        ['name' => 'Baltic Sea Logistics', 'code' => 'BALTIC-LOG']
    ];

    private array $vesselNames = [
        'Nordic Explorer',
        'Arctic Princess',
        'Polar Star',
        'Northern Light',
        'Ice Breaker',
        'Aurora',
        'Viking Spirit',
        'Ocean Pioneer',
        'Greenland Express',
        'Baltic Voyager'
    ];

    public function __construct(
        private readonly EntityManagerInterface $entityManager
    ) {
        parent::__construct();
    }

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

        try {
            // Shuffle company data to randomize selection
            shuffle($this->companyData);

            // Select 7-10 companies randomly
            $numberOfCompanies = random_int(7, 10);
            $selectedCompanies = array_slice($this->companyData, 0, $numberOfCompanies);

            foreach ($selectedCompanies as $companyInfo) {
                $company = new ShippingCompany($companyInfo['name'], $companyInfo['code']);
                $this->entityManager->persist($company);

                // Create 1-3 vessels for each company
                $numberOfVessels = random_int(1, 3);

                // Generate unique vessel names and codes
                for ($i = 0; $i < $numberOfVessels; $i++) {
                    $vesselName = $this->vesselNames[array_rand($this->vesselNames)];
                    // Create unique vessel code using company code and counter
                    $vesselCode = sprintf(
                        '%s-V%03d',
                        $companyInfo['code'],
                        random_int(1, 999)
                    );

                    $vessel = new Vessel($company, $vesselName, $vesselCode);
                    $this->entityManager->persist($vessel);
                    $io->writeln(sprintf(
                        'Created vessel "%s" (%s) for company "%s" (%s)',
                        $vesselName,
                        $vesselCode,
                        $companyInfo['name'],
                        $companyInfo['code']
                    ));
                }

                $io->writeln(sprintf(
                    'Created company "%s" (%s) with %d vessels',
                    $companyInfo['name'],
                    $companyInfo['code'],
                    $numberOfVessels
                ));
            }

            $this->entityManager->flush();
            $io->success(sprintf('Created %d companies with vessels', $numberOfCompanies));

            return Command::SUCCESS;
        } catch (\Exception $e) {
            $io->error('Error creating test data: ' . $e->getMessage());
            return Command::FAILURE;
        }
    }
}