<?php

declare(strict_types=1);

namespace App\Entity;

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\Repository\CustomFieldRepository;
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: CustomFieldRepository::class)]
#[ORM\Table(name: 'custom_fields')]
#[ORM\HasLifecycleCallbacks]
#[ApiResource(
    description: 'Définitions de champs personnalisés. Permet de créer des champs dynamiques (texte, nombre, date, etc.) applicables aux machines, pièces, composants et produits.',
    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')"),
    ]
)]
class CustomField
{
    use CuidEntityTrait;

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

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

    #[ORM\Column(type: Types::STRING, length: 50)]
    #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])]
    private string $type;

    #[ORM\Column(type: Types::BOOLEAN, options: ['default' => false])]
    #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])]
    private bool $required = false;

    #[ORM\Column(type: Types::BOOLEAN, options: ['default' => false], name: 'machinecontextonly')]
    #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])]
    private bool $machineContextOnly = false;

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

    #[ORM\Column(type: Types::JSON, nullable: true)]
    #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])]
    private ?array $options = null;

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

    #[ORM\ManyToOne(targetEntity: Machine::class, inversedBy: 'customFields')]
    #[ORM\JoinColumn(name: 'machineId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    private ?Machine $machine = null;

    #[ORM\ManyToOne(targetEntity: ModelType::class, inversedBy: 'customFields')]
    #[ORM\JoinColumn(name: 'typeComposantId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    private ?ModelType $typeComposant = null;

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

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

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

    #[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->createdAt         = new DateTimeImmutable();
        $this->updatedAt         = new DateTimeImmutable();
        $this->customFieldValues = new ArrayCollection();
    }

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

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

        return $this;
    }

    public function getType(): string
    {
        return $this->type;
    }

    public function setType(string $type): static
    {
        $this->type = $type;

        return $this;
    }

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

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

        return $this;
    }

    public function getDefaultValue(): ?string
    {
        return $this->defaultValue;
    }

    public function setDefaultValue(?string $defaultValue): static
    {
        $this->defaultValue = $defaultValue;

        return $this;
    }

    public function getOptions(): ?array
    {
        return $this->options;
    }

    public function setOptions(?array $options): static
    {
        $this->options = $options;

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

    public function isMachineContextOnly(): bool
    {
        return $this->machineContextOnly;
    }

    public function setMachineContextOnly(bool $machineContextOnly): static
    {
        $this->machineContextOnly = $machineContextOnly;

        return $this;
    }
}
