import 'package:flutter/material.dart';
import 'package:pshared/models/payment/status.dart';

import 'package:pweb/models/wallet_transaction.dart';
import 'package:pweb/services/wallet_transactions.dart';


class WalletTransactionsProvider extends ChangeNotifier {
  final WalletTransactionsService _service;

  WalletTransactionsProvider(this._service);

  List<WalletTransaction> _transactions = [];
  List<WalletTransaction> _filteredTransactions = [];
  DateTimeRange? _dateRange;
  final Set<OperationStatus> _selectedStatuses = {};
  final Set<WalletTransactionType> _selectedTypes = {};
  String? _walletId;
  bool _isLoading = false;
  String? _error;

  List<WalletTransaction> get transactions => _transactions;
  List<WalletTransaction> get filteredTransactions => _filteredTransactions;
  DateTimeRange? get dateRange => _dateRange;
  Set<OperationStatus> get selectedStatuses => _selectedStatuses;
  Set<WalletTransactionType> get selectedTypes => _selectedTypes;
  String? get walletId => _walletId;
  bool get isLoading => _isLoading;
  String? get error => _error;
  bool get hasFilters =>
      _dateRange != null ||
      _selectedStatuses.isNotEmpty ||
      _selectedTypes.isNotEmpty;

  Future<void> load({String? walletId}) async {
    _isLoading = true;
    _error = null;
    notifyListeners();

    try {
      _walletId = walletId ?? _walletId;
      _transactions = await _service.fetchHistory(walletId: _walletId);
      _applyFilters(notify: false);
      _isLoading = false;
      notifyListeners();
    } catch (e) {
      _error = e.toString();
      _isLoading = false;
      notifyListeners();
    }
  }

  void setWallet(String walletId) {
    _walletId = walletId;
    _applyFilters();
  }

  void setDateRange(DateTimeRange? range) {
    _dateRange = range;
    _applyFilters();
  }

  void toggleStatus(OperationStatus status) {
    if (_selectedStatuses.contains(status)) {
      _selectedStatuses.remove(status);
    } else {
      _selectedStatuses.add(status);
    }
    _applyFilters();
  }

  void toggleType(WalletTransactionType type) {
    if (_selectedTypes.contains(type)) {
      _selectedTypes.remove(type);
    } else {
      _selectedTypes.add(type);
    }
    _applyFilters();
  }

  void resetFilters() {
    _dateRange = null;
    _selectedStatuses.clear();
    _selectedTypes.clear();
    _applyFilters();
  }

  void _applyFilters({bool notify = true}) {
    _filteredTransactions = _transactions.where((tx) {
      final walletMatch = _walletId == null || tx.walletId == _walletId;
      final statusMatch =
          _selectedStatuses.isEmpty || _selectedStatuses.contains(tx.status);
      final typeMatch =
          _selectedTypes.isEmpty || _selectedTypes.contains(tx.type);
      final dateMatch = _dateRange == null ||
          (tx.date.isAfter(_dateRange!.start.subtract(const Duration(seconds: 1))) &&
              tx.date.isBefore(_dateRange!.end.add(const Duration(seconds: 1))));

      return walletMatch && statusMatch && typeMatch && dateMatch;
    }).toList();

    if (notify) notifyListeners();
  }
}
