<?php

namespace Media\Entities;

use App\Framework\Database\Attributes\Entity;

#[Entity(tableName: 'images')]
class Image
{
    public function __construct(
        public ?int $id = null,
        public string $filename = '',
        public string $originalFilename = '',
        public string $mimeType = '',
        public int $fileSize = 0,
        public int $width = 0,
        public int $height = 0,
        public string $hash = '',
        public string $uploadPath = '',
        public \DateTimeImmutable $createdAt = new \DateTimeImmutable(),
        public ?\DateTimeImmutable $updatedAt = null,
    ) {}

    public function getUploadDirectory(): string
    {
        return sprintf(
            '/uploads/%s/%s/%s',
            $this->createdAt->format('Y'),
            $this->createdAt->format('m'),
            $this->createdAt->format('d')
        );
    }

    public function getFilePathPattern(): string
    {
        $idStr = str_pad((string)$this->id, 9, '0', STR_PAD_LEFT);
        return sprintf(
            '%s/%s/%s/%s',
            substr($idStr, 0, 3),
            substr($idStr, 3, 3),
            substr($idStr, 6, 3),
            $idStr
        );
    }

    /**
     * Generiert eine eindeutige URL-freundliche ID ohne Slashes
     */
    public function getUrlId(): string
    {
        // Kombiniert Datum und ID zu einem eindeutigen String
        $dateStr = $this->createdAt->format('Ymd');
        $idStr = str_pad((string)$this->id, 8, '0', STR_PAD_LEFT);
        return $dateStr . $idStr;
    }

}
