class ProductAnalyticResponseModel {
  final bool success;
  final ProductAnalyticData? data;
  final dynamic errors;

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

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

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

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

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

class ProductAnalyticData {
  final String organizationId;
  final String outletId;
  final DateTime dateFrom;
  final DateTime dateTo;
  final List<ProductAnalyticItem> data;

  ProductAnalyticData({
    required this.organizationId,
    required this.outletId,
    required this.dateFrom,
    required this.dateTo,
    required this.data,
  });

  factory ProductAnalyticData.fromMap(Map<String, dynamic> map) =>
      ProductAnalyticData(
        organizationId: map['organization_id'],
        outletId: map['outlet_id'],
        dateFrom: DateTime.parse(map['date_from']),
        dateTo: DateTime.parse(map['date_to']),
        data: map['data'] == null
            ? []
            : List<ProductAnalyticItem>.from(
                map['data'].map((x) => ProductAnalyticItem.fromMap(x)),
              ),
      );

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

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

  ProductAnalyticItem({
    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 ProductAnalyticItem.fromMap(Map<String, dynamic> map) =>
      ProductAnalyticItem(
        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'] as num).toDouble(),
        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 ProductInsights {
  final List<ProductAnalyticItem> topProducts;
  final ProductAnalyticItem? bestProduct;
  final List<CategorySummary> categorySummary;
  final int totalProducts;
  final int totalRevenue;
  final int totalQuantitySold;

  ProductInsights({
    required this.topProducts,
    required this.bestProduct,
    required this.categorySummary,
    required this.totalProducts,
    required this.totalRevenue,
    required this.totalQuantitySold,
  });
}

// Category summary class
class CategorySummary {
  final String categoryName;
  int productCount;
  int totalRevenue;

  CategorySummary({
    required this.categoryName,
    required this.productCount,
    required this.totalRevenue,
  });
}
