import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../domain/entities/product_entity.dart';
import '../../domain/usecases/get_products_usecase.dart';

/// Products state class
/// Holds the current state of the products feature
class ProductsState {
  final List<ProductEntity> products;
  final String operationType;
  final int? warehouseId;
  final String? warehouseName;
  final bool isLoading;
  final String? error;

  const ProductsState({
    this.products = const [],
    this.operationType = 'import',
    this.warehouseId,
    this.warehouseName,
    this.isLoading = false,
    this.error,
  });

  ProductsState copyWith({
    List<ProductEntity>? products,
    String? operationType,
    int? warehouseId,
    String? warehouseName,
    bool? isLoading,
    String? error,
  }) {
    return ProductsState(
      products: products ?? this.products,
      operationType: operationType ?? this.operationType,
      warehouseId: warehouseId ?? this.warehouseId,
      warehouseName: warehouseName ?? this.warehouseName,
      isLoading: isLoading ?? this.isLoading,
      error: error,
    );
  }
}

/// Products notifier
/// Manages the products state and business logic
class ProductsNotifier extends StateNotifier<ProductsState> {
  final GetProductsUseCase getProductsUseCase;

  ProductsNotifier(this.getProductsUseCase) : super(const ProductsState());

  /// Load products for a specific warehouse and operation type
  ///
  /// [warehouseId] - The ID of the warehouse
  /// [warehouseName] - The name of the warehouse (for display)
  /// [type] - The operation type ('import' or 'export')
  /// [forceRefresh] - If true, bypass cache and fetch from API
  Future<void> loadProducts(
    int warehouseId,
    String warehouseName,
    String type, {
    bool forceRefresh = false,
  }) async {
    // Set loading state
    state = state.copyWith(
      isLoading: true,
      error: null,
      warehouseId: warehouseId,
      warehouseName: warehouseName,
      operationType: type,
    );

    // Call the use case with forceRefresh flag
    final result = await getProductsUseCase(
      warehouseId,
      type,
      forceRefresh: forceRefresh,
    );

    // Handle the result
    result.fold(
      (failure) {
        // Handle failure
        state = state.copyWith(
          isLoading: false,
          error: failure.message,
          products: [],
        );
      },
      (products) {
        // Handle success
        state = state.copyWith(
          isLoading: false,
          error: null,
          products: products,
        );
      },
    );
  }

  /// Clear products list
  void clearProducts() {
    state = const ProductsState();
  }

  /// Refresh products - forces fetch from API
  Future<void> refreshProducts() async {
    if (state.warehouseId != null) {
      await loadProducts(
        state.warehouseId!,
        state.warehouseName ?? '',
        state.operationType,
        forceRefresh: true, // Always force refresh when explicitly requested
      );
    }
  }
}
