using Tiku.Api.Middleware;
using Tiku.Application.Storage;

namespace Tiku.Api.Modules.System.Storage.Errors;

internal sealed class StorageExceptionProblemDetailsMapper : ExceptionProblemDetailsMapper
{
    public override IReadOnlyCollection<Type> HandledExceptionTypes { get; } = [typeof(ObjectStorageException)];

    public override bool TryMap(Exception exception, out ExceptionProblemDetailsMapping mapping)
    {
        if (exception is not ObjectStorageException storage) return NotMapped(out mapping);
        return Mapped(storage,
            storage is ObjectStorageNotConfiguredException
                ? StatusCodes.Status503ServiceUnavailable
                : StatusCodes.Status400BadRequest,
            storage.Code.ToLowerInvariant(),
            out mapping);
    }
}
