part of '../analytic_dtos.dart';

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

  const factory PaymentMethodAnalyticDto({
    @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") PaymentMethodAnalyticSummaryDto? summary,
    @JsonKey(name: "data") List<PaymentMethodAnalyticItemDto>? data,
  }) = _PaymentMethodAnalyticDto;

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

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

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

  const factory PaymentMethodAnalyticItemDto({
    @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") num? percentage,
  }) = _PaymentMethodAnalyticItemDto;

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

  // Optional mapper ke domain
  PaymentMethodAnalyticItem toDomain() => PaymentMethodAnalyticItem(
    paymentMethodId: paymentMethodId ?? '',
    paymentMethodName: paymentMethodName ?? '',
    paymentMethodType: paymentMethodType ?? '',
    totalAmount: totalAmount ?? 0,
    orderCount: orderCount ?? 0,
    paymentCount: paymentCount ?? 0,
    percentage: percentage ?? 0,
  );
}

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

  const factory PaymentMethodAnalyticSummaryDto({
    @JsonKey(name: "total_amount") int? totalAmount,
    @JsonKey(name: "total_orders") int? totalOrders,
    @JsonKey(name: "total_payments") int? totalPayments,
    @JsonKey(name: "average_order_value") double? averageOrderValue,
  }) = _PaymentMethodAnalyticSummaryDto;

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

  // Optional mapping ke domain entity
  PaymentMethodAnalyticSummary toDomain() => PaymentMethodAnalyticSummary(
    totalAmount: totalAmount ?? 0,
    totalOrders: totalOrders ?? 0,
    totalPayments: totalPayments ?? 0,
    averageOrderValue: averageOrderValue ?? 0.0,
  );
}
