part of '../analytic.dart';

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

  const factory SalesAnalytic({
    required String organizationId,
    required String outletId,
    required DateTime dateFrom,
    required DateTime dateTo,
    required String groupBy,
    required SalesSummary summary,
    required List<SalesAnalyticItem> data,
  }) = _SalesAnalytic;

  factory SalesAnalytic.empty() => SalesAnalytic(
    organizationId: '',
    outletId: '',
    dateFrom: DateTime.now(),
    dateTo: DateTime.now(),
    groupBy: '',
    summary: SalesSummary.empty(),
    data: const [],
  );

  List<SalesAnalyticItem> get sortedDailyData {
    final sortedList = List<SalesAnalyticItem>.from(data);
    sortedList.sort((a, b) => b.sales.compareTo(a.sales));
    return sortedList;
  }

  SalesAnalyticItem get highestRevenueDay {
    if (data.isEmpty) {
      return SalesAnalyticItem.empty();
    }
    return sortedDailyData.first;
  }
}

@freezed
class SalesSummary with _$SalesSummary {
  const factory SalesSummary({
    required int totalSales,
    required int totalOrders,
    required int totalItems,
    required double averageOrderValue,
    required int totalTax,
    required int totalDiscount,
    required int netSales,
  }) = _SalesSummary;

  factory SalesSummary.empty() => const SalesSummary(
    totalSales: 0,
    totalOrders: 0,
    totalItems: 0,
    averageOrderValue: 0.0,
    totalTax: 0,
    totalDiscount: 0,
    netSales: 0,
  );
}

@freezed
class SalesAnalyticItem with _$SalesAnalyticItem {
  const factory SalesAnalyticItem({
    required DateTime date,
    required int sales,
    required int orders,
    required int items,
    required int tax,
    required int discount,
    required int netSales,
  }) = _SalesAnalyticItem;

  factory SalesAnalyticItem.empty() => SalesAnalyticItem(
    date: DateTime.now(),
    sales: 0,
    orders: 0,
    items: 0,
    tax: 0,
    discount: 0,
    netSales: 0,
  );
}
