part of '../analytic_dtos.dart';

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

  const factory SalesAnalyticDto({
    @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: "group_by") String? groupBy,
    @JsonKey(name: "summary") SalesSummaryDto? summary,
    @JsonKey(name: "data") List<SalesAnalyticItemDto>? data,
  }) = _SalesAnalyticDto;

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

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

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

  const factory SalesSummaryDto({
    @JsonKey(name: "total_sales") int? totalSales,
    @JsonKey(name: "total_orders") int? totalOrders,
    @JsonKey(name: "total_items") int? totalItems,
    @JsonKey(name: "average_order_value") double? averageOrderValue,
    @JsonKey(name: "total_tax") int? totalTax,
    @JsonKey(name: "total_discount") int? totalDiscount,
    @JsonKey(name: "net_sales") int? netSales,
  }) = _SalesSummaryDto;

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

  // Optional mapping ke domain
  SalesSummary toDomain() => SalesSummary(
    totalSales: totalSales ?? 0,
    totalOrders: totalOrders ?? 0,
    totalItems: totalItems ?? 0,
    averageOrderValue: averageOrderValue ?? 0.0,
    totalTax: totalTax ?? 0,
    totalDiscount: totalDiscount ?? 0,
    netSales: netSales ?? 0,
  );
}

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

  const factory SalesAnalyticItemDto({
    @JsonKey(name: "date") DateTime? date,
    @JsonKey(name: "sales") int? sales,
    @JsonKey(name: "orders") int? orders,
    @JsonKey(name: "items") int? items,
    @JsonKey(name: "tax") int? tax,
    @JsonKey(name: "discount") int? discount,
    @JsonKey(name: "net_sales") int? netSales,
  }) = _SalesAnalyticItemDto;

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

  // Optional mapping ke domain
  SalesAnalyticItem toDomain() => SalesAnalyticItem(
    date: date ?? DateTime.now(),
    sales: sales ?? 0,
    orders: orders ?? 0,
    items: items ?? 0,
    tax: tax ?? 0,
    discount: discount ?? 0,
    netSales: netSales ?? 0,
  );
}
