<?php

declare(strict_types=1);

namespace App\Module\Transport\Domain\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Module\Transport\Infrastructure\ApiPlatform\State\Processor\CarrierContactProcessor;
use App\Shared\Domain\Attribute\Auditable;
use App\Shared\Domain\Contract\BlamableInterface;
use App\Shared\Domain\Contract\TimestampableInterface;
use App\Shared\Domain\Trait\TimestampableBlamableTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Contact d'un transporteur (1:n) — onglet Contact (M4). Jumeau de
 * SupplierContact (M2) : au moins un champ rempli (RG-4.08, garanti par le
 * CHECK chk_carrier_contact_filled + le Processor), max 2 telephones.
 *
 * Lecture : proprietes en `carrier:item:read` (embarquees au detail du
 * transporteur). Ecriture : groupe `carrier:write:contacts`.
 *
 * Sous-ressource API (ERP-160, spec § 4.5) — jumelle de SupplierContact (M2) :
 *  - POST /api/carriers/{carrierId}/contacts : creation rattachee au
 *    transporteur parent (Link toProperty 'carrier'), security
 *    transport.carriers.manage.
 *  - PATCH / DELETE /api/carrier_contacts/{id} : security
 *    transport.carriers.manage.
 *  - GET /api/carrier_contacts/{id} : lecture unitaire (security view) — la
 *    lecture courante reste via le parent. Pas de GET collection autonome.
 * Tout passe par le CarrierContactProcessor (rattachement parent + RG-4.08 +
 * RG-4.13).
 *
 * Telephones (RG-4.08, max 2) : le contrat d'ecriture expose un tableau virtuel
 * `phones` (liste dynamique cote front « x1, +1 possible, max 2 ») mappe par le
 * Processor vers `phonePrimary` / `phoneSecondary` (un 3e numero -> 422). Les
 * deux colonnes scalaires restent en lecture seule (embarquees au detail).
 *
 * Audite (#[Auditable]) + Timestampable / Blamable.
 */
#[ApiResource(
    operations: [
        new Get(
            security: "is_granted('transport.carriers.view')",
            normalizationContext: ['groups' => ['carrier:item:read', 'default:read']],
        ),
        new Post(
            uriTemplate: '/carriers/{carrierId}/contacts',
            uriVariables: [
                'carrierId' => new Link(fromClass: Carrier::class, toProperty: 'carrier'),
            ],
            // read:false : pas de stade lecture du parent. Le Link toProperty
            // resoudrait l'enfant (SELECT CarrierContact ... WHERE carrier = :id)
            // et casse en NonUniqueResult des >= 2 enfants. Le parent est rattache
            // manuellement par CarrierContactProcessor::linkParent (404 si absent).
            read: false,
            security: "is_granted('transport.carriers.manage')",
            normalizationContext: ['groups' => ['carrier:item:read', 'default:read']],
            denormalizationContext: ['groups' => ['carrier:write:contacts']],
            processor: CarrierContactProcessor::class,
        ),
        new Patch(
            security: "is_granted('transport.carriers.manage')",
            normalizationContext: ['groups' => ['carrier:item:read', 'default:read']],
            denormalizationContext: ['groups' => ['carrier:write:contacts']],
            processor: CarrierContactProcessor::class,
        ),
        new Delete(
            security: "is_granted('transport.carriers.manage')",
            processor: CarrierContactProcessor::class,
        ),
    ],
)]
#[ORM\Entity]
#[ORM\Table(name: 'carrier_contact')]
#[ORM\Index(name: 'idx_carrier_contact_carrier', columns: ['carrier_id'])]
#[ORM\Index(name: 'idx_carrier_contact_created_by', columns: ['created_by'])]
#[ORM\Index(name: 'idx_carrier_contact_updated_by', columns: ['updated_by'])]
#[Auditable]
class CarrierContact implements TimestampableInterface, BlamableInterface
{
    use TimestampableBlamableTrait;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    #[Groups(['carrier:item:read'])]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: Carrier::class, inversedBy: 'contacts')]
    #[ORM\JoinColumn(name: 'carrier_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private ?Carrier $carrier = null;

    // RG-4.08 : aucun champ obligatoire isolement (≥ 1 champ rempli, garde
    // Processor + CHECK BDD). Les colonnes restent nullable au niveau ORM.
    #[ORM\Column(name: 'first_name', length: 120, nullable: true)]
    #[Assert\Length(max: 120, maxMessage: 'Le prénom ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
    #[Groups(['carrier:item:read', 'carrier:write:contacts'])]
    private ?string $firstName = null;

    #[ORM\Column(name: 'last_name', length: 120, nullable: true)]
    #[Assert\Length(max: 120, maxMessage: 'Le nom ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
    #[Groups(['carrier:item:read', 'carrier:write:contacts'])]
    private ?string $lastName = null;

    #[ORM\Column(name: 'job_title', length: 120, nullable: true)]
    #[Assert\Length(max: 120, maxMessage: 'La fonction ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
    #[Groups(['carrier:item:read', 'carrier:write:contacts'])]
    private ?string $jobTitle = null;

    // Telephones en LECTURE seule : alimentes en ecriture via le tableau virtuel
    // `phones` (mappe par le CarrierContactProcessor). Pas de groupe write -> pas
    // de saisie directe (et donc exemptes du miroir Assert\Length, le Processor
    // borne deja la longueur).
    #[ORM\Column(name: 'phone_primary', length: 20, nullable: true)]
    #[Groups(['carrier:item:read'])]
    private ?string $phonePrimary = null;

    #[ORM\Column(name: 'phone_secondary', length: 20, nullable: true)]
    #[Groups(['carrier:item:read'])]
    private ?string $phoneSecondary = null;

    #[ORM\Column(length: 180, nullable: true)]
    #[Assert\Email(message: 'L\'adresse email n\'est pas valide.')]
    #[Assert\Length(max: 180, maxMessage: 'L\'email ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
    #[Groups(['carrier:item:read', 'carrier:write:contacts'])]
    private ?string $email = null;

    /**
     * Telephones en ecriture (RG-4.08, max 2), NON persiste : le
     * CarrierContactProcessor normalise chaque numero (RG-4.13) puis le mappe vers
     * phonePrimary / phoneSecondary. null = non fourni (PATCH partiel : on ne
     * touche pas aux telephones existants). Un 3e numero -> 422 sur `phones`.
     *
     * @var null|list<string>
     */
    #[Groups(['carrier:write:contacts'])]
    private ?array $phones = null;

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

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

    public function getCarrier(): ?Carrier
    {
        return $this->carrier;
    }

    public function setCarrier(?Carrier $carrier): static
    {
        $this->carrier = $carrier;

        return $this;
    }

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }

    public function setFirstName(?string $firstName): static
    {
        $this->firstName = $firstName;

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->lastName;
    }

    public function setLastName(?string $lastName): static
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getJobTitle(): ?string
    {
        return $this->jobTitle;
    }

    public function setJobTitle(?string $jobTitle): static
    {
        $this->jobTitle = $jobTitle;

        return $this;
    }

    public function getPhonePrimary(): ?string
    {
        return $this->phonePrimary;
    }

    public function setPhonePrimary(?string $phonePrimary): static
    {
        $this->phonePrimary = $phonePrimary;

        return $this;
    }

    public function getPhoneSecondary(): ?string
    {
        return $this->phoneSecondary;
    }

    public function setPhoneSecondary(?string $phoneSecondary): static
    {
        $this->phoneSecondary = $phoneSecondary;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(?string $email): static
    {
        $this->email = $email;

        return $this;
    }

    /**
     * @return null|list<string>
     */
    public function getPhones(): ?array
    {
        return $this->phones;
    }

    /**
     * @param null|list<string> $phones
     */
    public function setPhones(?array $phones): static
    {
        $this->phones = $phones;

        return $this;
    }

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

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

        return $this;
    }
}
