<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Doctrine\Orm\Filter\ExistsFilter;
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\Enum\DocumentType;
use App\Repository\DocumentRepository;
use App\State\DocumentUploadProcessor;
use DateTimeImmutable;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;

#[ORM\Entity(repositoryClass: DocumentRepository::class)]
#[ORM\Table(name: 'documents')]
#[ORM\HasLifecycleCallbacks]
#[ApiFilter(SearchFilter::class, properties: ['name' => 'ipartial', 'filename' => 'ipartial'])]
#[ApiFilter(ExistsFilter::class, properties: ['site', 'machine', 'composant', 'piece', 'product', 'comment'])]
#[ApiFilter(OrderFilter::class, properties: ['createdAt', 'name', 'size'])]
#[ApiResource(
    description: 'Documents et fichiers. Gestion des fichiers joints (PDF, images, etc.) rattachés aux machines, pièces, composants, produits ou sites. Upload via multipart/form-data.',
    operations: [
        new GetCollection(
            security: "is_granted('ROLE_VIEWER')",
            normalizationContext: ['groups' => ['document:list']],
        ),
        new Get(
            security: "is_granted('ROLE_VIEWER')",
            normalizationContext: ['groups' => ['document:list', 'document:detail']],
        ),
        new Post(
            security: "is_granted('ROLE_GESTIONNAIRE')",
            processor: DocumentUploadProcessor::class,
            deserialize: false,
            inputFormats: ['multipart' => ['multipart/form-data']],
        ),
        new Put(security: "is_granted('ROLE_GESTIONNAIRE')"),
        new Patch(security: "is_granted('ROLE_GESTIONNAIRE')"),
        new Delete(security: "is_granted('ROLE_GESTIONNAIRE')"),
    ],
    paginationClientItemsPerPage: true,
    paginationMaximumItemsPerPage: 500,
    order: ['createdAt' => 'DESC']
)]
class Document
{
    use CuidEntityTrait;

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

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

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

    #[ORM\Column(type: Types::TEXT)]
    private string $path;

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

    #[ORM\Column(type: Types::INTEGER)]
    #[Groups(['document:list', 'document:read', 'composant:read', 'piece:read', 'product:read'])]
    private int $size;

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

    #[ORM\ManyToOne(targetEntity: Composant::class, inversedBy: 'documents')]
    #[ORM\JoinColumn(name: 'composantId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    #[Groups(['document:list'])]
    private ?Composant $composant = null;

    #[ORM\ManyToOne(targetEntity: Piece::class, inversedBy: 'documents')]
    #[ORM\JoinColumn(name: 'pieceId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    #[Groups(['document:list'])]
    private ?Piece $piece = null;

    #[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'documents')]
    #[ORM\JoinColumn(name: 'productId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    #[Groups(['document:list'])]
    private ?Product $product = null;

    #[ORM\ManyToOne(targetEntity: Site::class, inversedBy: 'documents')]
    #[ORM\JoinColumn(name: 'siteId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    #[Groups(['document:list'])]
    private ?Site $site = null;

    #[ORM\ManyToOne(targetEntity: Comment::class, inversedBy: 'documents')]
    #[ORM\JoinColumn(name: 'comment_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    #[Groups(['document:list'])]
    private ?Comment $comment = null;

    #[ORM\Column(type: Types::STRING, length: 20, enumType: DocumentType::class)]
    #[Groups(['document:list', 'document:read', 'composant:read', 'piece:read', 'product:read'])]
    private DocumentType $type = DocumentType::DOCUMENTATION;

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'createdAt')]
    #[Groups(['document:list'])]
    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();
    }

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

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

        return $this;
    }

    public function getFilename(): string
    {
        return $this->filename;
    }

    public function setFilename(string $filename): static
    {
        $this->filename = $filename;

        return $this;
    }

    public function getPath(): string
    {
        return $this->path;
    }

    public function setPath(string $path): static
    {
        $this->path = $path;

        return $this;
    }

    public function getMimeType(): string
    {
        return $this->mimeType;
    }

    public function setMimeType(string $mimeType): static
    {
        $this->mimeType = $mimeType;

        return $this;
    }

    public function getSize(): int
    {
        return $this->size;
    }

    public function setSize(int $size): static
    {
        $this->size = $size;

        return $this;
    }

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

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

        return $this;
    }

    public function getComposant(): ?Composant
    {
        return $this->composant;
    }

    public function setComposant(?Composant $composant): static
    {
        $this->composant = $composant;

        return $this;
    }

    public function getPiece(): ?Piece
    {
        return $this->piece;
    }

    public function setPiece(?Piece $piece): static
    {
        $this->piece = $piece;

        return $this;
    }

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

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

        return $this;
    }

    public function getSite(): ?Site
    {
        return $this->site;
    }

    public function setSite(?Site $site): static
    {
        $this->site = $site;

        return $this;
    }

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

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

        return $this;
    }

    public function getComment(): ?Comment
    {
        return $this->comment;
    }

    public function setComment(?Comment $comment): static
    {
        $this->comment = $comment;

        return $this;
    }
}
