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

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

  PaymentMethodAnalytic toDomain() {
    return PaymentMethodAnalytic(
      organizationId: organizationId ?? '',
      outletId: outletId ?? '',
      outletName: outletName ?? '',
      dateFrom: dateFrom ?? '',
      dateTo: dateTo ?? '',
      groupBy: groupBy ?? '',
      summary: summary?.toDomain() ?? PaymentMethodSummary.empty(),
      data: data?.map((e) => e.toDomain()).toList() ?? [],
    );
  }
}

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

  const factory PaymentMethodItemDto({
    @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') num? totalAmount,
    @JsonKey(name: 'order_count') int? orderCount,
    @JsonKey(name: 'payment_count') int? paymentCount,
    @JsonKey(name: 'percentage') double? percentage,
  }) = _PaymentMethodItemDto;

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

  PaymentMethodItem toDomain() {
    return PaymentMethodItem(
      paymentMethodId: paymentMethodId ?? '',
      paymentMethodName: paymentMethodName ?? '',
      paymentMethodType: paymentMethodType ?? '',
      totalAmount: totalAmount ?? 0,
      orderCount: orderCount ?? 0,
      paymentCount: paymentCount ?? 0,
      percentage: percentage ?? 0.0,
    );
  }
}

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

  const factory PaymentMethodSummaryDto({
    @JsonKey(name: 'total_amount') num? totalAmount,
    @JsonKey(name: 'total_orders') int? totalOrders,
    @JsonKey(name: 'total_payments') int? totalPayments,
    @JsonKey(name: 'average_order_value') double? averageOrderValue,
  }) = _PaymentMethodSummaryDto;

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

  PaymentMethodSummary toDomain() {
    return PaymentMethodSummary(
      totalAmount: totalAmount ?? 0,
      totalOrders: totalOrders ?? 0,
      totalPayments: totalPayments ?? 0,
      averageOrderValue: averageOrderValue ?? 0.0,
    );
  }
}
