import 'package:flutter/material.dart';
import 'package:kmobile/api/services/branch_service.dart';
import 'package:kmobile/api/services/cheque_service.dart';
import 'package:kmobile/api/services/limit_service.dart';
import 'package:kmobile/api/services/rtgs_service.dart';
import 'package:kmobile/api/services/neft_service.dart';
import 'package:kmobile/api/services/imps_service.dart';
import 'package:get_it/get_it.dart';
import 'package:dio/dio.dart';
import 'package:kmobile/api/services/beneficiary_service.dart';
import 'package:kmobile/api/services/payment_service.dart';
import 'package:kmobile/api/services/user_service.dart';
import 'package:kmobile/api/services/yojna_service.dart';
import 'package:kmobile/data/repositories/transaction_repository.dart';
import 'package:kmobile/features/auth/controllers/theme_cubit.dart';
import 'package:kmobile/features/auth/controllers/theme_mode_cubit.dart';
import '../api/services/auth_service.dart';
import '../api/interceptors/auth_interceptor.dart';
import '../api/services/change_password_service.dart';
import '../data/repositories/auth_repository.dart';
import '../features/auth/controllers/auth_cubit.dart';
import '../security/secure_storage.dart';

final getIt = GetIt.instance;

Future<void> setupDependencies() async {
  getIt.registerSingleton<RouteObserver<ModalRoute<void>>>(
      RouteObserver<ModalRoute<void>>());
  //getIt.registerLazySingleton<ThemeController>(() => ThemeController());
  //getIt.registerLazySingleton<ThemeModeController>(() => ThemeModeController());
  getIt.registerSingleton<ThemeCubit>(ThemeCubit());
  getIt.registerSingleton<ThemeModeCubit>(ThemeModeCubit());

  // Register Dio client
  getIt.registerSingleton<Dio>(_createDioClient());

  // Register secure storage
  getIt.registerSingleton<SecureStorage>(SecureStorage());

  // Register user service if needed
  getIt.registerSingleton<UserService>(UserService(getIt<Dio>()));

  // Register services
  getIt.registerSingleton<AuthService>(AuthService(getIt<Dio>()));

  // Register repositories
  getIt.registerSingleton<AuthRepository>(
    AuthRepository(
        getIt<AuthService>(), getIt<UserService>(), getIt<SecureStorage>()),
  );
  getIt.registerSingleton<TransactionRepository>(
      TransactionRepositoryImpl(getIt<Dio>()));

  getIt.registerSingleton<PaymentService>(PaymentService(getIt<Dio>()));
  getIt.registerSingleton<BeneficiaryService>(BeneficiaryService(getIt<Dio>()));
  getIt.registerSingleton<LimitService>(LimitService(getIt<Dio>()));
  getIt.registerSingleton<NeftService>(NeftService(getIt<Dio>()));
  getIt.registerSingleton<RtgsService>(RtgsService(getIt<Dio>()));
  getIt.registerSingleton<ImpsService>(ImpsService(getIt<Dio>()));
  getIt.registerSingleton<BranchService>(BranchService(getIt<Dio>()));
  getIt.registerSingleton<ChequeService>(ChequeService(getIt<Dio>()));
  getIt.registerSingleton<YojnaService>(YojnaService(getIt<Dio>()));
  getIt.registerLazySingleton<ChangePasswordService>(
    () => ChangePasswordService(getIt<Dio>()),
  );

  // Add auth interceptor after repository is available
  getIt<Dio>().interceptors.add(
        AuthInterceptor(getIt<AuthRepository>(), getIt<Dio>()),
      );

  // Register controllers/cubits
  getIt.registerFactory<AuthCubit>(() => AuthCubit(
      getIt<AuthRepository>(), getIt<UserService>(), getIt<SecureStorage>()));
}

Dio _createDioClient() {
  final dio = Dio(
    BaseOptions(
      baseUrl:
        'http://lb-test-mobile-banking-app-192209417.ap-south-1.elb.amazonaws.com', //test
      //'http://lb-kccb-mobile-banking-app-848675342.ap-south-1.elb.amazonaws.com',  //prod
      //'https://kccbmbnk.net', //prod small
      connectTimeout: const Duration(seconds: 60),
      receiveTimeout: const Duration(seconds: 60),
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'X-Login-Type': 'MB',
      },
    ),
  );

  // Add logging interceptor for development
  dio.interceptors.add(LogInterceptor(
    request: true,
    requestHeader: true,
    requestBody: true,
    responseHeader: true,
    responseBody: true,
    error: true,
  ));

  return dio;
}
