part of 'operation_list_cubit.dart';

enum OperationListStatus { initial, loading, success, failure }

class OperationListState extends Equatable {
  final OperationListStatus status;
  final List<OperationModel> operations;
  final String? errorMessage;

  // Paginazione Ibrida
  final int currentPage;
  final int itemsPerPage;
  final int totalItems;
  final bool hasReachedMax;

  // 🥷 I FILTRI MANCANTI (Riparati!)
  final String? searchTerm;
  final DateTimeRange? dateRange;

  const OperationListState({
    this.status = OperationListStatus.initial,
    this.operations = const [],
    this.errorMessage,
    this.currentPage = 1,
    this.itemsPerPage = 25,
    this.totalItems = 0,
    this.hasReachedMax = false,
    this.searchTerm,
    this.dateRange,
  });

  int get totalPages => (totalItems / itemsPerPage).ceil();

  // 🥷 COPYWITH AVANZATO: Gestisce lo sbiancamento dei filtri alla perfezione
  OperationListState copyWith({
    OperationListStatus? status,
    List<OperationModel>? operations,
    String? errorMessage,
    int? currentPage,
    int? itemsPerPage,
    int? totalItems,
    bool? hasReachedMax,
    String? Function()? searchTerm, // Callback per gestire il null esplicito
    DateTimeRange? Function()?
    dateRange, // Callback per gestire il null esplicito
  }) {
    return OperationListState(
      status: status ?? this.status,
      operations: operations ?? this.operations,
      errorMessage: errorMessage ?? this.errorMessage,
      currentPage: currentPage ?? this.currentPage,
      itemsPerPage: itemsPerPage ?? this.itemsPerPage,
      totalItems: totalItems ?? this.totalItems,
      hasReachedMax: hasReachedMax ?? this.hasReachedMax,

      // Se passi la funzione la eseguiamo, altrimenti teniamo il valore corrente
      searchTerm: searchTerm != null ? searchTerm() : this.searchTerm,
      dateRange: dateRange != null ? dateRange() : this.dateRange,
    );
  }

  @override
  List<Object?> get props => [
    status,
    operations,
    errorMessage,
    currentPage,
    itemsPerPage,
    totalItems,
    hasReachedMax,
    searchTerm,
    dateRange,
  ];
}
