part of '../analytic_dtos.dart';

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

  const factory InventoryAnalyticDto({
    @JsonKey(name: "summary") InventorySummaryDto? summary,
    @JsonKey(name: "products") List<InventoryProductDto>? products,
    @JsonKey(name: "ingredients") List<InventoryIngredientDto>? ingredients,
  }) = _InventoryAnalyticDto;

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

  InventoryAnalytic toDomain() => InventoryAnalytic(
    summary: summary?.toDomain() ?? InventorySummary.empty(),
    products: products?.map((e) => e.toDomain()).toList() ?? [],
    ingredients: ingredients?.map((e) => e.toDomain()).toList() ?? [],
  );
}

@freezed
class InventorySummaryDto with _$InventorySummaryDto {
  const factory InventorySummaryDto({
    @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") String? generatedAt,
  }) = _InventorySummaryDto;

  factory InventorySummaryDto.fromJson(Map<String, dynamic> json) =>
      _$InventorySummaryDtoFromJson(json);
}

extension InventorySummaryDtoX on InventorySummaryDto {
  InventorySummary toDomain() => InventorySummary(
    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 ?? "",
  );
}

@freezed
class InventoryProductDto with _$InventoryProductDto {
  const factory InventoryProductDto({
    @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") int? 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") String? updatedAt,
  }) = _InventoryProductDto;

  factory InventoryProductDto.fromJson(Map<String, dynamic> json) =>
      _$InventoryProductDtoFromJson(json);
}

extension InventoryProductDtoX on InventoryProductDto {
  InventoryProduct toDomain() => InventoryProduct(
    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 ?? "",
  );
}

@freezed
class InventoryIngredientDto with _$InventoryIngredientDto {
  const factory InventoryIngredientDto({
    @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") int? 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") String? updatedAt,
  }) = _InventoryIngredientDto;

  factory InventoryIngredientDto.fromJson(Map<String, dynamic> json) =>
      _$InventoryIngredientDtoFromJson(json);
}

extension InventoryIngredientDtoX on InventoryIngredientDto {
  InventoryIngredient toDomain() => InventoryIngredient(
    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 ?? "",
  );
}
