<?php

namespace App\Framework\Http;

final readonly class UploadedFile
{
    public function __construct(
        public string $name,
        public string $type,
        public int $size,
        public string $tmpName,
        public UploadError $error,
    ) {
        if($this->isValid() === false) {
            throw new \InvalidArgumentException("Invalid uploaded file: {$this->name}");
        }
    }

    public function getExtension(): ?string
    {
        return pathinfo($this->name, PATHINFO_EXTENSION) ?: null;
    }

    public function getFileName(): string
    {
        return pathinfo($this->name, PATHINFO_FILENAME);
    }

    private function isValid(): bool
    {
        return $this->error === UploadError::OK && is_uploaded_file($this->tmpName);
    }

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