using Microsoft.AspNetCore.Http;

namespace Hua.Todo.Application.CloudSync.Models;

/// <summary>
/// 云同步 API 错误响应生成器。
/// </summary>
public static class CloudApiErrors
{
    /// <summary>
    /// 生成标准错误响应。
    /// </summary>
    /// <param name="statusCode">HTTP 状态码。</param>
    /// <param name="code">业务错误码。</param>
    /// <param name="message">错误消息。</param>
    /// <returns>最小 API 结果。</returns>
    public static IResult Error(int statusCode, string code, string message)
    {
        return Results.Json(
            new ApiErrorResponse { Code = code, Message = message },
            statusCode: statusCode);
    }

    /// <summary>
    /// 未认证。
    /// </summary>
    public static IResult Unauthorized(string message = "Unauthorized.")
        => Error(StatusCodes.Status401Unauthorized, "UNAUTHORIZED", message);

    /// <summary>
    /// 权限不足。
    /// </summary>
    public static IResult Forbidden(string message = "Forbidden.")
        => Error(StatusCodes.Status403Forbidden, "FORBIDDEN", message);

    /// <summary>
    /// 需要二次认证（step-up）。
    /// </summary>
    public static IResult SecondFactorRequired(string message = "Second factor required.")
        => Error(StatusCodes.Status403Forbidden, "SECOND_FACTOR_REQUIRED", message);

    /// <summary>
    /// 请求非法。
    /// </summary>
    public static IResult BadRequest(string message = "Bad request.")
        => Error(StatusCodes.Status400BadRequest, "BAD_REQUEST", message);

    /// <summary>
    /// 资源不存在。
    /// </summary>
    public static IResult NotFound(string message = "Not found.")
        => Error(StatusCodes.Status404NotFound, "NOT_FOUND", message);
}

