<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Entity\Trait\CuidEntityTrait;
use App\Filter\MultiSearchFilter;
use App\Repository\PieceRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Groups;

#[UniqueEntity(fields: ['reference'], message: 'Une pièce avec cette référence existe déjà.')]
#[ORM\Entity(repositoryClass: PieceRepository::class)]
#[ORM\Table(name: 'pieces')]
#[ORM\HasLifecycleCallbacks]
#[ApiFilter(SearchFilter::class, properties: ['name' => 'ipartial', 'reference' => 'ipartial', 'typePiece' => 'exact', 'typePiece.name' => 'ipartial'])]
#[ApiFilter(MultiSearchFilter::class, properties: ['name', 'reference'])]
#[ApiFilter(OrderFilter::class, properties: ['name', 'createdAt'])]
#[ApiResource(
    description: 'Pièces détachées du catalogue. Une pièce peut être rattachée à plusieurs machines et possède un type, des fournisseurs, des documents et un produit associé.',
    operations: [
        new Get(security: "is_granted('ROLE_VIEWER')"),
        new GetCollection(security: "is_granted('ROLE_VIEWER')"),
        new Post(security: "is_granted('ROLE_GESTIONNAIRE')"),
        new Put(security: "is_granted('ROLE_GESTIONNAIRE')"),
        new Patch(security: "is_granted('ROLE_GESTIONNAIRE')"),
        new Delete(security: "is_granted('ROLE_GESTIONNAIRE')"),
    ],
    normalizationContext: ['groups' => ['piece:read']],
    paginationClientItemsPerPage: true,
    paginationMaximumItemsPerPage: 200
)]
class Piece
{
    use CuidEntityTrait;

    #[ORM\Id]
    #[ORM\Column(type: Types::STRING, length: 36)]
    #[Groups(['piece:read', 'document:list'])]
    private ?string $id = null;

    #[ORM\Column(type: Types::STRING, length: 255)]
    #[Groups(['piece:read', 'document:list'])]
    private string $name;

    #[ORM\Column(type: Types::STRING, length: 255, unique: true, nullable: true)]
    #[Groups(['piece:read'])]
    private ?string $reference = null;

    #[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
    #[Groups(['piece:read'])]
    private ?string $referenceAuto = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    #[Groups(['piece:read'])]
    private ?string $description = null;

    #[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
    #[Groups(['piece:read'])]
    private ?string $prix = null;

    #[ORM\ManyToOne(targetEntity: ModelType::class, inversedBy: 'pieces')]
    #[ORM\JoinColumn(name: 'typePieceId', referencedColumnName: 'id', nullable: true)]
    #[Groups(['piece:read'])]
    private ?ModelType $typePiece = null;

    #[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'pieces')]
    #[ORM\JoinColumn(name: 'productId', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    #[Groups(['piece:read'])]
    private ?Product $product = null;

    /**
     * @var Collection<int, PieceConstructeurLink>
     */
    #[ORM\OneToMany(mappedBy: 'piece', targetEntity: PieceConstructeurLink::class, cascade: ['remove'])]
    #[Groups(['piece:read'])]
    private Collection $constructeurLinks;

    /**
     * @var Collection<int, Document>
     */
    #[ORM\OneToMany(mappedBy: 'piece', targetEntity: Document::class)]
    #[Groups(['piece:read'])]
    private Collection $documents;

    /**
     * @var Collection<int, CustomFieldValue>
     */
    #[ORM\OneToMany(mappedBy: 'piece', targetEntity: CustomFieldValue::class)]
    #[Groups(['piece:read'])]
    private Collection $customFieldValues;

    /**
     * @var Collection<int, Product>
     */
    #[ORM\ManyToMany(targetEntity: Product::class, inversedBy: 'linkedPieces')]
    #[ORM\JoinTable(name: 'piece_products')]
    #[ORM\JoinColumn(name: 'piece_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
    #[ORM\InverseJoinColumn(name: 'product_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
    private Collection $products;

    /**
     * @var Collection<int, PieceProductSlot>
     */
    #[ORM\OneToMany(targetEntity: PieceProductSlot::class, mappedBy: 'piece', cascade: ['persist', 'remove'], orphanRemoval: true)]
    #[ORM\OrderBy(['position' => 'ASC'])]
    private Collection $productSlots;

    /**
     * @var Collection<int, MachinePieceLink>
     */
    #[ORM\OneToMany(mappedBy: 'piece', targetEntity: MachinePieceLink::class)]
    private Collection $machineLinks;

    #[ORM\Column(type: Types::INTEGER, options: ['default' => 1])]
    #[Groups(['piece:read'])]
    private int $version = 1;

    private bool $skipAudit = false;

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'createdAt')]
    #[Groups(['piece:read'])]
    private DateTimeImmutable $createdAt;

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'updatedAt')]
    #[Groups(['piece:read'])]
    private DateTimeImmutable $updatedAt;

    public function __construct()
    {
        $this->createdAt         = new DateTimeImmutable();
        $this->updatedAt         = new DateTimeImmutable();
        $this->constructeurLinks = new ArrayCollection();
        $this->documents         = new ArrayCollection();
        $this->customFieldValues = new ArrayCollection();
        $this->products          = new ArrayCollection();
        $this->productSlots      = new ArrayCollection();
        $this->machineLinks      = new ArrayCollection();
    }

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

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

        return $this;
    }

    public function getReference(): ?string
    {
        return $this->reference;
    }

    public function setReference(?string $reference): static
    {
        $this->reference = $reference;

        return $this;
    }

    public function getReferenceAuto(): ?string
    {
        return $this->referenceAuto;
    }

    /**
     * @internal used by ReferenceAutoSubscriber only — not part of the public API
     */
    public function setReferenceAuto(?string $referenceAuto): static
    {
        $this->referenceAuto = $referenceAuto;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): static
    {
        $this->description = $description;

        return $this;
    }

    public function getPrix(): ?string
    {
        return $this->prix;
    }

    public function setPrix(?string $prix): static
    {
        $this->prix = $prix;

        return $this;
    }

    public function getTypePiece(): ?ModelType
    {
        return $this->typePiece;
    }

    public function setTypePiece(?ModelType $typePiece): static
    {
        $this->typePiece = $typePiece;

        return $this;
    }

    public function getProduct(): ?Product
    {
        return $this->product;
    }

    public function setProduct(?Product $product): static
    {
        $this->product = $product;

        if (null !== $product) {
            $this->addProduct($product);
        }

        return $this;
    }

    /**
     * @return string[]
     */
    #[Groups(['piece:read'])]
    public function getProductIds(): array
    {
        return $this->products->map(fn (Product $p) => $p->getId())->toArray();
    }

    /**
     * @return Collection<int, PieceConstructeurLink>
     */
    public function getConstructeurLinks(): Collection
    {
        return $this->constructeurLinks;
    }

    /**
     * @return Collection<int, Document>
     */
    public function getDocuments(): Collection
    {
        return $this->documents;
    }

    /**
     * @return Collection<int, CustomFieldValue>
     */
    public function getCustomFieldValues(): Collection
    {
        return $this->customFieldValues;
    }

    /**
     * @return Collection<int, Product>
     */
    public function getProducts(): Collection
    {
        return $this->products;
    }

    public function addProduct(Product $product): static
    {
        if (!$this->products->contains($product)) {
            $this->products->add($product);
        }

        return $this;
    }

    public function removeProduct(Product $product): static
    {
        $this->products->removeElement($product);

        return $this;
    }

    /**
     * @return Collection<int, PieceProductSlot>
     */
    public function getProductSlots(): Collection
    {
        return $this->productSlots;
    }

    public function addProductSlot(PieceProductSlot $slot): static
    {
        if (!$this->productSlots->contains($slot)) {
            $this->productSlots->add($slot);
            $slot->setPiece($this);
        }

        return $this;
    }

    public function removeProductSlot(PieceProductSlot $slot): static
    {
        $this->productSlots->removeElement($slot);

        return $this;
    }

    public function getVersion(): int
    {
        return $this->version;
    }

    public function incrementVersion(): static
    {
        ++$this->version;

        return $this;
    }

    public function getSkipAudit(): bool
    {
        return $this->skipAudit;
    }

    public function setSkipAudit(bool $skipAudit): static
    {
        $this->skipAudit = $skipAudit;

        return $this;
    }
}
