using Microsoft.AspNetCore.Http.HttpResults;
using RpgRoller.Contracts;
using RpgRoller.Services;

namespace RpgRoller.Api;

internal static class ApiResultMapper
{
    public static Results<Ok<T>, BadRequest<ApiError>, UnauthorizedHttpResult> ToApiResult<T>(ServiceResult<T> result)
    {
        if (result.Succeeded)
            return TypedResults.Ok(result.Value!);

        if (result.Error!.Code == "unauthorized")
            return TypedResults.Unauthorized();

        return TypedResults.BadRequest(new ApiError(result.Error.Message, result.Error.Code));
    }

    public static BadRequest<ApiError> ToBadRequest(ServiceError error)
    {
        return TypedResults.BadRequest(new ApiError(error.Message, error.Code));
    }
}