import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:get_it/get_it.dart';
import 'package:hmg_patient_app_new/core/api/api_client.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/location_util.dart';
import 'package:hmg_patient_app_new/features/authentication/authentication_repo.dart';
import 'package:hmg_patient_app_new/features/authentication/authentication_view_model.dart';
import 'package:hmg_patient_app_new/features/book_appointments/book_appointments_repo.dart';
import 'package:hmg_patient_app_new/features/common/common_repo.dart';
import 'package:hmg_patient_app_new/features/insurance/insurance_repo.dart';
import 'package:hmg_patient_app_new/features/insurance/insurance_view_model.dart';
import 'package:hmg_patient_app_new/features/lab/lab_repo.dart';
import 'package:hmg_patient_app_new/features/lab/lab_view_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_repo.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_view_model.dart';
import 'package:hmg_patient_app_new/features/payfort/payfort_repo.dart';
import 'package:hmg_patient_app_new/features/payfort/payfort_view_model.dart';
import 'package:hmg_patient_app_new/features/prescriptions/prescriptions_repo.dart';
import 'package:hmg_patient_app_new/features/prescriptions/prescriptions_view_model.dart';
import 'package:hmg_patient_app_new/features/radiology/radiology_repo.dart';
import 'package:hmg_patient_app_new/features/radiology/radiology_view_model.dart';
import 'package:hmg_patient_app_new/services/analytics/analytics_service.dart';
import 'package:hmg_patient_app_new/services/cache_service.dart';
import 'package:hmg_patient_app_new/services/dialog_service.dart';
import 'package:hmg_patient_app_new/services/error_handler_service.dart';
import 'package:hmg_patient_app_new/services/firebase_service.dart';
import 'package:hmg_patient_app_new/services/logger_service.dart';
import 'package:hmg_patient_app_new/services/navigation_service.dart';
import 'package:logger/web.dart';
import 'package:shared_preferences/shared_preferences.dart';

GetIt getIt = GetIt.instance;

class AppDependencies {
  static Future<void> addDependencies() async {
    Logger logger = Logger(
      printer: PrettyPrinter(
        methodCount: 2,
        errorMethodCount: 5,
        lineLength: 1000,
        colors: true,
        printEmojis: true,
      ),
    );

    // Core Services
    getIt.registerLazySingleton<LoggerService>(() => LoggerServiceImp(logger: logger));
    getIt.registerLazySingleton<FirebaseService>(() => FirebaseServiceImpl(
          loggerService: getIt(),
          appState: getIt(),
          firebaseMessaging: FirebaseMessaging.instance,
        ));

    getIt.registerLazySingleton<NavigationService>(() => NavigationService());
    getIt.registerLazySingleton<GAnalytics>(() => GAnalytics());
    getIt.registerLazySingleton<AppState>(() => AppState(navigationService: getIt()));
    getIt.registerLazySingleton<LocationUtils>(() => LocationUtils(
          isShowConfirmDialog: false,
          navigationService: getIt(),
          appState: getIt(),
        ));
    getIt.registerLazySingleton<DialogService>(() => DialogServiceImp(navigationService: getIt<NavigationService>()));
    getIt.registerLazySingleton<ErrorHandlerService>(() => ErrorHandlerServiceImp(
          dialogService: getIt(),
          loggerService: getIt(),
          navigationService: getIt(),
        ));

    final sharedPreferences = await SharedPreferences.getInstance();
    getIt.registerLazySingleton<CacheService>(() => CacheServiceImp(sharedPreferences: sharedPreferences, loggerService: getIt()));
    getIt.registerLazySingleton<ApiClient>(() => ApiClientImp(loggerService: getIt(), appState: getIt()));

    // Repositories
    getIt.registerLazySingleton<CommonRepo>(() => CommonRepoImp(loggerService: getIt()));
    getIt.registerLazySingleton<AuthenticationRepo>(() => AuthenticationRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt()));
    getIt.registerLazySingleton<BookAppointmentsRepo>(() => BookAppointmentsRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt()));
    getIt.registerLazySingleton<MyAppointmentsRepo>(() => MyAppointmentsRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt()));
    getIt.registerLazySingleton<LabRepo>(() => LabRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt()));
    getIt.registerLazySingleton<RadiologyRepo>(() => RadiologyRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt()));
    getIt.registerLazySingleton<PrescriptionsRepo>(() => PrescriptionsRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt()));
    getIt.registerLazySingleton<InsuranceRepo>(() => InsuranceRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt()));
    getIt.registerLazySingleton<PayfortRepo>(() => PayfortRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt()));

    // ViewModels
    // Global/shared VMs → LazySingleton

    getIt.registerLazySingleton<LabViewModel>(
      () => LabViewModel(
        labRepo: getIt(),
        errorHandlerService: getIt(),
      ),
    );

    getIt.registerLazySingleton<RadiologyViewModel>(
      () => RadiologyViewModel(
        radiologyRepo: getIt(),
        errorHandlerService: getIt(),
      ),
    );

    getIt.registerLazySingleton<PrescriptionsViewModel>(
      () => PrescriptionsViewModel(
        prescriptionsRepo: getIt(),
        errorHandlerService: getIt(),
      ),
    );

    getIt.registerLazySingleton<InsuranceViewModel>(
      () => InsuranceViewModel(
        insuranceRepo: getIt(),
        errorHandlerService: getIt(),
      ),
    );

    getIt.registerLazySingleton<MyAppointmentsViewModel>(
      () => MyAppointmentsViewModel(
        myAppointmentsRepo: getIt(),
        errorHandlerService: getIt(),
      ),
    );

    getIt.registerLazySingleton<PayfortViewModel>(
          () => PayfortViewModel(
        payfortRepo: getIt(),
        errorHandlerService: getIt(),
      ),
    );

    getIt.registerLazySingleton<AuthenticationViewModel>(
      () => AuthenticationViewModel(
        authenticationRepo: getIt(),
        cacheService: getIt(),
        navigationService: getIt(),
        dialogService: getIt(),
        appState: getIt(),
        errorHandlerService: getIt(),
      ),
    );

    // Screen-specific VMs → Factory
    // getIt.registerFactory<BookAppointmentsViewModel>(
    //       () => BookAppointmentsViewModel(
    //     bookAppointmentsRepo: getIt(),
    //     dialogService: getIt(),
    //     errorHandlerService: getIt(),
    //   ),
    // );
  }
}
