/// Cart Data Providers
///
/// State management for cart data layer using Riverpod.
/// Provides access to datasources and repositories.
library;

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:worker/core/database/hive_service.dart';
import 'package:worker/core/network/dio_client.dart';
import 'package:worker/features/cart/data/datasources/cart_local_datasource.dart';
import 'package:worker/features/cart/data/datasources/cart_remote_datasource.dart';
import 'package:worker/features/cart/data/repositories/cart_repository_impl.dart';
import 'package:worker/features/cart/domain/repositories/cart_repository.dart';

part 'cart_data_providers.g.dart';

/// Cart Local DataSource Provider
///
/// Provides instance of CartLocalDataSource.
/// keepAlive: true to persist with cart provider.
@Riverpod(keepAlive: true)
CartLocalDataSource cartLocalDataSource(Ref ref) {
  final hiveService = HiveService();
  return CartLocalDataSourceImpl(hiveService);
}

/// Cart Remote DataSource Provider
///
/// Provides instance of CartRemoteDataSource with DioClient.
/// keepAlive: true to persist with cart provider.
@Riverpod(keepAlive: true)
Future<CartRemoteDataSource> cartRemoteDataSource(Ref ref) async {
  final dioClient = await ref.watch(dioClientProvider.future);
  return CartRemoteDataSourceImpl(dioClient);
}

/// Cart Repository Provider
///
/// Provides instance of CartRepository implementation.
/// keepAlive: true to persist with cart provider.
@Riverpod(keepAlive: true)
Future<CartRepository> cartRepository(Ref ref) async {
  final remoteDataSource = await ref.watch(cartRemoteDataSourceProvider.future);
  final localDataSource = ref.watch(cartLocalDataSourceProvider);

  return CartRepositoryImpl(
    remoteDataSource: remoteDataSource,
    localDataSource: localDataSource,
  );
}
