/// 订单状态模型类
class KROrderStatus {
  /// 订单ID
  final int kr_id;
  
  /// 用户ID
  final int kr_userId;
  
  /// 订单编号
  final String kr_orderNo;
  
  /// 订单类型
  final int kr_type;
  
  /// 购买数量
  final int kr_quantity;
  
  /// 单价
  final double kr_price;
  
  /// 总金额
  final double kr_amount;
  
  /// 赠送金额
  final double kr_giftAmount;
  
  /// 折扣
  final double kr_discount;
  
  /// 优惠券码
  final String? kr_coupon;
  
  /// 优惠券折扣金额
  final double kr_couponDiscount;
  
  /// 佣金
  final double kr_commission;
  
  /// 支付方式
  final String kr_method;
  
  /// 手续费
  final double kr_feeAmount;
  
  /// 交易号
  final String kr_tradeNo;
  
  /// 订单状态
  final int kr_status;
  
  /// 订阅ID
  final int kr_subscribeId;
  
  /// 订阅信息
  final KRSubscribe? kr_subscribe;
  
  /// 创建时间
  final int kr_createdAt;
  
  /// 更新时间
  final int kr_updatedAt;

  /// 订单状态枚举
  static const int kr_statusPending = 0;    // 待支付
  static const int kr_statusPaid = 1;       // 已支付
  static const int kr_statusCancelled = 2;  // 已取消
  static const int kr_statusRefunded = 3;   // 已退款
  static const int kr_statusFailed = 4;     // 支付失败
  
  /// 获取订单状态描述
  String get kr_statusText {
    switch (kr_status) {
      case kr_statusPending:
        return '待支付';
      case kr_statusPaid:
        return '已支付';
      case kr_statusCancelled:
        return '已取消';
      case kr_statusRefunded:
        return '已退款';
      case kr_statusFailed:
        return '支付失败';
      default:
        return '未知状态';
    }
  }

  /// 判断订单是否待支付
  bool get kr_isPending => kr_status == kr_statusPending;

  /// 判断订单是否已支付
  bool get kr_isPaid => kr_status == kr_statusPaid;

  /// 判断订单是否已取消
  bool get kr_isCancelled => kr_status == kr_statusCancelled;

  /// 判断订单是否已退款
  bool get kr_isRefunded => kr_status == kr_statusRefunded;

  /// 判断订单是否支付失败
  bool get kr_isFailed => kr_status == kr_statusFailed;

  const KROrderStatus({
    required this.kr_id,
    required this.kr_userId,
    required this.kr_orderNo,
    required this.kr_type,
    required this.kr_quantity,
    required this.kr_price,
    required this.kr_amount,
    required this.kr_giftAmount,
    required this.kr_discount,
    this.kr_coupon,
    required this.kr_couponDiscount,
    required this.kr_commission,
    required this.kr_method,
    required this.kr_feeAmount,
    required this.kr_tradeNo,
    required this.kr_status,
    required this.kr_subscribeId,
    this.kr_subscribe,
    required this.kr_createdAt,
    required this.kr_updatedAt,
  });

  /// 从JSON映射创建订单状态实例
  factory KROrderStatus.fromJson(Map<String, dynamic> json) {
    return KROrderStatus(
      kr_id: json['id'] as int? ?? 0,
      kr_userId: json['user_id'] as int? ?? 0,
      kr_orderNo: json['order_no'] as String? ?? '',
      kr_type: json['type'] as int? ?? 0,
      kr_quantity: json['quantity'] as int? ?? 0,
      kr_price: (json['price'] as num?)?.toDouble() ?? 0.0,
      kr_amount: (json['amount'] as num?)?.toDouble() ?? 0.0,
      kr_giftAmount: (json['gift_amount'] as num?)?.toDouble() ?? 0.0,
      kr_discount: (json['discount'] as num?)?.toDouble() ?? 0.0,
      kr_coupon: json['coupon'] as String?,
      kr_couponDiscount: (json['coupon_discount'] as num?)?.toDouble() ?? 0.0,
      kr_commission: (json['commission'] as num?)?.toDouble() ?? 0.0,
      kr_method: json['method'] as String? ?? '',
      kr_feeAmount: (json['fee_amount'] as num?)?.toDouble() ?? 0.0,
      kr_tradeNo: json['trade_no'] as String? ?? '',
      kr_status: json['status'] as int? ?? 0,
      kr_subscribeId: json['subscribe_id'] as int? ?? 0,
      kr_subscribe: json['subscribe'] != null 
          ? KRSubscribe.fromJson(json['subscribe'] as Map<String, dynamic>)
          : null,
      kr_createdAt: json['created_at'] as int? ?? 0,
      kr_updatedAt: json['updated_at'] as int? ?? 0,
    );
  }

  /// 转换为JSON映射
  Map<String, dynamic> toJson() {
    return {
      'id': kr_id,
      'user_id': kr_userId,
      'order_no': kr_orderNo,
      'type': kr_type,
      'quantity': kr_quantity,
      'price': kr_price,
      'amount': kr_amount,
      'gift_amount': kr_giftAmount,
      'discount': kr_discount,
      'coupon': kr_coupon,
      'coupon_discount': kr_couponDiscount,
      'commission': kr_commission,
      'method': kr_method,
      'fee_amount': kr_feeAmount,
      'trade_no': kr_tradeNo,
      'status': kr_status,
      'subscribe_id': kr_subscribeId,
      'subscribe': kr_subscribe?.toJson(),
      'created_at': kr_createdAt,
      'updated_at': kr_updatedAt,
    };
  }
}

/// 订阅信息模型类
class KRSubscribe {
  final int kr_id;
  final String kr_name;
  final String kr_description;
  final double kr_unitPrice;
  final String kr_unitTime;
  final List<KRDiscount> kr_discount;
  final int kr_replacement;
  final int kr_inventory;
  final int kr_traffic;
  final int kr_speedLimit;
  final int kr_deviceLimit;
  final int kr_quota;
  final int kr_groupId;
  final List<int> kr_serverGroup;
  final List<int> kr_server;
  final bool kr_show;
  final bool kr_sell;
  final int kr_sort;
  final double kr_deductionRatio;
  final bool kr_allowDeduction;
  final int kr_resetCycle;
  final bool kr_renewalReset;
  final int kr_createdAt;
  final int kr_updatedAt;

  const KRSubscribe({
    required this.kr_id,
    required this.kr_name,
    required this.kr_description,
    required this.kr_unitPrice,
    required this.kr_unitTime,
    required this.kr_discount,
    required this.kr_replacement,
    required this.kr_inventory,
    required this.kr_traffic,
    required this.kr_speedLimit,
    required this.kr_deviceLimit,
    required this.kr_quota,
    required this.kr_groupId,
    required this.kr_serverGroup,
    required this.kr_server,
    required this.kr_show,
    required this.kr_sell,
    required this.kr_sort,
    required this.kr_deductionRatio,
    required this.kr_allowDeduction,
    required this.kr_resetCycle,
    required this.kr_renewalReset,
    required this.kr_createdAt,
    required this.kr_updatedAt,
  });

  factory KRSubscribe.fromJson(Map<String, dynamic> json) {
    return KRSubscribe(
      kr_id: json['id'] as int? ?? 0,
      kr_name: json['name'] as String? ?? '',
      kr_description: json['description'] as String? ?? '',
      kr_unitPrice: (json['unit_price'] as num?)?.toDouble() ?? 0.0,
      kr_unitTime: json['unit_time'] as String? ?? '',
      kr_discount: (json['discount'] as List<dynamic>?)
          ?.map((e) => KRDiscount.fromJson(e as Map<String, dynamic>))
          .toList() ?? [],
      kr_replacement: json['replacement'] as int? ?? 0,
      kr_inventory: json['inventory'] as int? ?? 0,
      kr_traffic: json['traffic'] as int? ?? 0,
      kr_speedLimit: json['speed_limit'] as int? ?? 0,
      kr_deviceLimit: json['device_limit'] as int? ?? 0,
      kr_quota: json['quota'] as int? ?? 0,
      kr_groupId: json['group_id'] as int? ?? 0,
      kr_serverGroup: (json['server_group'] as List<dynamic>?)
          ?.map((e) => e as int)
          .toList() ?? [],
      kr_server: (json['server'] as List<dynamic>?)
          ?.map((e) => e as int)
          .toList() ?? [],
      kr_show: json['show'] as bool? ?? false,
      kr_sell: json['sell'] as bool? ?? false,
      kr_sort: json['sort'] as int? ?? 0,
      kr_deductionRatio: (json['deduction_ratio'] as num?)?.toDouble() ?? 0.0,
      kr_allowDeduction: json['allow_deduction'] as bool? ?? false,
      kr_resetCycle: json['reset_cycle'] as int? ?? 0,
      kr_renewalReset: json['renewal_reset'] as bool? ?? false,
      kr_createdAt: json['created_at'] as int? ?? 0,
      kr_updatedAt: json['updated_at'] as int? ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': kr_id,
      'name': kr_name,
      'description': kr_description,
      'unit_price': kr_unitPrice,
      'unit_time': kr_unitTime,
      'discount': kr_discount.map((e) => e.toJson()).toList(),
      'replacement': kr_replacement,
      'inventory': kr_inventory,
      'traffic': kr_traffic,
      'speed_limit': kr_speedLimit,
      'device_limit': kr_deviceLimit,
      'quota': kr_quota,
      'group_id': kr_groupId,
      'server_group': kr_serverGroup,
      'server': kr_server,
      'show': kr_show,
      'sell': kr_sell,
      'sort': kr_sort,
      'deduction_ratio': kr_deductionRatio,
      'allow_deduction': kr_allowDeduction,
      'reset_cycle': kr_resetCycle,
      'renewal_reset': kr_renewalReset,
      'created_at': kr_createdAt,
      'updated_at': kr_updatedAt,
    };
  }
}

/// 折扣信息模型类
class KRDiscount {
  final int kr_quantity;
  final double kr_discount;

  const KRDiscount({
    required this.kr_quantity,
    required this.kr_discount,
  });

  factory KRDiscount.fromJson(Map<String, dynamic> json) {
    return KRDiscount(
      kr_quantity: json['quantity'] as int? ?? 0,
      kr_discount: (json['discount'] as num?)?.toDouble() ?? 0.0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'quantity': kr_quantity,
      'discount': kr_discount,
    };
  }
}
