part of '../analytic_dtos.dart';

@freezed
class ProductAnalyticDto with _$ProductAnalyticDto {
  const ProductAnalyticDto._();

  const factory ProductAnalyticDto({
    @JsonKey(name: "organization_id") String? organizationId,
    @JsonKey(name: "outlet_id") String? outletId,
    @JsonKey(name: "date_from") DateTime? dateFrom,
    @JsonKey(name: "date_to") DateTime? dateTo,
    @JsonKey(name: "data") List<ProductAnalyticItemDto>? data,
  }) = _ProductAnalyticDto;

  factory ProductAnalyticDto.fromJson(Map<String, dynamic> json) =>
      _$ProductAnalyticDtoFromJson(json);

  // Optional mapping ke domain entity
  ProductAnalytic toDomain() => ProductAnalytic(
    organizationId: organizationId ?? '',
    outletId: outletId ?? '',
    dateFrom: dateFrom ?? DateTime.now(),
    dateTo: dateTo ?? DateTime.now(),
    data: data?.map((e) => e.toDomain()).toList() ?? [],
  );
}

@freezed
class ProductAnalyticItemDto with _$ProductAnalyticItemDto {
  const ProductAnalyticItemDto._();

  const factory ProductAnalyticItemDto({
    @JsonKey(name: "product_id") String? productId,
    @JsonKey(name: "product_name") String? productName,
    @JsonKey(name: "category_id") String? categoryId,
    @JsonKey(name: "category_name") String? categoryName,
    @JsonKey(name: "quantity_sold") int? quantitySold,
    @JsonKey(name: "revenue") int? revenue,
    @JsonKey(name: "average_price") double? averagePrice,
    @JsonKey(name: "order_count") int? orderCount,
  }) = _ProductAnalyticItemDto;

  factory ProductAnalyticItemDto.fromJson(Map<String, dynamic> json) =>
      _$ProductAnalyticItemDtoFromJson(json);

  // Optional mapping ke domain entity
  ProductAnalyticItem toDomain() => ProductAnalyticItem(
    productId: productId ?? '',
    productName: productName ?? '',
    categoryId: categoryId ?? '',
    categoryName: categoryName ?? '',
    quantitySold: quantitySold ?? 0,
    revenue: revenue ?? 0,
    averagePrice: averagePrice ?? 0.0,
    orderCount: orderCount ?? 0,
  );
}
