<?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\ProductRepository;
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\Component\Serializer\Attribute\Groups;

#[ORM\Entity(repositoryClass: ProductRepository::class)]
#[ORM\Table(name: 'products')]
#[ORM\HasLifecycleCallbacks]
#[ApiFilter(SearchFilter::class, properties: ['name' => 'ipartial', 'reference' => 'ipartial', 'typeProduct' => 'exact', 'typeProduct.name' => 'ipartial'])]
#[ApiFilter(MultiSearchFilter::class, properties: ['name', 'reference'])]
#[ApiFilter(OrderFilter::class, properties: ['name', 'createdAt', 'supplierPrice'])]
#[ApiResource(
    description: 'Produits du catalogue fournisseur. Un produit possède une référence, un prix indicatif, un type, des fournisseurs et des documents. Il peut être lié à des machines.',
    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' => ['product:read']],
    paginationClientItemsPerPage: true,
    paginationMaximumItemsPerPage: 200
)]
class Product
{
    use CuidEntityTrait;

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

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

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

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

    #[ORM\ManyToOne(targetEntity: ModelType::class, inversedBy: 'products')]
    #[ORM\JoinColumn(name: 'typeProductId', referencedColumnName: 'id', nullable: true)]
    #[Groups(['product:read'])]
    private ?ModelType $typeProduct = null;

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

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

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

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

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

    /**
     * @var Collection<int, Piece>
     */
    #[ORM\ManyToMany(targetEntity: Piece::class, mappedBy: 'products')]
    private Collection $linkedPieces;

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

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

    private bool $skipAudit = false;

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

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'updatedAt')]
    #[Groups(['product: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->pieces            = new ArrayCollection();
        $this->composants        = new ArrayCollection();
        $this->linkedPieces      = 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 getSupplierPrice(): ?string
    {
        return $this->supplierPrice;
    }

    public function setSupplierPrice(?string $supplierPrice): static
    {
        $this->supplierPrice = $supplierPrice;

        return $this;
    }

    public function getTypeProduct(): ?ModelType
    {
        return $this->typeProduct;
    }

    public function setTypeProduct(?ModelType $typeProduct): static
    {
        $this->typeProduct = $typeProduct;

        return $this;
    }

    /**
     * @return Collection<int, ProductConstructeurLink>
     */
    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, Piece>
     */
    public function getLinkedPieces(): Collection
    {
        return $this->linkedPieces;
    }

    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;
    }
}
