class DashboardAnalyticResponseModel {
  final bool success;
  final DashboardAnalyticData data;
  final dynamic errors;

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

  /// Khusus untuk JSON string
  factory DashboardAnalyticResponseModel.fromJson(Map<String, dynamic> json) =>
      DashboardAnalyticResponseModel.fromMap(json);

  /// Untuk menerima Map biasa (bukan dari JSON string)
  factory DashboardAnalyticResponseModel.fromMap(Map<String, dynamic> map) {
    return DashboardAnalyticResponseModel(
      success: map['success'] ?? false,
      data: DashboardAnalyticData.fromMap(map['data'] ?? {}),
      errors: map['errors'],
    );
  }

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

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

class DashboardAnalyticData {
  final String organizationId;
  final String outletId;
  final String dateFrom;
  final String dateTo;
  final DashboardOverview overview;
  final List<TopProduct> topProducts;
  final List<PaymentMethodAnalytic> paymentMethods;
  final List<RecentSale> recentSales;

  DashboardAnalyticData({
    required this.organizationId,
    required this.outletId,
    required this.dateFrom,
    required this.dateTo,
    required this.overview,
    required this.topProducts,
    required this.paymentMethods,
    required this.recentSales,
  });

  factory DashboardAnalyticData.fromMap(Map<String, dynamic> map) =>
      DashboardAnalyticData(
        organizationId: map['organization_id'],
        outletId: map['outlet_id'],
        dateFrom: map['date_from'],
        dateTo: map['date_to'],
        overview: DashboardOverview.fromMap(map['overview']),
        topProducts: List<TopProduct>.from(
            map['top_products']?.map((x) => TopProduct.fromMap(x))),
        paymentMethods: List<PaymentMethodAnalytic>.from(map['payment_methods']
            ?.map((x) => PaymentMethodAnalytic.fromMap(x))),
        recentSales: List<RecentSale>.from(
            map['recent_sales']?.map((x) => RecentSale.fromMap(x))),
      );

  Map<String, dynamic> toMap() => {
        'organization_id': organizationId,
        'outlet_id': outletId,
        'date_from': dateFrom,
        'date_to': dateTo,
        'overview': overview.toMap(),
        'top_products': topProducts.map((x) => x.toMap()).toList(),
        'payment_methods': paymentMethods.map((x) => x.toMap()).toList(),
        'recent_sales': recentSales.map((x) => x.toMap()).toList(),
      };
}

class DashboardOverview {
  final int totalSales;
  final int totalOrders;
  final double averageOrderValue;
  final int totalCustomers;
  final int voidedOrders;
  final int refundedOrders;

  DashboardOverview({
    required this.totalSales,
    required this.totalOrders,
    required this.averageOrderValue,
    required this.totalCustomers,
    required this.voidedOrders,
    required this.refundedOrders,
  });

  factory DashboardOverview.fromMap(Map<String, dynamic> map) =>
      DashboardOverview(
        totalSales: map['total_sales'],
        totalOrders: map['total_orders'],
        averageOrderValue: map['average_order_value']?.toDouble() ?? 0.0,
        totalCustomers: map['total_customers'],
        voidedOrders: map['voided_orders'],
        refundedOrders: map['refunded_orders'],
      );

  Map<String, dynamic> toMap() => {
        'total_sales': totalSales,
        'total_orders': totalOrders,
        'average_order_value': averageOrderValue,
        'total_customers': totalCustomers,
        'voided_orders': voidedOrders,
        'refunded_orders': refundedOrders,
      };
}

class TopProduct {
  final String productId;
  final String productName;
  final String categoryId;
  final String categoryName;
  final int quantitySold;
  final int revenue;
  final double averagePrice;
  final int orderCount;

  TopProduct({
    required this.productId,
    required this.productName,
    required this.categoryId,
    required this.categoryName,
    required this.quantitySold,
    required this.revenue,
    required this.averagePrice,
    required this.orderCount,
  });

  factory TopProduct.fromMap(Map<String, dynamic> map) => TopProduct(
        productId: map['product_id'],
        productName: map['product_name'],
        categoryId: map['category_id'],
        categoryName: map['category_name'],
        quantitySold: map['quantity_sold'],
        revenue: map['revenue'],
        averagePrice: map['average_price']?.toDouble() ?? 0.0,
        orderCount: map['order_count'],
      );

  Map<String, dynamic> toMap() => {
        'product_id': productId,
        'product_name': productName,
        'category_id': categoryId,
        'category_name': categoryName,
        'quantity_sold': quantitySold,
        'revenue': revenue,
        'average_price': averagePrice,
        'order_count': orderCount,
      };
}

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

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

  factory PaymentMethodAnalytic.fromMap(Map<String, dynamic> map) =>
      PaymentMethodAnalytic(
        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']?.toDouble() ?? 0.0,
      );

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

class RecentSale {
  final String date;
  final int sales;
  final int orders;
  final int items;
  final int tax;
  final int discount;
  final int netSales;

  RecentSale({
    required this.date,
    required this.sales,
    required this.orders,
    required this.items,
    required this.tax,
    required this.discount,
    required this.netSales,
  });

  factory RecentSale.fromMap(Map<String, dynamic> map) => RecentSale(
        date: map['date'],
        sales: map['sales'],
        orders: map['orders'],
        items: map['items'],
        tax: map['tax'],
        discount: map['discount'],
        netSales: map['net_sales'],
      );

  Map<String, dynamic> toMap() => {
        'date': date,
        'sales': sales,
        'orders': orders,
        'items': items,
        'tax': tax,
        'discount': discount,
        'net_sales': netSales,
      };
}
