<?php

namespace App\Command;

use App\Entity\CoastalPoint;
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:seed-coastal-points',
    description: 'Initialise les points côtiers Antilles (idempotent)',
)]
class SeedCoastalPointsCommand extends Command
{
    // [nom, lng, lat, type, region]
    private const POINTS = [
        // Martinique
        ['Anse Trabaud',        -60.8241, 14.4161, 'beach',   'Martinique'],
        ['Les Salines',         -60.8297, 14.4111, 'beach',   'Martinique'],
        ['Le Diamant',          -61.0246, 14.4584, 'beach',   'Martinique'],
        ['Anse d\'Arlet',       -61.0846, 14.4864, 'beach',   'Martinique'],
        ['Cap Chevalier',       -60.8518, 14.4536, 'surf',    'Martinique'],
        ['Le Marin',            -60.8762, 14.4726, 'port',    'Martinique'],
        ['Fort-de-France',      -61.0736, 14.6048, 'port',    'Martinique'],
        ['Grand Rivière',       -61.1897, 14.8737, 'fishing', 'Martinique'],
        ['Le Vauclin',          -60.8380, 14.5548, 'fishing', 'Martinique'],
        ['Sainte-Anne',         -60.8833, 14.4300, 'beach',   'Martinique'],
        // Guadeloupe
        ['Saint-François',      -61.2695, 16.2527, 'beach',   'Guadeloupe'],
        ['Sainte-Anne (Gwad)',   -61.3789, 16.2248, 'beach',   'Guadeloupe'],
        ['Le Gosier',           -61.4918, 16.1989, 'beach',   'Guadeloupe'],
        ['Plage de la Caravelle',-61.3661, 16.2361, 'surf',    'Guadeloupe'],
        ['Pointe-à-Pitre',      -61.5361, 16.2419, 'port',    'Guadeloupe'],
        ['Basse-Terre port',    -61.7339, 15.9955, 'port',    'Guadeloupe'],
        ['Bouillante',          -61.7681, 16.1348, 'beach',   'Guadeloupe'],
        ['Capesterre-Belle-Eau',-61.5621, 16.0432, 'fishing', 'Guadeloupe'],
        // Sainte-Lucie
        ['Rodney Bay',          -60.9520, 14.0778, 'port',    'Sainte-Lucie'],
        ['Anse Chastanet',      -61.0661, 13.8588, 'beach',   'Sainte-Lucie'],
        ['Marigot Bay',         -61.0230, 13.9667, 'port',    'Sainte-Lucie'],
        ['Castries harbour',    -61.0045, 14.0101, 'port',    'Sainte-Lucie'],
        // Barbade
        ['Miami Beach Barbados', -59.6203, 13.0756, 'surf',   'Barbade'],
        ['Bathsheba',           -59.5337, 13.2115, 'surf',    'Barbade'],
        ['Bridgetown Harbour',  -59.6147, 13.0975, 'port',    'Barbade'],
        ['Carlisle Bay',        -59.6161, 13.0817, 'beach',   'Barbade'],
        // Saint-Martin
        ['Orient Bay',          -63.0311, 18.0917, 'beach',   'Saint-Martin'],
        ['Grand Case',          -63.0500, 18.1017, 'beach',   'Saint-Martin'],
        ['Marigot',             -63.0833, 18.0667, 'port',    'Saint-Martin'],
    ];

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

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $io->title('Seed CoastalPoints — Antilles');

        $repo    = $this->em->getRepository(CoastalPoint::class);
        $created = 0;
        $skipped = 0;

        foreach (self::POINTS as [$name, $lng, $lat, $type, $region]) {
            $existing = $repo->findOneBy(['name' => $name, 'region' => $region]);

            if ($existing !== null) {
                $skipped++;
                continue;
            }

            $point = new CoastalPoint();
            $point->setName($name);
            $point->setGeometry(sprintf('SRID=4326;POINT(%f %f)', $lng, $lat));
            $point->setType($type);
            $point->setRegion($region);

            $this->em->persist($point);
            $created++;
        }

        $this->em->flush();

        $io->success(sprintf('%d point(s) créé(s), %d ignoré(s) (déjà existants).', $created, $skipped));

        return Command::SUCCESS;
    }
}
