<?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 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'])]
#[ApiFilter(OrderFilter::class, properties: ['name', 'createdAt'])]
#[ApiResource(
    normalizationContext: ['groups' => ['product:read']],
    paginationClientItemsPerPage: true,
    paginationMaximumItemsPerPage: 500
)]
class Product
{
    #[ORM\Id]
    #[ORM\Column(type: Types::STRING, length: 36)]
    #[Groups(['product:read'])]
    private ?string $id = null;

    #[ORM\Column(type: Types::STRING, length: 255, unique: true)]
    #[Groups(['product:read'])]
    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, Constructeur>
     */
    #[ORM\ManyToMany(targetEntity: Constructeur::class, inversedBy: 'products')]
    #[ORM\JoinTable(
        name: '_ProductConstructeurs',
        joinColumns: [new ORM\JoinColumn(name: 'A', referencedColumnName: 'id', onDelete: 'CASCADE')],
        inverseJoinColumns: [new ORM\InverseJoinColumn(name: 'B', referencedColumnName: 'id', onDelete: 'CASCADE')]
    )]
    #[Groups(['product:read'])]
    private Collection $constructeurs;

    /**
     * @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, MachineProductLink>
     */
    #[ORM\OneToMany(mappedBy: 'product', targetEntity: MachineProductLink::class)]
    private Collection $machineLinks;

    #[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->constructeurs     = new ArrayCollection();
        $this->documents         = new ArrayCollection();
        $this->customFieldValues = new ArrayCollection();
        $this->pieces            = new ArrayCollection();
        $this->composants        = new ArrayCollection();
        $this->machineLinks      = new ArrayCollection();
    }

    #[ORM\PrePersist]
    public function setCreatedAtValue(): void
    {
        $now             = new DateTimeImmutable();
        $this->createdAt = $now;
        $this->updatedAt = $now;

        if (null === $this->id) {
            $this->id = $this->generateCuid();
        }
    }

    #[ORM\PreUpdate]
    public function setUpdatedAtValue(): void
    {
        $this->updatedAt = new DateTimeImmutable();
    }

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

    public function setId(string $id): static
    {
        $this->id = $id;

        return $this;
    }

    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, Constructeur>
     */
    public function getConstructeurs(): Collection
    {
        return $this->constructeurs;
    }

    public function addConstructeur(Constructeur $constructeur): static
    {
        if (!$this->constructeurs->contains($constructeur)) {
            $this->constructeurs->add($constructeur);
        }

        return $this;
    }

    public function removeConstructeur(Constructeur $constructeur): static
    {
        $this->constructeurs->removeElement($constructeur);

        return $this;
    }

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

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

    public function getCreatedAt(): DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function getUpdatedAt(): DateTimeImmutable
    {
        return $this->updatedAt;
    }

    private function generateCuid(): string
    {
        return 'cl'.bin2hex(random_bytes(12));
    }
}
