class DetailOrderModelUnderscore {
  String idOrder;
  List<VirtualNumbers>? bankVa;
  String? virtualNumber;
  String? billerCode;
  String? bankName;
  String paymentType;
  String? email;
  String? name;
  String? url;
  // List<OrderModel>? orders;
  String totalPayment;
  DateTime transactionTime;
  DateTime transactionTimeLimit;
  String? qrCodeUrl;
  String? urlGopay;
  String? transactionStatus;
  String? merchantId;

  DetailOrderModelUnderscore({
    required this.idOrder,
    this.bankVa,
    this.url,
    this.bankName,
    this.qrCodeUrl,
    this.urlGopay,
    this.email,
    this.name,
    this.virtualNumber,
    this.billerCode,
    this.transactionStatus,
    this.merchantId,
    // this.orders,
    required this.transactionTime,
    required this.transactionTimeLimit,
    required this.totalPayment,
    required this.paymentType,
  });

  factory DetailOrderModelUnderscore.fromJson(Map<String, dynamic> json) {
    final pType = json['payment_type'] ?? '';
    final permataCheck = json['status_message'] ?? '';
    final total = json['gross_amount'] ?? '';
    final statusTransaction = json['transaction_status'] ?? '';
    final time = DateTime.parse(json['transaction_time'] ?? '');

    if (pType == 'echannel') {
      // Model for Payment Mandiri VA
      return DetailOrderModelUnderscore(
          transactionTime: time,
          idOrder: json["order_id"] ?? '',
          virtualNumber: json["bill_key"] ?? '',
          billerCode: json["biller_code"] ?? '',
          bankName: 'mandiri',
          paymentType: json['payment_type'] ?? '',
          totalPayment: total,
          transactionStatus: statusTransaction,
          transactionTimeLimit: time.add(Duration(days: 1)));
    } else if (pType == 'bank_transfer' &&
        permataCheck == 'Success, PERMATA VA transaction is successful') {
      // Model for Payment Permata VA
      return DetailOrderModelUnderscore(
          transactionTime: time,
          idOrder: json["order_id"] ?? '',
          virtualNumber: json["permata_va_number"] ?? '',
          bankName: 'permata',
          paymentType: json['payment_type'] ?? '',
          totalPayment: total,
          transactionStatus: statusTransaction,
          transactionTimeLimit: time.add(Duration(days: 1)));
    } else if (pType == 'bank_transfer') {
      // Model for Payment Bank Transfer (BCA & BNI)
      final bankVAResponse = List<VirtualNumbers>.from(
          json["va_numbers"].map((x) => VirtualNumbers.fromJson(x))).toList();
      return DetailOrderModelUnderscore(
          transactionTime: time,
          idOrder: json['order_id'] ?? '',
          paymentType: pType,
          bankVa: bankVAResponse,
          virtualNumber: bankVAResponse[0].vaNumber,
          totalPayment: total,
          transactionStatus: statusTransaction,
          transactionTimeLimit: time.add(Duration(days: 1)),
          bankName: bankVAResponse[0].bank);
    } else if (pType == 'credit_card') {
      // Model for CC Payment
      return DetailOrderModelUnderscore(
          transactionTime: time,
          url: json['redirect_url'] ?? '',
          idOrder: json['order_id'] ?? '',
          paymentType: pType,
          totalPayment: total,
          transactionStatus: statusTransaction,
          transactionTimeLimit: time.add(Duration(days: 1)),
          bankName: json['bank'] ?? '');
    } else if (pType == 'cstore') {
      // Model for Store (Indomaret & Alfamart)
      return DetailOrderModelUnderscore(
          transactionTime: time,
          idOrder: json["order_id"] ?? '',
          virtualNumber: json["payment_code"] ?? '',
          merchantId: json["merchant_id"] ?? '',
          paymentType: pType,
          bankName: json['store'] ?? '',
          totalPayment: total,
          transactionStatus: statusTransaction,
          transactionTimeLimit: time.add(Duration(days: 1)));
    } else {
      // Model for GoPay
      final qrUrl = json['actions'][0]['url'];
      final urlGop = json['actions'][1]['url'];

      return DetailOrderModelUnderscore(
        idOrder: json['order_id'] ?? '',
        transactionTime: time,
        transactionTimeLimit: time.add(Duration(days: 1)),
        totalPayment: total,
        paymentType: pType,
        transactionStatus: statusTransaction,
        qrCodeUrl: qrUrl,
        urlGopay: urlGop,
      );
    }
  }

  Map<String, dynamic> toJson() {
    return paymentType == "echannel"
        ? {
            "order_id": idOrder,
            "payment_type": paymentType,
            "bill_key": bankVa,
            "transaction_status": transactionStatus,
          }
        : paymentType == 'permata'
            ? {
                "order_id": idOrder,
                "payment_type": paymentType,
                "permata_va_number": bankVa,
                "transaction_status": transactionStatus,
              }
            : {
                "order_id": idOrder,
                "va_numbers":
                    List<dynamic>.from(bankVa!.map((x) => x.toJson())).toList(),
                "payment_type": paymentType,
                "transaction_status": transactionStatus,
              };
  }
}

class VirtualNumbers {
  VirtualNumbers({
    this.bank,
    this.vaNumber,
  });

  String? bank;
  String? vaNumber;

  factory VirtualNumbers.fromJson(Map<String, dynamic> json) => VirtualNumbers(
        bank: json["bank"],
        vaNumber: json["va_number"],
      );

  Map<String, dynamic> toJson() => {
        "bank": bank,
        "va_number": vaNumber,
      };
}
