using Tiku.Api.Middleware;
using Tiku.Application.Jobs;

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

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

    public override bool TryMap(Exception exception, out ExceptionProblemDetailsMapping mapping)
    {
        if (exception is not BackgroundJobException job) return NotMapped(out mapping);
        var status = job.Code switch
        {
            "background_job_not_found" => StatusCodes.Status404NotFound,
            "background_job_not_cancellable" or "background_job_not_retryable" => StatusCodes.Status409Conflict,
            _ => StatusCodes.Status400BadRequest
        };
        return Mapped(job, status, job.Code, out mapping);
    }
}
