part of '../analytic_dtos.dart';

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

  const factory InventoryAnalyticDto({
    @JsonKey(name: "summary") InventoryAnalyticSummaryDto? summary,
    @JsonKey(name: "products") List<InventoryAnalyticProductItemDto>? products,
    @JsonKey(name: "ingredients")
    List<InventoryAnalyticIngredientItemDto>? ingredients,
  }) = _InventoryAnalyticDto;

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

  // Mapper ke domain (opsional)
  InventoryAnalytic toDomain() => InventoryAnalytic(
    summary: summary?.toDomain() ?? InventoryAnalyticSummary.empty(),
    products: products?.map((e) => e.toDomain()).toList() ?? [],
    ingredients: ingredients?.map((e) => e.toDomain()).toList() ?? [],
  );
}

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

  const factory InventoryAnalyticSummaryDto({
    @JsonKey(name: "total_products") int? totalProducts,
    @JsonKey(name: "total_ingredients") int? totalIngredients,
    @JsonKey(name: "total_value") int? totalValue,
    @JsonKey(name: "low_stock_products") int? lowStockProducts,
    @JsonKey(name: "low_stock_ingredients") int? lowStockIngredients,
    @JsonKey(name: "zero_stock_products") int? zeroStockProducts,
    @JsonKey(name: "zero_stock_ingredients") int? zeroStockIngredients,
    @JsonKey(name: "total_sold_products") int? totalSoldProducts,
    @JsonKey(name: "total_sold_ingredients") int? totalSoldIngredients,
    @JsonKey(name: "outlet_id") String? outletId,
    @JsonKey(name: "outlet_name") String? outletName,
    @JsonKey(name: "generated_at") DateTime? generatedAt,
  }) = _InventoryAnalyticSummaryDto;

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

  // Optional mapper ke entity
  InventoryAnalyticSummary toDomain() => InventoryAnalyticSummary(
    totalProducts: totalProducts ?? 0,
    totalIngredients: totalIngredients ?? 0,
    totalValue: totalValue ?? 0,
    lowStockProducts: lowStockProducts ?? 0,
    lowStockIngredients: lowStockIngredients ?? 0,
    zeroStockProducts: zeroStockProducts ?? 0,
    zeroStockIngredients: zeroStockIngredients ?? 0,
    totalSoldProducts: totalSoldProducts ?? 0,
    totalSoldIngredients: totalSoldIngredients ?? 0,
    outletId: outletId ?? '',
    outletName: outletName ?? '',
    generatedAt: generatedAt ?? DateTime.now(),
  );
}

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

  const factory InventoryAnalyticProductItemDto({
    @JsonKey(name: "id") String? id,
    @JsonKey(name: "product_id") String? productId,
    @JsonKey(name: "product_name") String? productName,
    @JsonKey(name: "category_name") String? categoryName,
    @JsonKey(name: "quantity") int? quantity,
    @JsonKey(name: "reorder_level") int? reorderLevel,
    @JsonKey(name: "unit_cost") num? unitCost,
    @JsonKey(name: "total_value") int? totalValue,
    @JsonKey(name: "total_in") int? totalIn,
    @JsonKey(name: "total_out") int? totalOut,
    @JsonKey(name: "is_low_stock") bool? isLowStock,
    @JsonKey(name: "is_zero_stock") bool? isZeroStock,
    @JsonKey(name: "updated_at") DateTime? updatedAt,
  }) = _InventoryAnalyticProductItemDto;

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

  // Mapper ke domain (opsional)
  InventoryAnalyticProductItem toDomain() => InventoryAnalyticProductItem(
    id: id ?? '',
    productId: productId ?? '',
    productName: productName ?? '',
    categoryName: categoryName ?? '',
    quantity: quantity ?? 0,
    reorderLevel: reorderLevel ?? 0,
    unitCost: unitCost ?? 0,
    totalValue: totalValue ?? 0,
    totalIn: totalIn ?? 0,
    totalOut: totalOut ?? 0,
    isLowStock: isLowStock ?? false,
    isZeroStock: isZeroStock ?? false,
    updatedAt: updatedAt ?? DateTime.now(),
  );
}

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

  const factory InventoryAnalyticIngredientItemDto({
    @JsonKey(name: "id") String? id,
    @JsonKey(name: "ingredient_id") String? ingredientId,
    @JsonKey(name: "ingredient_name") String? ingredientName,
    @JsonKey(name: "unit_name") String? unitName,
    @JsonKey(name: "quantity") int? quantity,
    @JsonKey(name: "reorder_level") int? reorderLevel,
    @JsonKey(name: "unit_cost") num? unitCost,
    @JsonKey(name: "total_value") int? totalValue,
    @JsonKey(name: "total_in") int? totalIn,
    @JsonKey(name: "total_out") int? totalOut,
    @JsonKey(name: "is_low_stock") bool? isLowStock,
    @JsonKey(name: "is_zero_stock") bool? isZeroStock,
    @JsonKey(name: "updated_at") DateTime? updatedAt,
  }) = _InventoryAnalyticIngredientItemDto;

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

  // Mapper ke domain (opsional)
  InventoryAnalyticIngredientItem toDomain() => InventoryAnalyticIngredientItem(
    id: id ?? '',
    ingredientId: ingredientId ?? '',
    ingredientName: ingredientName ?? '',
    unitName: unitName ?? '',
    quantity: quantity ?? 0,
    reorderLevel: reorderLevel ?? 0,
    unitCost: unitCost ?? 0,
    totalValue: totalValue ?? 0,
    totalIn: totalIn ?? 0,
    totalOut: totalOut ?? 0,
    isLowStock: isLowStock ?? false,
    isZeroStock: isZeroStock ?? false,
    updatedAt: updatedAt ?? DateTime.now(),
  );
}
