using System.Text.Json.Serialization;

namespace HoneyBox.Model.Base;

/// <summary>
/// 统一响应基类（兼容PHP API格式）
/// </summary>
public class ApiResponse
{
    /// <summary>
    /// 状态码：1=成功，0=失败，-1=未登录，-9=需要绑定手机号
    /// </summary>
    [JsonPropertyName("status")]
    public int Status { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    [JsonPropertyName("msg")]
    public string Msg { get; set; } = string.Empty;

    public static ApiResponse Success(string msg = "success")
        => new() { Status = 1, Msg = msg };

    public static ApiResponse Fail(string msg, int status = 0)
        => new() { Status = status, Msg = msg };

    /// <summary>
    /// 未登录响应
    /// </summary>
    public static ApiResponse Unauthorized(string msg = "未登录")
        => new() { Status = -1, Msg = msg };
}

/// <summary>
/// 带数据的统一响应
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public class ApiResponse<T> : ApiResponse
{
    /// <summary>
    /// 响应数据
    /// </summary>
    [JsonPropertyName("data")]
    public T? Data { get; set; }

    public static ApiResponse<T> Success(T data, string msg = "success")
        => new() { Status = 1, Msg = msg, Data = data };

    public new static ApiResponse<T> Fail(string msg, int status = 0)
        => new() { Status = status, Msg = msg };

    /// <summary>
    /// 未登录响应
    /// </summary>
    public new static ApiResponse<T> Unauthorized(string msg = "未登录")
        => new() { Status = -1, Msg = msg };
}
