using System.Text.Json.Serialization;

namespace HoneyBox.Model.Models.Payment;

#region Wechat Pay Request/Response Models

/// <summary>
/// 微信支付请求
/// </summary>
public class WechatPayRequest
{
    /// <summary>
    /// 订单号
    /// </summary>
    [JsonPropertyName("order_no")]
    public string OrderNo { get; set; } = string.Empty;

    /// <summary>
    /// 支付金额（单位：元）
    /// </summary>
    [JsonPropertyName("amount")]
    public decimal Amount { get; set; }

    /// <summary>
    /// 商品描述
    /// </summary>
    [JsonPropertyName("body")]
    public string Body { get; set; } = string.Empty;

    /// <summary>
    /// 附加数据（用于区分订单类型）
    /// </summary>
    [JsonPropertyName("attach")]
    public string Attach { get; set; } = string.Empty;

    /// <summary>
    /// 用户OpenId
    /// </summary>
    [JsonPropertyName("open_id")]
    public string OpenId { get; set; } = string.Empty;

    /// <summary>
    /// 用户ID
    /// </summary>
    [JsonPropertyName("user_id")]
    public int UserId { get; set; }
}

/// <summary>
/// 微信支付结果
/// </summary>
public class WechatPayResult
{
    /// <summary>
    /// 状态：1=成功，0=失败
    /// </summary>
    [JsonPropertyName("status")]
    public int Status { get; set; }

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

    /// <summary>
    /// 支付数据
    /// </summary>
    [JsonPropertyName("data")]
    public WechatPayData? Data { get; set; }
}

/// <summary>
/// 微信支付数据（返回给前端调起支付）
/// </summary>
public class WechatPayData
{
    /// <summary>
    /// 应用ID
    /// </summary>
    [JsonPropertyName("appId")]
    public string AppId { get; set; } = string.Empty;

    /// <summary>
    /// 时间戳
    /// </summary>
    [JsonPropertyName("timeStamp")]
    public string TimeStamp { get; set; } = string.Empty;

    /// <summary>
    /// 随机字符串
    /// </summary>
    [JsonPropertyName("nonceStr")]
    public string NonceStr { get; set; } = string.Empty;

    /// <summary>
    /// 预支付交易会话标识
    /// </summary>
    [JsonPropertyName("package")]
    public string Package { get; set; } = string.Empty;

    /// <summary>
    /// 签名类型
    /// </summary>
    [JsonPropertyName("signType")]
    public string SignType { get; set; } = "MD5";

    /// <summary>
    /// 签名
    /// </summary>
    [JsonPropertyName("paySign")]
    public string PaySign { get; set; } = string.Empty;

    /// <summary>
    /// 是否微信环境：1=是，0=否
    /// </summary>
    [JsonPropertyName("is_weixin")]
    public int IsWeixin { get; set; }
}

#endregion

#region Payment Notify Models

/// <summary>
/// 支付回调通知结果
/// </summary>
public class NotifyResult
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    public string Message { get; set; } = string.Empty;

    /// <summary>
    /// XML响应内容（返回给微信 V2）
    /// </summary>
    public string XmlResponse { get; set; } = string.Empty;

    /// <summary>
    /// JSON响应内容（返回给微信 V3）
    /// </summary>
    public string JsonResponse { get; set; } = string.Empty;
}

/// <summary>
/// 微信支付回调数据
/// </summary>
public class WechatNotifyData
{
    /// <summary>
    /// 返回状态码
    /// </summary>
    public string ReturnCode { get; set; } = string.Empty;

    /// <summary>
    /// 返回信息
    /// </summary>
    public string ReturnMsg { get; set; } = string.Empty;

    /// <summary>
    /// 业务结果
    /// </summary>
    public string ResultCode { get; set; } = string.Empty;

    /// <summary>
    /// 错误代码
    /// </summary>
    public string ErrCode { get; set; } = string.Empty;

    /// <summary>
    /// 错误代码描述
    /// </summary>
    public string ErrCodeDes { get; set; } = string.Empty;

    /// <summary>
    /// 应用ID
    /// </summary>
    public string AppId { get; set; } = string.Empty;

    /// <summary>
    /// 商户号
    /// </summary>
    public string MchId { get; set; } = string.Empty;

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

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

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

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

    /// <summary>
    /// 交易类型
    /// </summary>
    public string TradeType { get; set; } = string.Empty;

    /// <summary>
    /// 付款银行
    /// </summary>
    public string BankType { get; set; } = string.Empty;

    /// <summary>
    /// 订单金额（单位：分）
    /// </summary>
    public int TotalFee { get; set; }

    /// <summary>
    /// 货币种类
    /// </summary>
    public string FeeType { get; set; } = string.Empty;

    /// <summary>
    /// 现金支付金额（单位：分）
    /// </summary>
    public int CashFee { get; set; }

    /// <summary>
    /// 微信支付订单号
    /// </summary>
    public string TransactionId { get; set; } = string.Empty;

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

    /// <summary>
    /// 附加数据
    /// </summary>
    public string Attach { get; set; } = string.Empty;

    /// <summary>
    /// 支付完成时间
    /// </summary>
    public string TimeEnd { get; set; } = string.Empty;
}

#endregion


#region Payment Record Models

/// <summary>
/// 支付记录DTO
/// </summary>
public class PaymentRecordDto
{
    /// <summary>
    /// 记录ID
    /// </summary>
    [JsonPropertyName("id")]
    public int Id { get; set; }

    /// <summary>
    /// 用户ID
    /// </summary>
    [JsonPropertyName("user_id")]
    public int UserId { get; set; }

    /// <summary>
    /// 订单号
    /// </summary>
    [JsonPropertyName("order_num")]
    public string OrderNum { get; set; } = string.Empty;

    /// <summary>
    /// 支付金额
    /// </summary>
    [JsonPropertyName("change_money")]
    public string ChangeMoney { get; set; } = "0.00";

    /// <summary>
    /// 支付说明
    /// </summary>
    [JsonPropertyName("content")]
    public string Content { get; set; } = string.Empty;

    /// <summary>
    /// 支付类型：1=微信支付，2=余额支付，3=积分支付，4=哈尼券支付
    /// </summary>
    [JsonPropertyName("pay_type")]
    public int PayType { get; set; }

    /// <summary>
    /// 支付类型文本
    /// </summary>
    [JsonPropertyName("pay_type_text")]
    public string PayTypeText { get; set; } = string.Empty;

    /// <summary>
    /// 创建时间戳
    /// </summary>
    [JsonPropertyName("created_at")]
    public long CreatedAt { get; set; }
}

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

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

    /// <summary>
    /// 支付金额
    /// </summary>
    public decimal Amount { get; set; }

    /// <summary>
    /// 支付类型
    /// </summary>
    public int PayType { get; set; }

    /// <summary>
    /// 支付说明
    /// </summary>
    public string Content { get; set; } = string.Empty;
}

#endregion

#region Asset Deduction Models

/// <summary>
/// 资产扣减请求
/// </summary>
public class AssetDeductionRequest
{
    /// <summary>
    /// 用户ID
    /// </summary>
    public int UserId { get; set; }

    /// <summary>
    /// 扣减金额
    /// </summary>
    public decimal Amount { get; set; }

    /// <summary>
    /// 扣减说明
    /// </summary>
    public string Content { get; set; } = string.Empty;

    /// <summary>
    /// 关联订单号
    /// </summary>
    public string? OrderNum { get; set; }
}

/// <summary>
/// 资产扣减结果
/// </summary>
public class AssetDeductionResult
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    public string Message { get; set; } = string.Empty;

    /// <summary>
    /// 扣减后余额
    /// </summary>
    public decimal RemainingBalance { get; set; }
}

#endregion

#region Wechat Pay Configuration

/// <summary>
/// 微信支付商户配置
/// </summary>
public class WechatPayMerchantConfig
{
    /// <summary>
    /// 商户名称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 商户ID
    /// </summary>
    public string MchId { get; set; } = string.Empty;

    /// <summary>
    /// 应用ID
    /// </summary>
    public string AppId { get; set; } = string.Empty;

    /// <summary>
    /// 商户密钥（V2版本使用）
    /// </summary>
    public string Key { get; set; } = string.Empty;

    /// <summary>
    /// 订单前缀（用于匹配商户，3位字符）
    /// </summary>
    public string OrderPrefix { get; set; } = string.Empty;

    /// <summary>
    /// 权重（用于负载均衡）
    /// </summary>
    public int Weight { get; set; } = 1;

    /// <summary>
    /// 回调通知URL
    /// </summary>
    public string NotifyUrl { get; set; } = string.Empty;

    // ===== V3 新增字段 =====

    /// <summary>
    /// 支付版本: "V2" 或 "V3"，默认 "V2"
    /// </summary>
    public string PayVersion { get; set; } = "V2";

    /// <summary>
    /// APIv3 密钥（32位字符串，V3版本使用）
    /// </summary>
    public string? ApiV3Key { get; set; }

    /// <summary>
    /// 商户API证书序列号（V3版本使用）
    /// </summary>
    public string? CertSerialNo { get; set; }

    /// <summary>
    /// 商户私钥文件路径（V3版本使用）
    /// </summary>
    public string? PrivateKeyPath { get; set; }

    /// <summary>
    /// 微信支付公钥ID（V3版本使用）
    /// </summary>
    public string? WechatPublicKeyId { get; set; }

    /// <summary>
    /// 微信支付公钥文件路径（V3版本使用）
    /// </summary>
    public string? WechatPublicKeyPath { get; set; }
}

/// <summary>
/// 小程序配置
/// </summary>
public class MiniprogramConfig
{
    /// <summary>
    /// 小程序名称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 小程序AppId
    /// </summary>
    public string AppId { get; set; } = string.Empty;

    /// <summary>
    /// 小程序AppSecret
    /// </summary>
    public string AppSecret { get; set; } = string.Empty;

    /// <summary>
    /// 订单前缀（2位字符）
    /// </summary>
    public string OrderPrefix { get; set; } = string.Empty;

    /// <summary>
    /// 关联的商户ID列表
    /// </summary>
    public List<string> Merchants { get; set; } = new();

    /// <summary>
    /// 关联的域名列表（逗号分隔）
    /// </summary>
    public string Domain { get; set; } = string.Empty;

    /// <summary>
    /// 是否为默认小程序
    /// </summary>
    public bool IsDefault { get; set; }
}

/// <summary>
/// 微信支付配置
/// </summary>
public class WechatPaySettings
{
    /// <summary>
    /// 默认商户配置
    /// </summary>
    public WechatPayMerchantConfig DefaultMerchant { get; set; } = new();

    /// <summary>
    /// 多商户配置列表
    /// </summary>
    public List<WechatPayMerchantConfig> Merchants { get; set; } = new();

    /// <summary>
    /// 小程序配置列表
    /// </summary>
    public List<MiniprogramConfig> Miniprograms { get; set; } = new();

    /// <summary>
    /// 统一下单API地址
    /// </summary>
    public string UnifiedOrderUrl { get; set; } = "https://api.mch.weixin.qq.com/pay/unifiedorder";

    /// <summary>
    /// 发货通知API地址
    /// </summary>
    public string ShippingNotifyUrl { get; set; } = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info";

    /// <summary>
    /// 默认回调通知URL基础地址
    /// </summary>
    public string NotifyBaseUrl { get; set; } = string.Empty;
}

/// <summary>
/// 订单前缀信息
/// </summary>
public class OrderPrefixInfo
{
    /// <summary>
    /// 商户前缀（3位字符）
    /// </summary>
    public string? MerchantPrefix { get; set; }

    /// <summary>
    /// 小程序前缀（2位字符）
    /// </summary>
    public string? MiniprogramPrefix { get; set; }
}

#endregion

#region Order Shipping Notify Models

/// <summary>
/// 订单发货通知请求
/// </summary>
public class OrderShippingNotifyRequest
{
    /// <summary>
    /// 订单号
    /// </summary>
    public string OrderNo { get; set; } = string.Empty;

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

    /// <summary>
    /// 物流公司编码
    /// </summary>
    public string? LogisticsCompany { get; set; }

    /// <summary>
    /// 物流单号
    /// </summary>
    public string? TrackingNo { get; set; }

    /// <summary>
    /// 商品描述（可选，默认根据订单类型自动生成）
    /// </summary>
    public string? ItemDesc { get; set; }

    /// <summary>
    /// 物流类型：1=实体物流，2=同城配送，3=虚拟商品，4=用户自提
    /// </summary>
    public int LogisticsType { get; set; } = 4;

    /// <summary>
    /// 发货模式：1=统一发货，2=分拆发货
    /// </summary>
    public int DeliveryMode { get; set; } = 1;
}

/// <summary>
/// 订单发货通知结果
/// </summary>
public class OrderShippingNotifyResult
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 错误码
    /// </summary>
    public int ErrCode { get; set; }

    /// <summary>
    /// 错误消息
    /// </summary>
    public string ErrMsg { get; set; } = string.Empty;

    /// <summary>
    /// 是否已存入重试队列
    /// </summary>
    public bool QueuedForRetry { get; set; }
}

/// <summary>
/// 发货失败订单数据（存储在Redis中用于重试）
/// </summary>
public class FailedShippingOrderData
{
    /// <summary>
    /// 用户OpenId
    /// </summary>
    public string OpenId { get; set; } = string.Empty;

    /// <summary>
    /// 应用ID
    /// </summary>
    public string AppId { get; set; } = string.Empty;

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

    /// <summary>
    /// 商户号
    /// </summary>
    public string MchId { get; set; } = string.Empty;

    /// <summary>
    /// 商品描述
    /// </summary>
    public string ItemDesc { get; set; } = string.Empty;

    /// <summary>
    /// 错误码
    /// </summary>
    public int ErrorCode { get; set; }

    /// <summary>
    /// 错误消息
    /// </summary>
    public string ErrorMsg { get; set; } = string.Empty;

    /// <summary>
    /// 重试次数
    /// </summary>
    public int RetryCount { get; set; }

    /// <summary>
    /// 最后重试时间（Unix时间戳）
    /// </summary>
    public long LastRetryTime { get; set; }

    /// <summary>
    /// 创建时间（Unix时间戳）
    /// </summary>
    public long CreateTime { get; set; }
}

#endregion

#region Payment Type Enum

/// <summary>
/// 支付类型枚举
/// </summary>
public enum PaymentType
{
    /// <summary>
    /// 微信支付
    /// </summary>
    WechatPay = 1,

    /// <summary>
    /// 余额支付
    /// </summary>
    BalancePay = 2,

    /// <summary>
    /// 积分支付
    /// </summary>
    IntegralPay = 3,

    /// <summary>
    /// 哈尼券支付
    /// </summary>
    Money2Pay = 4
}

/// <summary>
/// 订单类型枚举（attach值）
/// </summary>
public static class OrderAttachType
{
    /// <summary>
    /// 余额充值
    /// </summary>
    public const string UserRecharge = "user_recharge";

    /// <summary>
    /// 一番赏订单
    /// </summary>
    public const string OrderYfs = "order_yfs";

    /// <summary>
    /// 擂台赏订单
    /// </summary>
    public const string OrderLts = "order_lts";

    /// <summary>
    /// 转转赏订单
    /// </summary>
    public const string OrderZzs = "order_zzs";

    /// <summary>
    /// 福利屋订单
    /// </summary>
    public const string OrderFlw = "order_flw";

    /// <summary>
    /// 商城赏订单
    /// </summary>
    public const string OrderScs = "order_scs";

    /// <summary>
    /// 无限赏订单
    /// </summary>
    public const string OrderWxs = "order_wxs";

    /// <summary>
    /// 翻倍赏订单
    /// </summary>
    public const string OrderFbs = "order_fbs";

    /// <summary>
    /// 抽卡机订单
    /// </summary>
    public const string OrderCkj = "order_ckj";

    /// <summary>
    /// 发货运费
    /// </summary>
    public const string OrderListSend = "order_list_send";

    /// <summary>
    /// 钻石商品订单
    /// </summary>
    public const string OrderProduct = "order_product";
}

#endregion


#region Recharge Models

/// <summary>
/// 充值请求
/// </summary>
public class RechargeRequest
{
    /// <summary>
    /// 充值金额
    /// </summary>
    [JsonPropertyName("money")]
    public decimal Money { get; set; }
}

/// <summary>
/// 充值结果
/// </summary>
public class RechargeResult
{
    /// <summary>
    /// 状态：1=成功，0=失败
    /// </summary>
    [JsonPropertyName("status")]
    public int Status { get; set; }

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

    /// <summary>
    /// 支付数据
    /// </summary>
    [JsonPropertyName("data")]
    public WechatPayData? Data { get; set; }

    /// <summary>
    /// 订单号
    /// </summary>
    [JsonPropertyName("order_num")]
    public string? OrderNum { get; set; }
}

/// <summary>
/// 充值配置响应
/// </summary>
public class RechargeConfigResponse
{
    /// <summary>
    /// 最低充值金额
    /// </summary>
    [JsonPropertyName("min_money")]
    public decimal MinMoney { get; set; }

    /// <summary>
    /// 充值说明
    /// </summary>
    [JsonPropertyName("description")]
    public string Description { get; set; } = string.Empty;

    /// <summary>
    /// 充值选项列表
    /// </summary>
    [JsonPropertyName("options")]
    public List<RechargeOption> Options { get; set; } = new();
}

/// <summary>
/// 充值选项
/// </summary>
public class RechargeOption
{
    /// <summary>
    /// 充值金额
    /// </summary>
    [JsonPropertyName("money")]
    public decimal Money { get; set; }

    /// <summary>
    /// 赠送金额
    /// </summary>
    [JsonPropertyName("gift")]
    public decimal Gift { get; set; }

    /// <summary>
    /// 显示标签
    /// </summary>
    [JsonPropertyName("label")]
    public string Label { get; set; } = string.Empty;
}

/// <summary>
/// 支付状态响应
/// </summary>
public class PayStatusResponse
{
    /// <summary>
    /// 状态：0=待支付，1=已支付，2=已失效
    /// </summary>
    [JsonPropertyName("status")]
    public int Status { get; set; }

    /// <summary>
    /// 状态文本
    /// </summary>
    [JsonPropertyName("status_text")]
    public string StatusText { get; set; } = string.Empty;

    /// <summary>
    /// 订单号
    /// </summary>
    [JsonPropertyName("order_num")]
    public string OrderNum { get; set; } = string.Empty;
}

/// <summary>
/// 余额支付请求
/// </summary>
public class BalancePayRequest
{
    /// <summary>
    /// 订单号
    /// </summary>
    [JsonPropertyName("order_num")]
    public string OrderNum { get; set; } = string.Empty;

    /// <summary>
    /// 支付金额
    /// </summary>
    [JsonPropertyName("amount")]
    public decimal Amount { get; set; }
}

/// <summary>
/// 余额支付结果
/// </summary>
public class BalancePayResult
{
    /// <summary>
    /// 状态：1=成功，0=失败
    /// </summary>
    [JsonPropertyName("status")]
    public int Status { get; set; }

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

    /// <summary>
    /// 剩余余额
    /// </summary>
    [JsonPropertyName("balance")]
    public decimal Balance { get; set; }
}

/// <summary>
/// 创建充值订单请求
/// </summary>
public class CreateRechargeOrderRequest
{
    /// <summary>
    /// 充值金额
    /// </summary>
    [JsonPropertyName("money")]
    public decimal Money { get; set; }
}

#endregion
