/// EXAMPLE FILE: How to set up the warehouse provider
///
/// This file demonstrates how to wire up all the warehouse feature dependencies
/// using Riverpod providers. Copy this setup to your actual provider configuration file.

import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/network/api_client.dart';
import '../../core/storage/secure_storage.dart';
import 'data/datasources/warehouse_remote_datasource.dart';
import 'data/repositories/warehouse_repository_impl.dart';
import 'domain/usecases/get_warehouses_usecase.dart';
import 'presentation/providers/warehouse_provider.dart';

// ==============================================================================
// STEP 1: Provide core dependencies (ApiClient, SecureStorage)
// ==============================================================================
// These should already be set up in your app. If not, add them to your
// core providers file (e.g., lib/core/di/providers.dart or lib/core/providers.dart)

// Provider for SecureStorage
final secureStorageProvider = Provider<SecureStorage>((ref) {
  return SecureStorage();
});

// Provider for ApiClient
final apiClientProvider = Provider<ApiClient>((ref) {
  final secureStorage = ref.watch(secureStorageProvider);
  return ApiClient(secureStorage);
});

// ==============================================================================
// STEP 2: Data Layer Providers
// ==============================================================================

// Provider for WarehouseRemoteDataSource
final warehouseRemoteDataSourceProvider = Provider<WarehouseRemoteDataSource>((ref) {
  final apiClient = ref.watch(apiClientProvider);
  return WarehouseRemoteDataSourceImpl(apiClient);
});

// Provider for WarehouseRepository
final warehouseRepositoryProvider = Provider((ref) {
  final remoteDataSource = ref.watch(warehouseRemoteDataSourceProvider);
  return WarehouseRepositoryImpl(remoteDataSource);
});

// ==============================================================================
// STEP 3: Domain Layer Providers (Use Cases)
// ==============================================================================

// Provider for GetWarehousesUseCase
final getWarehousesUseCaseProvider = Provider((ref) {
  final repository = ref.watch(warehouseRepositoryProvider);
  return GetWarehousesUseCase(repository);
});

// ==============================================================================
// STEP 4: Presentation Layer Providers (State Management)
// ==============================================================================

// Provider for WarehouseNotifier (StateNotifier)
final warehouseProvider = StateNotifierProvider<WarehouseNotifier, WarehouseState>((ref) {
  final getWarehousesUseCase = ref.watch(getWarehousesUseCaseProvider);
  return WarehouseNotifier(getWarehousesUseCase);
});

// ==============================================================================
// USAGE IN WIDGETS
// ==============================================================================

/*
// Example 1: In WarehouseSelectionPage
class WarehouseSelectionPage extends ConsumerStatefulWidget {
  const WarehouseSelectionPage({super.key});

  @override
  ConsumerState<WarehouseSelectionPage> createState() =>
      _WarehouseSelectionPageState();
}

class _WarehouseSelectionPageState extends ConsumerState<WarehouseSelectionPage> {
  @override
  void initState() {
    super.initState();
    // Load warehouses when page is created
    WidgetsBinding.instance.addPostFrameCallback((_) {
      ref.read(warehouseProvider.notifier).loadWarehouses();
    });
  }

  @override
  Widget build(BuildContext context) {
    final state = ref.watch(warehouseProvider);

    return Scaffold(
      appBar: AppBar(title: const Text('Select Warehouse')),
      body: state.isLoading
          ? const Center(child: CircularProgressIndicator())
          : state.error != null
              ? Center(child: Text('Error: ${state.error}'))
              : ListView.builder(
                  itemCount: state.warehouses.length,
                  itemBuilder: (context, index) {
                    final warehouse = state.warehouses[index];
                    return ListTile(
                      title: Text(warehouse.name),
                      subtitle: Text('Code: ${warehouse.code}'),
                      onTap: () {
                        ref.read(warehouseProvider.notifier).selectWarehouse(warehouse);
                        // Navigate to next screen
                        context.push('/operations', extra: warehouse);
                      },
                    );
                  },
                ),
    );
  }
}

// Example 2: Accessing selected warehouse in another page
class OperationSelectionPage extends ConsumerWidget {
  const OperationSelectionPage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final state = ref.watch(warehouseProvider);
    final selectedWarehouse = state.selectedWarehouse;

    return Scaffold(
      appBar: AppBar(
        title: Text('Warehouse: ${selectedWarehouse?.code ?? 'None'}'),
      ),
      body: Center(
        child: Text('Selected: ${selectedWarehouse?.name ?? 'No selection'}'),
      ),
    );
  }
}

// Example 3: Manually loading warehouses on button press
ElevatedButton(
  onPressed: () {
    ref.read(warehouseProvider.notifier).loadWarehouses();
  },
  child: const Text('Load Warehouses'),
);

// Example 4: Refresh warehouses
RefreshIndicator(
  onRefresh: () => ref.read(warehouseProvider.notifier).refresh(),
  child: ListView(...),
);
*/
