namespace Tiku.Api.Middleware;

internal sealed record ExceptionProblemDetailsMapping(
    string Title,
    int Status,
    string Code,
    IReadOnlyDictionary<string, object?> Extensions)
{
    public ExceptionProblemDetailsMapping(string title, int status, string code)
        : this(title, status, code, new Dictionary<string, object?>())
    {
    }
}

internal interface IExceptionProblemDetailsMapper
{
    IReadOnlyCollection<Type> HandledExceptionTypes { get; }

    bool TryMap(Exception exception, out ExceptionProblemDetailsMapping mapping);
}

internal abstract class ExceptionProblemDetailsMapper : IExceptionProblemDetailsMapper
{
    public abstract IReadOnlyCollection<Type> HandledExceptionTypes { get; }

    public abstract bool TryMap(Exception exception, out ExceptionProblemDetailsMapping mapping);

    protected static bool Mapped(
        Exception exception,
        int status,
        string code,
        out ExceptionProblemDetailsMapping mapping)
    {
        mapping = new ExceptionProblemDetailsMapping(exception.Message, status, code);
        return true;
    }

    protected static bool NotMapped(out ExceptionProblemDetailsMapping mapping)
    {
        mapping = null!;
        return false;
    }
}
