using HoneyBox.Model.Models.Auth;
using HoneyBox.Model.Models.Payment;

namespace HoneyBox.Core.Interfaces;

/// <summary>
/// 微信服务接口
/// </summary>
public interface IWechatService
{
    /// <summary>
    /// 获取微信openid和unionid
    /// </summary>
    /// <param name="code">微信授权code</param>
    /// <returns>微信认证结果</returns>
    Task<WechatAuthResult> GetOpenIdAsync(string code);

    /// <summary>
    /// 获取微信授权的手机号
    /// </summary>
    /// <param name="code">微信授权code</param>
    /// <returns>微信手机号结果</returns>
    Task<WechatMobileResult> GetMobileAsync(string code);

    /// <summary>
    /// 获取小程序接口调用凭证（access_token）
    /// </summary>
    /// <param name="appId">小程序AppId（可选，不传则使用默认配置）</param>
    /// <returns>access_token，失败返回null</returns>
    Task<string?> GetAccessTokenAsync(string? appId = null);

    /// <summary>
    /// 创建支付订单（原生微信支付）
    /// </summary>
    /// <param name="request">支付请求</param>
    /// <returns>支付结果，包含前端调用 uni.requestPayment 所需的参数</returns>
    Task<CreatePayResult> CreatePayOrderAsync(CreatePayRequest request);

    /// <summary>
    /// 获取小程序码（无限制）
    /// 调用微信API getwxacodeunlimit 生成小程序码
    /// </summary>
    /// <param name="scene">场景值，最大32个可见字符</param>
    /// <param name="page">小程序页面路径（可选）</param>
    /// <param name="width">二维码宽度，默认430</param>
    /// <returns>小程序码图片字节数组，失败返回null</returns>
    Task<byte[]?> GetWxaCodeUnlimitAsync(string scene, string? page = null, int width = 430);

    /// <summary>
    /// 上传微信小程序发货信息
    /// 调用微信API upload_shipping_info 将订单标记为已发货
    /// </summary>
    /// <param name="request">发货信息请求</param>
    /// <returns>发货结果</returns>
    Task<WechatShippingResult> UploadShippingInfoAsync(WechatShippingRequest request);
}

/// <summary>
/// 创建支付请求
/// </summary>
public class CreatePayRequest
{
    /// <summary>
    /// 用户ID
    /// </summary>
    public int UserId { get; set; }

    /// <summary>
    /// 用户OpenId
    /// </summary>
    public string OpenId { get; set; } = string.Empty;

    /// <summary>
    /// 支付金额（元）
    /// </summary>
    public decimal Price { get; set; }

    /// <summary>
    /// 商品标题
    /// </summary>
    public string Title { get; set; } = string.Empty;

    /// <summary>
    /// 附加数据（订单类型）
    /// </summary>
    public string Attach { get; set; } = string.Empty;

    /// <summary>
    /// 订单号前缀
    /// </summary>
    public string Prefix { get; set; } = "MH_";
}

/// <summary>
/// 创建支付结果
/// </summary>
public class CreatePayResult
{
    /// <summary>
    /// 状态：1=成功，0=失败
    /// </summary>
    public int Status { get; set; }

    /// <summary>
    /// 错误消息
    /// </summary>
    public string? Message { get; set; }

    /// <summary>
    /// 订单号
    /// </summary>
    public string OrderNo { get; set; } = string.Empty;

    /// <summary>
    /// 支付参数（返回给前端，用于调用 uni.requestPayment）
    /// </summary>
    public NativePayParams? Res { get; set; }
}

/// <summary>
/// 原生微信支付参数（用于 uni.requestPayment）
/// </summary>
public class NativePayParams
{
    /// <summary>
    /// 小程序AppId
    /// </summary>
    public string AppId { get; set; } = string.Empty;

    /// <summary>
    /// 时间戳（秒）
    /// </summary>
    public string TimeStamp { get; set; } = string.Empty;

    /// <summary>
    /// 随机字符串
    /// </summary>
    public string NonceStr { get; set; } = string.Empty;

    /// <summary>
    /// 统一下单接口返回的 prepay_id（格式：prepay_id=xxx）
    /// </summary>
    public string Package { get; set; } = string.Empty;

    /// <summary>
    /// 签名类型
    /// </summary>
    public string SignType { get; set; } = "MD5";

    /// <summary>
    /// 签名
    /// </summary>
    public string PaySign { get; set; } = string.Empty;
}

/// <summary>
/// Web支付参数（客服消息支付，备用）
/// </summary>
public class WebPayParams
{
    /// <summary>
    /// 支付数据
    /// </summary>
    public WebPayData? Data { get; set; }

    /// <summary>
    /// 请求支付URL
    /// </summary>
    public string RequestPay { get; set; } = string.Empty;

    /// <summary>
    /// 提示信息
    /// </summary>
    public string Tips { get; set; } = string.Empty;
}

/// <summary>
/// Web支付数据
/// </summary>
public class WebPayData
{
    /// <summary>
    /// 订单号
    /// </summary>
    public string OrderNum { get; set; } = string.Empty;
}
