import 'package:em2rp/models/event_model.dart';
import 'package:flutter/material.dart';

class EventStatisticsFilter {
  final DateTimeRange period;
  final Set<String> eventTypeIds;
  final bool includeCanceled;
  final Set<EventStatus> selectedStatuses;

  const EventStatisticsFilter({
    required this.period,
    this.eventTypeIds = const {},
    this.includeCanceled = false,
    this.selectedStatuses = const {
      EventStatus.confirmed,
      EventStatus.waitingForApproval,
    },
  });

  EventStatisticsFilter copyWith({
    DateTimeRange? period,
    Set<String>? eventTypeIds,
    bool? includeCanceled,
    Set<EventStatus>? selectedStatuses,
  }) {
    return EventStatisticsFilter(
      period: period ?? this.period,
      eventTypeIds: eventTypeIds ?? this.eventTypeIds,
      includeCanceled: includeCanceled ?? this.includeCanceled,
      selectedStatuses: selectedStatuses ?? this.selectedStatuses,
    );
  }
}

class EventTypeStatistics {
  final String eventTypeId;
  final String eventTypeName;
  final int totalEvents;
  final double totalAmount;
  final double validatedAmount;
  final double pendingAmount;
  final double canceledAmount;

  const EventTypeStatistics({
    required this.eventTypeId,
    required this.eventTypeName,
    required this.totalEvents,
    required this.totalAmount,
    required this.validatedAmount,
    required this.pendingAmount,
    required this.canceledAmount,
  });
}

class OptionStatistics {
  final String optionKey;
  final String optionLabel;
  final int usageCount;
  final int validatedUsageCount;
  final int quantity;
  final double totalAmount;

  const OptionStatistics({
    required this.optionKey,
    required this.optionLabel,
    required this.usageCount,
    required this.validatedUsageCount,
    required this.quantity,
    required this.totalAmount,
  });
}

class EventStatisticsSummary {
  final int totalEvents;
  final int validatedEvents;
  final int pendingEvents;
  final int canceledEvents;

  final double totalAmount;
  final double validatedAmount;
  final double pendingAmount;
  final double canceledAmount;

  final double baseAmount;
  final double optionsAmount;
  final double medianAmount;
  final List<EventTypeStatistics> byEventType;
  final List<OptionStatistics> topOptions;

  const EventStatisticsSummary({
    required this.totalEvents,
    required this.validatedEvents,
    required this.pendingEvents,
    required this.canceledEvents,
    required this.totalAmount,
    required this.validatedAmount,
    required this.pendingAmount,
    required this.canceledAmount,
    required this.baseAmount,
    required this.optionsAmount,
    required this.medianAmount,
    required this.byEventType,
    required this.topOptions,
  });

  static const empty = EventStatisticsSummary(
    totalEvents: 0,
    validatedEvents: 0,
    pendingEvents: 0,
    canceledEvents: 0,
    totalAmount: 0,
    validatedAmount: 0,
    pendingAmount: 0,
    canceledAmount: 0,
    baseAmount: 0,
    optionsAmount: 0,
    medianAmount: 0,
    byEventType: [],
    topOptions: [],
  );

  double get averageAmount => totalEvents == 0 ? 0 : totalAmount / totalEvents;

  double get validationRate =>
      totalEvents == 0 ? 0 : validatedEvents / totalEvents;

  double get baseContributionRate =>
      totalAmount == 0 ? 0 : baseAmount / totalAmount;

  double get optionsContributionRate =>
      totalAmount == 0 ? 0 : optionsAmount / totalAmount;
}
