<?php

declare(strict_types=1);

class FileUpload
{
    private const IMAGE_MAX = 5 * 1024 * 1024;
    private const VIDEO_MAX = 500 * 1024 * 1024;

    private const IMAGE_TYPES = [
        'image/jpeg' => 'jpg', 'image/png' => 'png',
        'image/gif' => 'gif', 'image/webp' => 'webp',
    ];

    private const VIDEO_TYPES = [
        'video/mp4' => 'mp4', 'video/webm' => 'webm', 'video/ogg' => 'ogg',
    ];

    public static function image(array $file, string $subdir = 'thumbnails'): array
    {
        if (!isset($file['tmp_name']) || $file['error'] !== UPLOAD_ERR_OK) {
            return ['success' => false, 'path' => '', 'error' => 'Upload error.'];
        }

        if ($file['size'] > self::IMAGE_MAX) {
            return ['success' => false, 'path' => '', 'error' => 'Image must not exceed 5 MB.'];
        }

        $mime = Mime::fromFile($file['tmp_name'], $file['name'] ?? null);
        if (!$mime || !isset(self::IMAGE_TYPES[$mime])) {
            return ['success' => false, 'path' => '', 'error' => 'Format not allowed (JPG, PNG, GIF, WebP).'];
        }

        $ext = self::IMAGE_TYPES[$mime];
        $name = self::generateName($ext);
        $dir = APP_ROOT . '/public/uploads/' . $subdir;

        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }

        if (!move_uploaded_file($file['tmp_name'], $dir . '/' . $name)) {
            return ['success' => false, 'path' => '', 'error' => 'Unable to move the file.'];
        }

        return ['success' => true, 'path' => $name, 'error' => ''];
    }

    public static function video(array $file): array
    {
        if (!isset($file['tmp_name']) || $file['error'] !== UPLOAD_ERR_OK) {
            return ['success' => false, 'path' => '', 'error' => 'Upload error.'];
        }

        if ($file['size'] > self::VIDEO_MAX) {
            return ['success' => false, 'path' => '', 'error' => 'Video must not exceed 500 MB.'];
        }

        $mime = Mime::fromFile($file['tmp_name'], $file['name'] ?? null);
        if (!$mime || !isset(self::VIDEO_TYPES[$mime])) {
            return ['success' => false, 'path' => '', 'error' => 'Format not allowed (MP4, WebM, OGG).'];
        }

        $ext = self::VIDEO_TYPES[$mime];
        $name = self::generateName($ext);
        $dir = APP_ROOT . '/public/uploads/videos';

        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }

        if (!move_uploaded_file($file['tmp_name'], $dir . '/' . $name)) {
            return ['success' => false, 'path' => '', 'error' => 'Unable to move the file.'];
        }

        return ['success' => true, 'path' => $name, 'error' => ''];
    }

    public static function delete(string $path): bool
    {
        if ($path === '') {
            return false;
        }
        $uploadsDir = APP_ROOT . '/public/uploads';
        $full = realpath($uploadsDir . '/' . $path);
         
        if ($full === false || !str_starts_with($full, realpath($uploadsDir) . '/')) {
            return false;
        }
        if (is_file($full)) {
            return unlink($full);
        }
        return false;
    }

    private static function generateName(string $ext): string
    {
        return time() . '_' . bin2hex(random_bytes(8)) . '.' . $ext;
    }
}
