<?php

declare(strict_types=1);

namespace App\Framework\Http;

enum UploadError: int
{
    case OK = 0;
    case INI_SIZE = 1;
    case FORM_SIZE = 2;
    case PARTIAL = 3;
    case NO_FILE = 4;
    case NO_TMP_DIR = 6;
    case CANT_WRITE = 7;
    case EXTENSION = 8;

    public static function fromUploadError(int $error): self
    {
        return match ($error) {
            UPLOAD_ERR_OK => self::OK,
            UPLOAD_ERR_INI_SIZE => self::INI_SIZE,
            UPLOAD_ERR_FORM_SIZE => self::FORM_SIZE,
            UPLOAD_ERR_PARTIAL => self::PARTIAL,
            UPLOAD_ERR_NO_FILE => self::NO_FILE,
            UPLOAD_ERR_NO_TMP_DIR => self::NO_TMP_DIR,
            UPLOAD_ERR_CANT_WRITE => self::CANT_WRITE,
            UPLOAD_ERR_EXTENSION => self::EXTENSION,
            default => self::NO_FILE,
        };
    }

    public function getMessage(): string
    {
        return match ($this) {
            self::OK => 'Upload erfolgreich',
            self::INI_SIZE => 'Datei überschreitet upload_max_filesize',
            self::FORM_SIZE => 'Datei überschreitet MAX_FILE_SIZE des HTML-Formulars',
            self::PARTIAL => 'Datei wurde nur teilweise hochgeladen',
            self::NO_FILE => 'Keine Datei hochgeladen',
            self::NO_TMP_DIR => 'Temporärer Ordner fehlt',
            self::CANT_WRITE => 'Fehler beim Schreiben auf Festplatte',
            self::EXTENSION => 'Upload durch PHP-Erweiterung gestoppt',
        };
    }

    public function isError(): bool
    {
        return $this !== self::OK;
    }
}
