class KRPurchaseOrderNo {
  final String orderNo;

  KRPurchaseOrderNo({required this.orderNo});

  factory KRPurchaseOrderNo.fromJson(Map<String, dynamic> json) {
    return KRPurchaseOrderNo(orderNo: json['order_no'] ?? '');
  }
}

class KRPurchaseOrderUrl {
  final String url;

  KRPurchaseOrderUrl({required this.url});

  factory KRPurchaseOrderUrl.fromJson(Map<String, dynamic> json) {
    return KRPurchaseOrderUrl(url: json['checkout_url'] ?? '');
  }
}

/// Checkout 响应（参考 Tauri 项目）
class KRCheckoutResponse {
  final String type; // "url" | "qr" | "stripe" | "apple_iap"
  final String? checkoutUrl;
  final KRStripePayment? stripe;
  final KRIpaPayment? ipa;

  KRCheckoutResponse({
    required this.type,
    this.checkoutUrl,
    this.stripe,
    this.ipa,
  });

  factory KRCheckoutResponse.fromJson(Map<String, dynamic> json) {
    return KRCheckoutResponse(
      type: json['type'] ?? 'url',
      checkoutUrl: json['checkout_url'],
      stripe: json['stripe'] != null
          ? KRStripePayment.fromJson(json['stripe'])
          : null,
      ipa: json['ipa'] != null ? KRIpaPayment.fromJson(json['ipa']) : null,
    );
  }
}

/// Stripe 支付信息
class KRStripePayment {
  final String method;
  final String clientSecret;
  final String publishableKey;

  KRStripePayment({
    required this.method,
    required this.clientSecret,
    required this.publishableKey,
  });

  factory KRStripePayment.fromJson(Map<String, dynamic> json) {
    return KRStripePayment(
      method: json['method'] ?? '',
      clientSecret: json['client_secret'] ?? '',
      publishableKey: json['publishable_key'] ?? '',
    );
  }
}

class KRIpaPayment {
  final String productId;
  final String? applicationUsername;
  final String? orderNo;

  KRIpaPayment({
    required this.productId,
    this.applicationUsername,
    this.orderNo,
  });

  factory KRIpaPayment.fromJson(Map<String, dynamic> json) {
    return KRIpaPayment(
      productId: json['product_id'] ?? json['productId'] ?? '',
      applicationUsername:
          json['application_username'] ?? json['applicationUsername'],
      orderNo: json['order_no'] ?? json['orderNo'],
    );
  }
}
