class CategoryAnalyticResponseModel {
  final bool success;
  final CategoryAnalyticData? data;
  final dynamic errors;

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

  // Dari JSON String ke Model
  factory CategoryAnalyticResponseModel.fromJson(Map<String, dynamic> json) {
    return CategoryAnalyticResponseModel(
      success: json['success'],
      data: json['data'] == null
          ? null
          : CategoryAnalyticData.fromMap(json['data']),
      errors: json['errors'],
    );
  }

  // Dari Model ke JSON String
  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'data': data?.toMap(),
      'errors': errors,
    };
  }

  // Dari Map ke Model
  factory CategoryAnalyticResponseModel.fromMap(Map<String, dynamic> map) {
    return CategoryAnalyticResponseModel(
      success: map['success'],
      data: CategoryAnalyticData.fromMap(map['data']),
      errors: map['errors'],
    );
  }

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

class CategoryAnalyticData {
  final String organizationId;
  final String outletId;
  final DateTime dateFrom;
  final DateTime dateTo;
  final List<CategoryAnalyticItem> data;

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

  factory CategoryAnalyticData.fromMap(Map<String, dynamic> map) {
    return CategoryAnalyticData(
      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<CategoryAnalyticItem>.from(
              map['data']?.map((x) => CategoryAnalyticItem.fromMap(x)) ?? [],
            ),
    );
  }

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

class CategoryAnalyticItem {
  final String categoryId;
  final String categoryName;
  final int totalRevenue;
  final int totalQuantity;
  final int productCount;
  final int orderCount;

  CategoryAnalyticItem({
    required this.categoryId,
    required this.categoryName,
    required this.totalRevenue,
    required this.totalQuantity,
    required this.productCount,
    required this.orderCount,
  });

  factory CategoryAnalyticItem.fromMap(Map<String, dynamic> map) {
    return CategoryAnalyticItem(
      categoryId: map['category_id'],
      categoryName: map['category_name'],
      totalRevenue: map['total_revenue'] ?? 0,
      totalQuantity: map['total_quantity'] ?? 0,
      productCount: map['product_count'] ?? 0,
      orderCount: map['order_count'] ?? 0,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'category_id': categoryId,
      'category_name': categoryName,
      'total_revenue': totalRevenue,
      'total_quantity': totalQuantity,
      'product_count': productCount,
      'order_count': orderCount,
    };
  }
}
