import 'package:rasadyar_auth/data/services/token_storage_service.dart';
import 'package:rasadyar_chicken/data/repositories/chicken_repository_imp.dart';
import 'package:rasadyar_core/core.dart';

GetIt diChicken = GetIt.instance;

Future<void> setupChickenDI() async {
  var tokenService = Get.find<TokenStorageService>();

  diChicken.registerLazySingleton<AppInterceptor>(
    () => AppInterceptor(
      refreshTokenCallback: () async {},
      saveTokenCallback: (String newToken) async {
        await tokenService.saveAccessToken(newToken);
      },
      clearTokenCallback: () async {
        await tokenService.deleteTokens();
      },
    ),
    instanceName: 'chickenInterceptor',
  );

  tokenService.getBaseUrl();
  diChicken.registerLazySingleton<DioRemote>(() {
    return DioRemote(
      baseUrl: tokenService.baseurl.value,
      interceptors: diChicken.get<AppInterceptor>(instanceName: 'chickenInterceptor'),
    );
  }, instanceName: 'chickenDioRemote');

  final dioRemote = diChicken.get<DioRemote>(instanceName: 'chickenDioRemote');
  await dioRemote.init();
  diChicken.registerLazySingleton<ChickenRepositoryImpl>(() => ChickenRepositoryImpl(dioRemote));

  diChicken.registerSingleton(ImagePicker());
}
