class PaymentMethodAnalyticResponseModel {
  final bool success;
  final PaymentMethodAnalyticData? data;
  final dynamic errors;

  PaymentMethodAnalyticResponseModel({
    required this.success,
    required this.data,
    this.errors,
  });

  factory PaymentMethodAnalyticResponseModel.fromJson(
          Map<String, dynamic> json) =>
      PaymentMethodAnalyticResponseModel.fromMap(json);

  Map<String, dynamic> toJson() => toMap();

  factory PaymentMethodAnalyticResponseModel.fromMap(Map<String, dynamic> map) {
    return PaymentMethodAnalyticResponseModel(
      success: map['success'],
      data: map['data'] == null
          ? null
          : PaymentMethodAnalyticData.fromMap(map['data']),
      errors: map['errors'],
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'success': success,
      'data': data?.toMap(),
      'errors': errors,
    };
  }
}

class PaymentMethodAnalyticData {
  final String organizationId;
  final String outletId;
  final DateTime dateFrom;
  final DateTime dateTo;
  final String groupBy;
  final PaymentSummary summary;
  final List<PaymentMethodAnalyticItem> data;

  PaymentMethodAnalyticData({
    required this.organizationId,
    required this.outletId,
    required this.dateFrom,
    required this.dateTo,
    required this.groupBy,
    required this.summary,
    required this.data,
  });

  factory PaymentMethodAnalyticData.fromJson(Map<String, dynamic> json) =>
      PaymentMethodAnalyticData.fromMap(json);

  Map<String, dynamic> toJson() => toMap();

  factory PaymentMethodAnalyticData.fromMap(Map<String, dynamic> map) {
    return PaymentMethodAnalyticData(
      organizationId: map['organization_id'],
      outletId: map['outlet_id'],
      dateFrom: DateTime.parse(map['date_from']),
      dateTo: DateTime.parse(map['date_to']),
      groupBy: map['group_by'],
      summary: PaymentSummary.fromMap(map['summary']),
      data: map['data'] == null
          ? []
          : List<PaymentMethodAnalyticItem>.from(
              map['data']?.map((x) => PaymentMethodAnalyticItem.fromMap(x)) ??
                  [],
            ),
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'organization_id': organizationId,
      'outlet_id': outletId,
      'date_from': dateFrom.toIso8601String(),
      'date_to': dateTo.toIso8601String(),
      'group_by': groupBy,
      'summary': summary.toMap(),
      'data': data.map((x) => x.toMap()).toList(),
    };
  }
}

class PaymentSummary {
  final int totalAmount;
  final int totalOrders;
  final int totalPayments;
  final double averageOrderValue;

  PaymentSummary({
    required this.totalAmount,
    required this.totalOrders,
    required this.totalPayments,
    required this.averageOrderValue,
  });

  factory PaymentSummary.fromJson(Map<String, dynamic> json) =>
      PaymentSummary.fromMap(json);

  Map<String, dynamic> toJson() => toMap();

  factory PaymentSummary.fromMap(Map<String, dynamic> map) {
    return PaymentSummary(
      totalAmount: map['total_amount'],
      totalOrders: map['total_orders'],
      totalPayments: map['total_payments'],
      averageOrderValue: (map['average_order_value'] as num).toDouble(),
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'total_amount': totalAmount,
      'total_orders': totalOrders,
      'total_payments': totalPayments,
      'average_order_value': averageOrderValue,
    };
  }
}

class PaymentMethodAnalyticItem {
  final String paymentMethodId;
  final String paymentMethodName;
  final String paymentMethodType;
  final int totalAmount;
  final int orderCount;
  final int paymentCount;
  final int percentage;

  PaymentMethodAnalyticItem({
    required this.paymentMethodId,
    required this.paymentMethodName,
    required this.paymentMethodType,
    required this.totalAmount,
    required this.orderCount,
    required this.paymentCount,
    required this.percentage,
  });

  factory PaymentMethodAnalyticItem.fromJson(Map<String, dynamic> json) =>
      PaymentMethodAnalyticItem.fromMap(json);

  Map<String, dynamic> toJson() => toMap();

  factory PaymentMethodAnalyticItem.fromMap(Map<String, dynamic> map) {
    return PaymentMethodAnalyticItem(
      paymentMethodId: map['payment_method_id'],
      paymentMethodName: map['payment_method_name'],
      paymentMethodType: map['payment_method_type'],
      totalAmount: map['total_amount'],
      orderCount: map['order_count'],
      paymentCount: map['payment_count'],
      percentage: map['percentage'],
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'payment_method_id': paymentMethodId,
      'payment_method_name': paymentMethodName,
      'payment_method_type': paymentMethodType,
      'total_amount': totalAmount,
      'order_count': orderCount,
      'payment_count': paymentCount,
      'percentage': percentage,
    };
  }
}
