/// Provider: Product Filters State
///
/// Manages product filter selections with separate pending and applied states.
/// Pending filters: Updated on every checkbox toggle (no API call)
/// Applied filters: Only updated when Apply button is pressed (triggers API)
library;

import 'package:flutter_riverpod/flutter_riverpod.dart';

/// Product Filters State
class ProductFiltersState {
  final Set<String> productLines;
  final Set<String> sizes;
  final Set<String> surfaces;
  final Set<String> colors;
  final Set<String> brands;

  const ProductFiltersState({
    this.productLines = const {},
    this.sizes = const {},
    this.surfaces = const {},
    this.colors = const {},
    this.brands = const {},
  });

  ProductFiltersState copyWith({
    Set<String>? productLines,
    Set<String>? sizes,
    Set<String>? surfaces,
    Set<String>? colors,
    Set<String>? brands,
  }) {
    return ProductFiltersState(
      productLines: productLines ?? this.productLines,
      sizes: sizes ?? this.sizes,
      surfaces: surfaces ?? this.surfaces,
      colors: colors ?? this.colors,
      brands: brands ?? this.brands,
    );
  }

  /// Get total filter count
  int get totalCount =>
      productLines.length +
      sizes.length +
      surfaces.length +
      colors.length +
      brands.length;

  /// Check if any filters are active
  bool get hasActiveFilters => totalCount > 0;

  /// Reset all filters
  ProductFiltersState reset() {
    return const ProductFiltersState();
  }
}

/// Product Filters Notifier (Pending Filters - for UI selection)
///
/// This provider stores the PENDING filter selections in the drawer.
/// Changes here do NOT trigger API calls.
class ProductFiltersNotifier extends Notifier<ProductFiltersState> {
  @override
  ProductFiltersState build() => const ProductFiltersState();

  /// Toggle product line filter
  void toggleProductLine(String value) {
    final newSet = Set<String>.from(state.productLines);
    if (newSet.contains(value)) {
      newSet.remove(value);
    } else {
      newSet.add(value);
    }
    state = state.copyWith(productLines: newSet);
  }

  /// Toggle size filter
  void toggleSize(String value) {
    final newSet = Set<String>.from(state.sizes);
    if (newSet.contains(value)) {
      newSet.remove(value);
    } else {
      newSet.add(value);
    }
    state = state.copyWith(sizes: newSet);
  }

  /// Toggle surface filter
  void toggleSurface(String value) {
    final newSet = Set<String>.from(state.surfaces);
    if (newSet.contains(value)) {
      newSet.remove(value);
    } else {
      newSet.add(value);
    }
    state = state.copyWith(surfaces: newSet);
  }

  /// Toggle color filter
  void toggleColor(String value) {
    final newSet = Set<String>.from(state.colors);
    if (newSet.contains(value)) {
      newSet.remove(value);
    } else {
      newSet.add(value);
    }
    state = state.copyWith(colors: newSet);
  }

  /// Toggle brand filter
  void toggleBrand(String value) {
    final newSet = Set<String>.from(state.brands);
    if (newSet.contains(value)) {
      newSet.remove(value);
    } else {
      newSet.add(value);
    }
    state = state.copyWith(brands: newSet);
  }

  /// Reset all filters (both pending and applied)
  void reset() {
    state = const ProductFiltersState();
    // Also reset applied filters
    ref.read(appliedProductFiltersProvider.notifier).reset();
  }

  /// Apply filters - copies pending state to applied state
  /// This is the ONLY action that triggers API calls
  void apply() {
    ref.read(appliedProductFiltersProvider.notifier).applyFilters(state);
  }

  /// Sync pending state with applied state (when opening drawer)
  void syncWithApplied() {
    state = ref.read(appliedProductFiltersProvider);
  }
}

/// Applied Product Filters Notifier (Triggers API calls)
///
/// This provider stores the APPLIED filter state.
/// The products provider watches THIS provider, not the pending one.
class AppliedProductFiltersNotifier extends Notifier<ProductFiltersState> {
  @override
  ProductFiltersState build() => const ProductFiltersState();

  /// Apply filters from pending state
  void applyFilters(ProductFiltersState filters) {
    state = filters;
  }

  /// Reset applied filters
  void reset() {
    state = const ProductFiltersState();
  }
}

/// Product Filters Provider (Pending - for drawer UI)
final productFiltersProvider =
    NotifierProvider<ProductFiltersNotifier, ProductFiltersState>(
  ProductFiltersNotifier.new,
);

/// Applied Product Filters Provider (Triggers API)
final appliedProductFiltersProvider =
    NotifierProvider<AppliedProductFiltersNotifier, ProductFiltersState>(
  AppliedProductFiltersNotifier.new,
);
