<?php

declare(strict_types=1);

namespace App\Command\Import;

use App\Entity\Zone;
use App\Entity\Location;
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:import-zones-locations',
    description: 'Import zones and locations from matrix'
)]
class ImportZonesLocationsCommand extends Command
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager
    ) {
        parent::__construct();
    }

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

        // Zone data
        $zoneData = [
            4 => 'Area 4',
            5 => 'Area 5',
            6 => 'Area 6',
            7 => 'Area 7',
        ];

        // Location data
        $locationData = [
            4 => [
                ['code' => 'TAS', 'name' => 'Tasiilaq'],
                ['code' => 'UMI', 'name' => 'Umivik'],
                ['code' => 'SKJ', 'name' => 'Skjoldungen'],
            ],
            5 => [
                ['code' => 'PCS', 'name' => 'Prins Chr. Sund'],
                ['code' => 'NAN', 'name' => 'Nanortalik / Tasermiut fjorden'],
                ['code' => 'QAQ', 'name' => 'Qaqortoq / Igaliku fjorden'],
                ['code' => 'NRQ', 'name' => 'Narsaq/Skovfjord/Bredefjord'],
                ['code' => 'NRQ', 'name' => 'Narsaq - Havnelodsning'],
                ['code' => 'NSSQ', 'name' => 'Narsarssuaq/Skovfjord/Bredefjord'],
                ['code' => 'IVI', 'name' => 'Arsuk/Ivigtut/Grønnedal'],
                ['code' => 'PAA', 'name' => 'Paamiut/Kvanefjord'],
                ['code' => 'NUU', 'name' => 'Nuuk/Nuuk Fjorden'],
                ['code' => 'NUU', 'name' => 'Nuuk - Havnelodsning'],
            ],
            6 => [
                ['code' => 'MAN', 'name' => 'Maniitsoq/Hamborgsund'],
                ['code' => 'EVI', 'name' => 'Evighedsfjorden'],
                ['code' => 'SFJ', 'name' => 'Kangerlussuaq'],
                ['code' => 'SIS', 'name' => 'Sisimiut'],
                ['code' => 'SIS', 'name' => 'Sisimiut - Havnelodsning'],
            ],
            7 => [
                ['code' => 'AAS', 'name' => 'Aasiaat'],
                ['code' => 'QEQ', 'name' => 'Qeqertarsuaq'],
                ['code' => 'QAS', 'name' => 'Qasigiannguit'],
                ['code' => 'ILU', 'name' => 'Ilulissat/Ilimanaq'],
                ['code' => 'QER', 'name' => 'Qeqertaq'],
                ['code' => 'SQQ', 'name' => 'Sarqaq'],
                ['code' => 'VAI', 'name' => 'Vaigat'],
                ['code' => 'UUM', 'name' => 'Uummannaq'],
            ],
        ];

        try {
            // Import zones
            $zones = [];
            foreach ($zoneData as $zoneId => $zoneName) {
                $zone = new Zone($zoneName);
                $this->entityManager->persist($zone);
                $zones[$zoneId] = $zone;
                $io->writeln(sprintf('Created zone "%s"', $zoneName));
            }

            // Import locations
            foreach ($locationData as $zoneId => $locations) {
                $zone = $zones[$zoneId];
                foreach ($locations as $locationInfo) {
                    $location = new Location(
                        $zone,
                        $locationInfo['name'],
                        $locationInfo['code']
                    );

                    $this->entityManager->persist($location);
                    $io->writeln(sprintf(
                        'Created location "%s" (%s) in zone "%s"',
                        $locationInfo['name'],
                        $locationInfo['code'],
                        $zone->getName()
                    ));
                }
            }

            $this->entityManager->flush();
            $io->success('Import completed successfully');

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