<?php

declare(strict_types=1);

namespace App\Module\Commercial\Domain\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use App\Module\Commercial\Infrastructure\Doctrine\DoctrineCountryRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;

/**
 * Pays selectionnable dans les adresses (clients / fournisseurs) : referentiel
 * statique seede par la migration (France, Allemagne, Belgique, Espagne, Italie,
 * Royaume-Uni). Remplace la liste de pays jusqu'ici codee en dur cote front.
 *
 * Perimetre minimal (ticket ERP-116, 1re iteration) : code ISO + libelle + ordre
 * d'affichage uniquement. AUCUNE longueur bancaire/fiscale (numero de compte,
 * IBAN, TVA, BIC, SIREN) a ce stade — ces colonnes feront l'objet d'une iteration
 * ulterieure du meme ticket.
 *
 * Lecture seule : GetCollection + Get uniquement ; POST/PATCH/DELETE -> 405.
 * Permission alignee sur Bank (referentiel d'adresse partage clients/fournisseurs).
 * Pas de Timestampable/Blamable (referentiel statique whiteliste dans
 * EntitiesAreTimestampableBlamableTest::EXCLUDED, comme Bank).
 */
#[ApiResource(
    operations: [
        new GetCollection(
            security: "is_granted('commercial.clients.view') or is_granted('commercial.suppliers.view')",
            normalizationContext: ['groups' => ['country:read']],
            // Tri par defaut : position ASC (France en tete) puis name ASC.
            order: ['position' => 'ASC', 'name' => 'ASC'],
            // Toggle ?pagination=false pour alimenter le select (cf. Bank).
            paginationClientEnabled: true,
        ),
        new Get(
            security: "is_granted('commercial.clients.view') or is_granted('commercial.suppliers.view')",
            normalizationContext: ['groups' => ['country:read']],
        ),
    ],
    security: "is_granted('commercial.clients.view') or is_granted('commercial.suppliers.view')",
)]
#[ORM\Entity(repositoryClass: DoctrineCountryRepository::class)]
#[ORM\Table(name: 'country')]
#[ORM\UniqueConstraint(name: 'uq_country_code', columns: ['code'])]
class Country
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    #[Groups(['country:read'])]
    private ?int $id = null;

    #[ORM\Column(length: 2)]
    #[Groups(['country:read'])]
    private ?string $code = null;

    #[ORM\Column(length: 80)]
    #[Groups(['country:read'])]
    private ?string $name = null;

    #[ORM\Column(options: ['default' => 0])]
    #[Groups(['country:read'])]
    private int $position = 0;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getCode(): ?string
    {
        return $this->code;
    }

    public function setCode(string $code): static
    {
        $this->code = $code;

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): static
    {
        $this->name = $name;

        return $this;
    }

    public function getPosition(): int
    {
        return $this->position;
    }

    public function setPosition(int $position): static
    {
        $this->position = $position;

        return $this;
    }
}
