<?php

declare(strict_types=1);

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,
        private bool $skipValidation = false
    ) {
        if (! $this->skipValidation && $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);
    }

    /**
     * Create an UploadedFile for testing without validation
     */
    public static function createForTesting(
        string $name,
        string $type,
        int $size,
        string $tmpName,
        UploadError $error = UploadError::OK
    ): self {
        return new self($name, $type, $size, $tmpName, $error, skipValidation: true);
    }
}
