<?php

namespace App\Entity;

use App\Repository\ScanAttachmentRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

/**
 * Document libre joint à un inventaire ("Autres documents").
 * Le fichier est stocké hors DocumentRoot (cf. AttachmentStorage) ; seules les métadonnées sont en base.
 */
#[ORM\Entity(repositoryClass: ScanAttachmentRepository::class)]
#[ORM\Table(name: 'scan_attachment')]
#[ORM\Index(name: 'idx_attachment_scan', columns: ['scan_id'])]
class ScanAttachment
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: ScanSession::class, inversedBy: 'attachments')]
    #[ORM\JoinColumn(name: 'scan_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private ScanSession $scan;

    /** Nom de fichier sur le disque ({uuid}.{ext}). */
    #[ORM\Column(length: 64)]
    private string $storedName;

    #[ORM\Column(length: 255)]
    private string $originalName;

    #[ORM\Column(length: 128)]
    private string $mimeType;

    #[ORM\Column]
    private int $size = 0;

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
    private \DateTimeImmutable $uploadedAt;

    #[ORM\Column(length: 180)]
    private string $uploadedByEmail;

    /** Commentaire associé à ce document précis. */
    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $comment = null;

    public function __construct(ScanSession $scan, string $storedName, string $originalName, string $mimeType, int $size, string $uploadedByEmail)
    {
        $this->scan = $scan;
        $this->storedName = $storedName;
        $this->originalName = $originalName;
        $this->mimeType = $mimeType;
        $this->size = $size;
        $this->uploadedByEmail = $uploadedByEmail;
        $this->uploadedAt = new \DateTimeImmutable();
    }

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

    public function getScan(): ScanSession
    {
        return $this->scan;
    }

    public function setScan(ScanSession $scan): static
    {
        $this->scan = $scan;

        return $this;
    }

    public function getStoredName(): string
    {
        return $this->storedName;
    }

    public function getOriginalName(): string
    {
        return $this->originalName;
    }

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

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

    public function getUploadedAt(): \DateTimeImmutable
    {
        return $this->uploadedAt;
    }

    public function getUploadedByEmail(): string
    {
        return $this->uploadedByEmail;
    }

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

    public function setComment(?string $comment): static
    {
        $comment = null === $comment ? null : trim($comment);
        $this->comment = '' === $comment ? null : $comment;

        return $this;
    }
}
