part of '../analytic_dtos.dart';

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

  const factory ProfitLossAnalyticDto({
    @JsonKey(name: "organization_id") String? organizationId,
    @JsonKey(name: "date_from") String? dateFrom,
    @JsonKey(name: "date_to") String? dateTo,
    @JsonKey(name: "group_by") String? groupBy,
    @JsonKey(name: "summary") ProfitLossAnalyticSummaryDto? summary,
    @JsonKey(name: "data") List<ProfitLossAnalyticItemDto>? data,
    @JsonKey(name: "product_data")
    List<ProfitLossAnalyticProductDto>? productData,
  }) = _ProfitLossAnalyticDto;

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

  // Optional: mapper ke domain entity
  ProfitLossAnalytic toDomain() => ProfitLossAnalytic(
    organizationId: organizationId ?? '',
    dateFrom: dateFrom ?? '',
    dateTo: dateTo ?? '',
    groupBy: groupBy ?? 'day',
    summary: summary?.toDomain() ?? ProfitLossAnalyticSummary.empty(),
    data: data?.map((e) => e.toDomain()).toList() ?? [],
    productData: productData?.map((e) => e.toDomain()).toList() ?? [],
  );
}

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

  const factory ProfitLossAnalyticSummaryDto({
    @JsonKey(name: "total_revenue") int? totalRevenue,
    @JsonKey(name: "total_cost") num? totalCost,
    @JsonKey(name: "gross_profit") num? grossProfit,
    @JsonKey(name: "gross_profit_margin") double? grossProfitMargin,
    @JsonKey(name: "total_tax") int? totalTax,
    @JsonKey(name: "total_discount") int? totalDiscount,
    @JsonKey(name: "net_profit") num? netProfit,
    @JsonKey(name: "net_profit_margin") double? netProfitMargin,
    @JsonKey(name: "total_orders") int? totalOrders,
    @JsonKey(name: "average_profit") double? averageProfit,
    @JsonKey(name: "profitability_ratio") double? profitabilityRatio,
  }) = _ProfitLossAnalyticSummaryDto;

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

  // Optional mapper ke domain
  ProfitLossAnalyticSummary toDomain() => ProfitLossAnalyticSummary(
    totalRevenue: totalRevenue ?? 0,
    totalCost: totalCost ?? 0,
    grossProfit: grossProfit ?? 0,
    grossProfitMargin: grossProfitMargin ?? 0.0,
    totalTax: totalTax ?? 0,
    totalDiscount: totalDiscount ?? 0,
    netProfit: netProfit ?? 0,
    netProfitMargin: netProfitMargin ?? 0.0,
    totalOrders: totalOrders ?? 0,
    averageProfit: averageProfit ?? 0.0,
    profitabilityRatio: profitabilityRatio ?? 0.0,
  );
}

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

  const factory ProfitLossAnalyticItemDto({
    @JsonKey(name: "date") String? date,
    @JsonKey(name: "revenue") int? revenue,
    @JsonKey(name: "cost") num? cost,
    @JsonKey(name: "gross_profit") num? grossProfit,
    @JsonKey(name: "gross_profit_margin") double? grossProfitMargin,
    @JsonKey(name: "tax") int? tax,
    @JsonKey(name: "discount") int? discount,
    @JsonKey(name: "net_profit") num? netProfit,
    @JsonKey(name: "net_profit_margin") double? netProfitMargin,
    @JsonKey(name: "orders") int? orders,
  }) = _ProfitLossAnalyticItemDto;

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

  // Optional mapper ke domain
  ProfitLossAnalyticItem toDomain() => ProfitLossAnalyticItem(
    date: date ?? '',
    revenue: revenue ?? 0,
    cost: cost ?? 0,
    grossProfit: grossProfit ?? 0,
    grossProfitMargin: grossProfitMargin ?? 0.0,
    tax: tax ?? 0,
    discount: discount ?? 0,
    netProfit: netProfit ?? 0,
    netProfitMargin: netProfitMargin ?? 0.0,
    orders: orders ?? 0,
  );
}

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

  const factory ProfitLossAnalyticProductDto({
    @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: "cost") num? cost,
    @JsonKey(name: "gross_profit") num? grossProfit,
    @JsonKey(name: "gross_profit_margin") double? grossProfitMargin,
    @JsonKey(name: "average_price") int? averagePrice,
    @JsonKey(name: "average_cost") num? averageCost,
    @JsonKey(name: "profit_per_unit") num? profitPerUnit,
  }) = _ProfitLossAnalyticProductDto;

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

  // Optional mapper ke domain
  ProfitLossAnalyticProduct toDomain() => ProfitLossAnalyticProduct(
    productId: productId ?? '',
    productName: productName ?? '',
    categoryId: categoryId ?? '',
    categoryName: categoryName ?? '',
    quantitySold: quantitySold ?? 0,
    revenue: revenue ?? 0,
    cost: cost ?? 0,
    grossProfit: grossProfit ?? 0,
    grossProfitMargin: grossProfitMargin ?? 0.0,
    averagePrice: averagePrice ?? 0,
    averageCost: averageCost ?? 0,
    profitPerUnit: profitPerUnit ?? 0,
  );
}
