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: 'outlet_name') String? outletName,
    @JsonKey(name: 'date_from') DateTime? dateFrom,
    @JsonKey(name: 'date_to') DateTime? dateTo,
    @JsonKey(name: 'group_by') String? groupBy,
    @JsonKey(name: 'summary') SalesAnalyticSummaryDto? summary,
    @JsonKey(name: 'data') List<SalesAnalyticDataDto>? data,
  }) = _SalesAnalyticDto;

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

  SalesAnalytic toDomain() => SalesAnalytic(
    organizationId: organizationId ?? '',
    outletId: outletId ?? '',
    outletName: outletName ?? '',
    dateFrom: dateFrom ?? DateTime.fromMillisecondsSinceEpoch(0),
    dateTo: dateTo ?? DateTime.fromMillisecondsSinceEpoch(0),
    groupBy: groupBy ?? '',
    summary: summary?.toDomain() ?? SalesAnalyticSummary.empty(),
    data: data?.map((e) => e.toDomain()).toList() ?? [],
  );
}

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

  const factory SalesAnalyticSummaryDto({
    @JsonKey(name: 'total_sales') num? totalSales,
    @JsonKey(name: 'total_orders') num? totalOrders,
    @JsonKey(name: 'total_items') num? totalItems,
    @JsonKey(name: 'average_order_value') num? averageOrderValue,
    @JsonKey(name: 'total_tax') num? totalTax,
    @JsonKey(name: 'total_discount') num? totalDiscount,
    @JsonKey(name: 'net_sales') num? netSales,
  }) = _SalesAnalyticSummaryDto;

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

  SalesAnalyticSummary toDomain() => SalesAnalyticSummary(
    totalSales: totalSales?.toInt() ?? 0,
    totalOrders: totalOrders?.toInt() ?? 0,
    totalItems: totalItems?.toInt() ?? 0,
    averageOrderValue: averageOrderValue?.toDouble() ?? 0,
    totalTax: totalTax?.toInt() ?? 0,
    totalDiscount: totalDiscount?.toInt() ?? 0,
    netSales: netSales?.toInt() ?? 0,
  );
}

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

  const factory SalesAnalyticDataDto({
    @JsonKey(name: 'date') DateTime? date,
    @JsonKey(name: 'sales') num? sales,
    @JsonKey(name: 'orders') num? orders,
    @JsonKey(name: 'items') num? items,
    @JsonKey(name: 'tax') num? tax,
    @JsonKey(name: 'discount') num? discount,
    @JsonKey(name: 'net_sales') num? netSales,
  }) = _SalesAnalyticDataDto;

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

  SalesAnalyticData toDomain() => SalesAnalyticData(
    date: date ?? DateTime.fromMillisecondsSinceEpoch(0),
    sales: sales?.toInt() ?? 0,
    orders: orders?.toInt() ?? 0,
    items: items?.toInt() ?? 0,
    tax: tax?.toInt() ?? 0,
    discount: discount?.toInt() ?? 0,
    netSales: netSales?.toInt() ?? 0,
  );
}
