import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../domain/entities/warehouse_entity.dart';
import '../../domain/usecases/get_warehouses_usecase.dart';

/// Warehouse state that holds the current state of warehouse feature
class WarehouseState {
  final List<WarehouseEntity> warehouses;
  final WarehouseEntity? selectedWarehouse;
  final bool isLoading;
  final String? error;

  const WarehouseState({
    this.warehouses = const [],
    this.selectedWarehouse,
    this.isLoading = false,
    this.error,
  });

  /// Create initial state
  factory WarehouseState.initial() {
    return const WarehouseState();
  }

  /// Create loading state
  WarehouseState setLoading() {
    return WarehouseState(
      warehouses: warehouses,
      selectedWarehouse: selectedWarehouse,
      isLoading: true,
      error: null,
    );
  }

  /// Create success state
  WarehouseState setSuccess(List<WarehouseEntity> warehouses) {
    return WarehouseState(
      warehouses: warehouses,
      selectedWarehouse: selectedWarehouse,
      isLoading: false,
      error: null,
    );
  }

  /// Create error state
  WarehouseState setError(String error) {
    return WarehouseState(
      warehouses: warehouses,
      selectedWarehouse: selectedWarehouse,
      isLoading: false,
      error: error,
    );
  }

  /// Create state with selected warehouse
  WarehouseState setSelectedWarehouse(WarehouseEntity? warehouse) {
    return WarehouseState(
      warehouses: warehouses,
      selectedWarehouse: warehouse,
      isLoading: isLoading,
      error: error,
    );
  }

  /// Create a copy with modified fields
  WarehouseState copyWith({
    List<WarehouseEntity>? warehouses,
    WarehouseEntity? selectedWarehouse,
    bool? isLoading,
    String? error,
  }) {
    return WarehouseState(
      warehouses: warehouses ?? this.warehouses,
      selectedWarehouse: selectedWarehouse ?? this.selectedWarehouse,
      isLoading: isLoading ?? this.isLoading,
      error: error,
    );
  }

  /// Check if warehouses are loaded
  bool get hasWarehouses => warehouses.isNotEmpty;

  /// Check if a warehouse is selected
  bool get hasSelection => selectedWarehouse != null;

  @override
  String toString() {
    return 'WarehouseState(warehouses: ${warehouses.length}, '
        'selectedWarehouse: ${selectedWarehouse?.name}, '
        'isLoading: $isLoading, error: $error)';
  }
}

/// State notifier for warehouse operations
/// Manages the warehouse state and handles business logic
class WarehouseNotifier extends StateNotifier<WarehouseState> {
  final GetWarehousesUseCase getWarehousesUseCase;

  WarehouseNotifier(this.getWarehousesUseCase)
      : super(WarehouseState.initial());

  /// Load all warehouses from the API
  Future<void> loadWarehouses() async {
    // Set loading state
    state = state.setLoading();

    // Execute the use case
    final result = await getWarehousesUseCase();

    // Handle the result
    result.fold(
      (failure) {
        // Set error state on failure
        state = state.setError(failure.message);
      },
      (warehouses) {
        // Set success state with warehouses
        state = state.setSuccess(warehouses);
      },
    );
  }

  /// Select a warehouse
  void selectWarehouse(WarehouseEntity warehouse) {
    state = state.setSelectedWarehouse(warehouse);
  }

  /// Clear selected warehouse
  void clearSelection() {
    state = state.setSelectedWarehouse(null);
  }

  /// Refresh warehouses (reload from API)
  Future<void> refresh() async {
    await loadWarehouses();
  }

  /// Clear error message
  void clearError() {
    state = state.copyWith(error: null);
  }

  /// Reset state to initial
  void reset() {
    state = WarehouseState.initial();
  }
}
