import 'package:rasadyar_auth/data/models/local/user_local/user_local_model.dart';
import 'package:rasadyar_auth/data/services/token_storage_service.dart';
import 'package:rasadyar_auth/presentation/routes/pages.dart';
import 'package:rasadyar_chicken/data/datasource/local/chicken_local_imp.dart';
import 'package:rasadyar_chicken/data/datasource/remote/chicken_remote_imp.dart';
import 'package:rasadyar_chicken/data/repositories/chicken_repository_imp.dart';
import 'package:rasadyar_chicken/hive_registrar.g.dart';
import 'package:rasadyar_core/core.dart';

GetIt diChicken = GetIt.instance;

Future<void> setupChickenDI() async {
  var tokenService = Get.find<TokenStorageService>();
  Hive.registerAdapters();
  diChicken.registerLazySingleton<ChickenLocalDataSourceImp>(() => ChickenLocalDataSourceImp());
  diChicken.get<ChickenLocalDataSourceImp>().openBox();

  diChicken.registerLazySingleton<AppInterceptor>(
    () => AppInterceptor(
      refreshTokenCallback: () async {},
      saveTokenCallback: (String newToken) async {
        await tokenService.saveAccessToken(newToken);
      },
      clearTokenCallback: () async {
        await tokenService.deleteTokens();
        Get.offAllNamed(AuthPaths.auth, arguments: Module.chicken);
      },
    ),
    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(() => ChickenRemoteDatasourceImp(dioRemote));

  diChicken.registerLazySingleton<ChickenRepositoryImp>(
    () => ChickenRepositoryImp(
      local: diChicken.get<ChickenLocalDataSourceImp>(),
      remote: diChicken.get<ChickenRemoteDatasourceImp>(),
    ),
  );

  diChicken.registerSingleton(ImagePicker());
}
