<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Uid\Uuid;

/**
 * A file that arrived with an inbound email reply (a host or parent attached a
 * PDF/DOCX/… to their answer). The bytes live in the tenant-isolated S3
 * `inbound_attachments` storage; this row keeps the validated metadata and the
 * storage path, and is served to admins only via a forced download.
 *
 * Everything here has already passed {@see \Ferienpass\CoreBundle\Comment\InboundAttachmentPolicy}:
 * `mimeType`/`name`/`size` are the sanitized, trusted values, not the sender's claims.
 */
#[ORM\Entity]
#[ORM\Index(columns: ['conversation_item_id'])]
class ConversationItemAttachment implements \Stringable
{
    #[ORM\Id]
    #[ORM\Column(type: UuidType::NAME)]
    private Uuid $uuid;

    #[ORM\ManyToOne(targetEntity: ConversationItem::class, inversedBy: 'attachments')]
    #[ORM\JoinColumn(name: 'conversation_item_id', referencedColumnName: 'uuid', onDelete: 'CASCADE')]
    private ConversationItem $conversationItem;

    /** Path within the `inbound_attachments` virtual filesystem (never sender-controlled). */
    #[ORM\Column(type: 'string', length: 255)]
    private string $path;

    /** Sanitized original filename, used as the download filename and label. */
    #[ORM\Column(type: 'string', length: 255)]
    private string $name;

    /** Canonical MIME type, derived from the verified content (not the claimed one). */
    #[ORM\Column(type: 'string', length: 128)]
    private string $mimeType;

    #[ORM\Column(type: 'integer', options: ['unsigned' => true])]
    private int $size;

    #[ORM\Column(type: 'datetime_immutable', options: ['default' => 'CURRENT_TIMESTAMP'])]
    private \DateTimeInterface $createdAt;

    public function __construct(ConversationItem $conversationItem, string $path, string $name, string $mimeType, int $size)
    {
        $this->uuid = Uuid::v7();
        $this->conversationItem = $conversationItem;
        $this->path = $path;
        $this->name = $name;
        $this->mimeType = $mimeType;
        $this->size = $size;
        $this->createdAt = new \DateTimeImmutable();
    }

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

    public function getUuid(): Uuid
    {
        return $this->uuid;
    }

    public function getConversationItem(): ConversationItem
    {
        return $this->conversationItem;
    }

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

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

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

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

    public function getCreatedAt(): \DateTimeInterface
    {
        return $this->createdAt;
    }
}
