<?php

declare(strict_types=1);

namespace App\Framework\Filesystem\Exceptions;

use App\Framework\Exception\Core\FilesystemErrorCode;
use App\Framework\Exception\ExceptionContext;

/**
 * Exception für File-Validation Errors
 *
 * Wird geworfen wenn Dateipfade, Extensions oder Filegrößen
 * die Validierungsregeln nicht erfüllen
 */
final class FileValidationException extends FilesystemException
{
    /**
     * Factory für Path Validation Error
     */
    public static function invalidPath(
        string $path,
        string $reason,
        FilesystemErrorCode $errorCode = FilesystemErrorCode::INVALID_PATH
    ): self {
        return self::forPath(
            path: $path,
            message: $reason,
            errorCode: $errorCode,
            operation: 'file.validation.path'
        );
    }

    /**
     * Factory für Extension Validation Error
     */
    public static function invalidExtension(
        string $path,
        string $extension,
        array $allowedExtensions
    ): self {
        $context = ExceptionContext::forOperation(
            'file.validation.extension',
            'FileValidator'
        )->withData([
            'path' => $path,
            'extension' => $extension,
            'allowed_extensions' => $allowedExtensions,
        ]);

        return self::fromContext(
            "File extension '.{$extension}' is not allowed",
            $context,
            FilesystemErrorCode::INVALID_PATH
        );
    }

    /**
     * Factory für Blocked Extension Error
     */
    public static function blockedExtension(
        string $path,
        string $extension,
        array $blockedExtensions
    ): self {
        $context = ExceptionContext::forOperation(
            'file.validation.extension',
            'FileValidator'
        )->withData([
            'path' => $path,
            'extension' => $extension,
            'blocked_extensions' => $blockedExtensions,
        ]);

        return self::fromContext(
            "File extension '.{$extension}' is blocked",
            $context,
            FilesystemErrorCode::INVALID_PATH
        );
    }

    /**
     * Factory für FileSize Validation Error
     */
    public static function fileSizeExceeded(
        string $fileSizeHuman,
        string $maxSizeHuman,
        int $fileSizeBytes,
        int $maxSizeBytes
    ): self {
        $context = ExceptionContext::forOperation(
            'file.validation.size',
            'FileValidator'
        )->withData([
            'file_size' => $fileSizeHuman,
            'max_size' => $maxSizeHuman,
            'file_size_bytes' => $fileSizeBytes,
            'max_size_bytes' => $maxSizeBytes,
        ]);

        return self::fromContext(
            'File size exceeds maximum allowed size',
            $context,
            FilesystemErrorCode::INVALID_PATH
        );
    }
}
