<?php

declare(strict_types=1);

namespace App\Framework\Filesystem\Exceptions;

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

/**
 * Exception wenn ein Serializer nicht gefunden wurde
 */
final class SerializerNotFoundException extends FilesystemException
{
    public static function byName(string $name): self
    {
        $context = ExceptionContext::forOperation('serializer.lookup', 'SerializerRegistry')
            ->withData(['serializer_name' => $name]);

        return self::fromContext(
            "Serializer '{$name}' not found in registry",
            $context,
            FilesystemErrorCode::SERIALIZER_NOT_FOUND
        );
    }

    public static function byExtension(string $extension): self
    {
        $context = ExceptionContext::forOperation('serializer.lookup.extension', 'SerializerRegistry')
            ->withData(['file_extension' => $extension]);

        return self::fromContext(
            "No serializer found for extension '.{$extension}'",
            $context,
            FilesystemErrorCode::SERIALIZER_NOT_FOUND
        );
    }

    public static function byMimeType(string $mimeType): self
    {
        $context = ExceptionContext::forOperation('serializer.lookup.mimetype', 'SerializerRegistry')
            ->withData(['mime_type' => $mimeType]);

        return self::fromContext(
            "No serializer found for MIME type '{$mimeType}'",
            $context,
            FilesystemErrorCode::SERIALIZER_NOT_FOUND
        );
    }

    public static function noExtensionInPath(string $path): self
    {
        $context = ExceptionContext::forOperation('serializer.detect', 'SerializerRegistry')
            ->withData(['path' => $path]);

        return self::fromContext(
            "Cannot detect serializer: No file extension in path '{$path}'",
            $context,
            FilesystemErrorCode::SERIALIZER_NOT_FOUND
        );
    }

    public static function noDefault(): self
    {
        $context = ExceptionContext::forOperation('serializer.get_default', 'SerializerRegistry');

        return self::fromContext(
            'No default serializer configured in registry',
            $context,
            FilesystemErrorCode::SERIALIZER_NOT_FOUND
        );
    }
}
