part of '../analytic_dtos.dart';

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

  const factory DashboardAnalyticDto({
    @JsonKey(name: 'organization_id') String? organizationId,
    @JsonKey(name: 'outlet_id') String? outletId,
    @JsonKey(name: 'outlet_name') String? outletName,
    @JsonKey(name: 'date_from') String? dateFrom,
    @JsonKey(name: 'date_to') String? dateTo,
    @JsonKey(name: 'overview') DashboardOverviewDto? overview,
    @JsonKey(name: 'top_products') List<DashboardTopProductDto>? topProducts,
    @JsonKey(name: 'payment_methods')
    List<DashboardPaymentMethodDto>? paymentMethods,
    @JsonKey(name: 'recent_sales') List<DashboardRecentSaleDto>? recentSales,
  }) = _DashboardAnalyticDto;

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

  DashboardAnalytic toDomain() => DashboardAnalytic(
    organizationId: organizationId ?? '',
    outletId: outletId ?? '',
    outletName: outletName ?? '',
    dateFrom: dateFrom ?? '',
    dateTo: dateTo ?? '',
    overview: overview?.toDomain() ?? DashboardOverview.empty(),
    topProducts: topProducts?.map((e) => e.toDomain()).toList() ?? [],
    paymentMethods: paymentMethods?.map((e) => e.toDomain()).toList() ?? [],
    recentSales: recentSales?.map((e) => e.toDomain()).toList() ?? [],
  );
}

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

  const factory DashboardOverviewDto({
    @JsonKey(name: 'total_sales') int? totalSales,
    @JsonKey(name: 'total_orders') int? totalOrders,
    @JsonKey(name: 'average_order_value') double? averageOrderValue,
    @JsonKey(name: 'total_customers') int? totalCustomers,
    @JsonKey(name: 'voided_orders') int? voidedOrders,
    @JsonKey(name: 'refunded_orders') int? refundedOrders,
    @JsonKey(name: 'total_item_sold') int? totalItemSold,
    @JsonKey(name: 'total_low_stock') int? totalLowStock,
    @JsonKey(name: 'total_product_active') int? totalProductActive,
  }) = _DashboardOverviewDto;

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

  DashboardOverview toDomain() => DashboardOverview(
    totalSales: totalSales ?? 0,
    totalOrders: totalOrders ?? 0,
    averageOrderValue: averageOrderValue ?? 0.0,
    totalCustomers: totalCustomers ?? 0,
    voidedOrders: voidedOrders ?? 0,
    refundedOrders: refundedOrders ?? 0,
    totalItemSold: totalItemSold ?? 0,
    totalLowStock: totalLowStock ?? 0,
    totalProductActive: totalProductActive ?? 0,
  );
}

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

  const factory DashboardTopProductDto({
    @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: 'average_price') double? averagePrice,
    @JsonKey(name: 'order_count') int? orderCount,
  }) = _DashboardTopProductDto;

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

  DashboardTopProduct toDomain() => DashboardTopProduct(
    productId: productId ?? '',
    productName: productName ?? '',
    categoryId: categoryId ?? '',
    categoryName: categoryName ?? '',
    quantitySold: quantitySold ?? 0,
    revenue: revenue ?? 0,
    averagePrice: averagePrice ?? 0.0,
    orderCount: orderCount ?? 0,
  );
}

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

  const factory DashboardPaymentMethodDto({
    @JsonKey(name: 'payment_method_id') String? paymentMethodId,
    @JsonKey(name: 'payment_method_name') String? paymentMethodName,
    @JsonKey(name: 'payment_method_type') String? paymentMethodType,
    @JsonKey(name: 'total_amount') int? totalAmount,
    @JsonKey(name: 'order_count') int? orderCount,
    @JsonKey(name: 'payment_count') int? paymentCount,
    @JsonKey(name: 'percentage') double? percentage,
  }) = _DashboardPaymentMethodDto;

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

  DashboardPaymentMethod toDomain() => DashboardPaymentMethod(
    paymentMethodId: paymentMethodId ?? '',
    paymentMethodName: paymentMethodName ?? '',
    paymentMethodType: paymentMethodType ?? '',
    totalAmount: totalAmount ?? 0,
    orderCount: orderCount ?? 0,
    paymentCount: paymentCount ?? 0,
    percentage: percentage ?? 0.0,
  );
}

/// ===================== RECENT SALE DTO =====================
@freezed
class DashboardRecentSaleDto with _$DashboardRecentSaleDto {
  const DashboardRecentSaleDto._();

  const factory DashboardRecentSaleDto({
    @JsonKey(name: 'date') String? 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,
  }) = _DashboardRecentSaleDto;

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

  DashboardRecentSale toDomain() => DashboardRecentSale(
    date: date ?? '',
    sales: sales ?? 0,
    orders: orders ?? 0,
    items: items ?? 0,
    tax: tax ?? 0,
    discount: discount ?? 0,
    netSales: netSales ?? 0,
  );
}
