<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use App\Repository\MachineProductLinkRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: MachineProductLinkRepository::class)]
#[ORM\Table(name: 'machine_product_links')]
#[ORM\HasLifecycleCallbacks]
#[ApiResource]
class MachineProductLink
{
    #[ORM\Id]
    #[ORM\Column(type: Types::STRING, length: 36)]
    private ?string $id = null;

    #[ORM\ManyToOne(targetEntity: Machine::class, inversedBy: 'productLinks')]
    #[ORM\JoinColumn(name: 'machineId', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private Machine $machine;

    #[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'machineLinks')]
    #[ORM\JoinColumn(name: 'productId', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private Product $product;

    #[ORM\ManyToOne(targetEntity: TypeMachineProductRequirement::class, inversedBy: 'machineProductLinks')]
    #[ORM\JoinColumn(name: 'typeMachineProductRequirementId', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private ?TypeMachineProductRequirement $typeMachineProductRequirement = null;

    #[ORM\ManyToOne(targetEntity: MachineProductLink::class, inversedBy: 'childLinks')]
    #[ORM\JoinColumn(name: 'parentLinkId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    private ?MachineProductLink $parentLink = null;

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

    #[ORM\ManyToOne(targetEntity: MachineComponentLink::class, inversedBy: 'productLinks')]
    #[ORM\JoinColumn(name: 'parentComponentLinkId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    private ?MachineComponentLink $parentComponentLink = null;

    #[ORM\ManyToOne(targetEntity: MachinePieceLink::class, inversedBy: 'productLinks')]
    #[ORM\JoinColumn(name: 'parentPieceLinkId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    private ?MachinePieceLink $parentPieceLink = null;

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

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

    public function __construct()
    {
        $this->childLinks = new ArrayCollection();
    }

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

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

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

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

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

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

        return $this;
    }

    public function getMachine(): Machine
    {
        return $this->machine;
    }

    public function setMachine(Machine $machine): static
    {
        $this->machine = $machine;

        return $this;
    }

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

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

        return $this;
    }

    public function getTypeMachineProductRequirement(): ?TypeMachineProductRequirement
    {
        return $this->typeMachineProductRequirement;
    }

    public function setTypeMachineProductRequirement(?TypeMachineProductRequirement $requirement): static
    {
        $this->typeMachineProductRequirement = $requirement;

        return $this;
    }

    public function getParentLink(): ?MachineProductLink
    {
        return $this->parentLink;
    }

    public function setParentLink(?MachineProductLink $parentLink): static
    {
        $this->parentLink = $parentLink;

        return $this;
    }

    public function getParentComponentLink(): ?MachineComponentLink
    {
        return $this->parentComponentLink;
    }

    public function setParentComponentLink(?MachineComponentLink $parentComponentLink): static
    {
        $this->parentComponentLink = $parentComponentLink;

        return $this;
    }

    public function getParentPieceLink(): ?MachinePieceLink
    {
        return $this->parentPieceLink;
    }

    public function setParentPieceLink(?MachinePieceLink $parentPieceLink): static
    {
        $this->parentPieceLink = $parentPieceLink;

        return $this;
    }
}
