using System;

namespace CncModels.Dto
{
    /// <summary>
    /// 统一API响应包装类
    /// 前端约定：code=0 表示成功，非0表示失败
    /// </summary>
    /// <typeparam name="T">业务数据类型</typeparam>
    public class ApiResponse<T>
    {
        /// <summary>错误码，0=成功</summary>
        public int Code { get; set; }

        /// <summary>提示信息</summary>
        public string Message { get; set; }

        /// <summary>业务数据</summary>
        public T Data { get; set; }

        /// <summary>
        /// 构建成功响应
        /// </summary>
        /// <param name="data">业务数据</param>
        /// <param name="message">提示信息，默认 "success"</param>
        /// <returns>成功响应</returns>
        public static ApiResponse<T> Success(T data, string message = "success")
        {
            return new ApiResponse<T>
            {
                Code = 0,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 构建失败响应
        /// </summary>
        /// <param name="code">错误码（非0）</param>
        /// <param name="message">错误信息</param>
        /// <returns>失败响应</returns>
        public static ApiResponse<T> Fail(int code, string message)
        {
            return new ApiResponse<T>
            {
                Code = code,
                Message = message,
                Data = default(T)
            };
        }
    }
}
