using Tiku.Api.Middleware;
using Tiku.Application.TenantAdmin;

namespace Tiku.Api.Modules.Tenant.Management.Errors;

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

    public override bool TryMap(Exception exception, out ExceptionProblemDetailsMapping mapping)
    {
        if (exception is not TenantAdminDirectException tenantAdmin) return NotMapped(out mapping);
        var status = tenantAdmin.Code switch
        {
            "tenant_admin_access_denied" => StatusCodes.Status403Forbidden,
            "class_not_found" or "class_member_not_found" or "student_not_found" or "user_not_found" or
                "tenant_member_not_found" => StatusCodes.Status404NotFound,
            _ when tenantAdmin.Code.EndsWith("_not_found", StringComparison.Ordinal) =>
                StatusCodes.Status404NotFound,
            _ => StatusCodes.Status400BadRequest
        };
        return Mapped(tenantAdmin, status, tenantAdmin.Code, out mapping);
    }
}
