<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\TypeMachineComponentRequirementRepository;
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\Annotation\Groups;

#[ORM\Entity(repositoryClass: TypeMachineComponentRequirementRepository::class)]
#[ORM\Table(name: 'type_machine_component_requirements')]
#[ORM\HasLifecycleCallbacks]
#[ApiResource]
class TypeMachineComponentRequirement
{
    #[ORM\Id]
    #[ORM\Column(type: Types::STRING, length: 36)]
    #[Groups(['type_machine:read'])]
    private ?string $id = null;

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

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

    #[ORM\Column(type: Types::INTEGER, nullable: true, name: 'maxCount')]
    #[Groups(['type_machine:read'])]
    private ?int $maxCount = null;

    #[ORM\Column(type: Types::BOOLEAN, options: ['default' => true], name: 'required')]
    #[Groups(['type_machine:read'])]
    private bool $required = true;

    #[ORM\Column(type: Types::BOOLEAN, options: ['default' => true], name: 'allowNewModels')]
    #[Groups(['type_machine:read'])]
    private bool $allowNewModels = true;

    #[ORM\Column(type: Types::INTEGER, options: ['default' => 0], name: 'orderIndex')]
    #[Groups(['type_machine:read'])]
    private int $orderIndex = 0;

    #[ORM\ManyToOne(targetEntity: TypeMachine::class, inversedBy: 'componentRequirements')]
    #[ORM\JoinColumn(name: 'typeMachineId', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private TypeMachine $typeMachine;

    #[ORM\ManyToOne(targetEntity: ModelType::class, inversedBy: 'componentRequirements')]
    #[ORM\JoinColumn(name: 'typeComposantId', referencedColumnName: 'id', nullable: false)]
    #[ApiProperty(readableLink: true)]
    #[Groups(['type_machine:read'])]
    private ModelType $typeComposant;

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

    #[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->machineComponentLinks = 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 getLabel(): ?string
    {
        return $this->label;
    }

    public function setLabel(?string $label): static
    {
        $this->label = $label;

        return $this;
    }

    public function getMinCount(): int
    {
        return $this->minCount;
    }

    public function setMinCount(int $minCount): static
    {
        $this->minCount = $minCount;

        return $this;
    }

    public function getMaxCount(): ?int
    {
        return $this->maxCount;
    }

    public function setMaxCount(?int $maxCount): static
    {
        $this->maxCount = $maxCount;

        return $this;
    }

    public function isRequired(): bool
    {
        return $this->required;
    }

    public function setRequired(bool $required): static
    {
        $this->required = $required;

        return $this;
    }

    public function isAllowNewModels(): bool
    {
        return $this->allowNewModels;
    }

    public function setAllowNewModels(bool $allowNewModels): static
    {
        $this->allowNewModels = $allowNewModels;

        return $this;
    }

    public function getOrderIndex(): int
    {
        return $this->orderIndex;
    }

    public function setOrderIndex(int $orderIndex): static
    {
        $this->orderIndex = $orderIndex;

        return $this;
    }

    public function getTypeMachine(): TypeMachine
    {
        return $this->typeMachine;
    }

    public function setTypeMachine(TypeMachine $typeMachine): static
    {
        $this->typeMachine = $typeMachine;

        return $this;
    }

    public function getTypeComposant(): ModelType
    {
        return $this->typeComposant;
    }

    public function setTypeComposant(ModelType $typeComposant): static
    {
        $this->typeComposant = $typeComposant;

        return $this;
    }
}
