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/book_appointments/book_appointments_view_model.dart';
import 'package:hmg_patient_app_new/features/common/common_repo.dart';
import 'package:hmg_patient_app_new/features/doctor_filter/doctor_filter_view_model.dart';
import 'package:hmg_patient_app_new/features/emergency_services/emergency_services_repo.dart';
import 'package:hmg_patient_app_new/features/emergency_services/emergency_services_view_model.dart';
import 'package:hmg_patient_app_new/features/habib_wallet/habib_wallet_repo.dart';
import 'package:hmg_patient_app_new/features/habib_wallet/habib_wallet_view_model.dart';
import 'package:hmg_patient_app_new/features/immediate_livecare/immediate_livecare_repo.dart';
import 'package:hmg_patient_app_new/features/immediate_livecare/immediate_livecare_view_model.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/medical_file/medical_file_repo.dart';
import 'package:hmg_patient_app_new/features/medical_file/medical_file_view_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/appointment_via_region_viewmodel.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/profile_settings/profile_settings_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/localauth_service.dart';
import 'package:hmg_patient_app_new/services/logger_service.dart';
import 'package:hmg_patient_app_new/services/navigation_service.dart';
import 'package:hmg_patient_app_new/widgets/date_range_selector/viewmodel/date_range_view_model.dart';
import 'package:http/http.dart';
import 'package:local_auth/local_auth.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<LocalAuthentication>(() => LocalAuthentication());

    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(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()));
    getIt.registerLazySingleton<LocalAuthService>(() => LocalAuthService(loggerService: getIt<LoggerService>(), localAuth: getIt<LocalAuthentication>()));
    getIt.registerLazySingleton<HabibWalletRepo>(() => HabibWalletRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt()));
    getIt.registerLazySingleton<MedicalFileRepo>(() => MedicalFileRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt()));
    getIt.registerLazySingleton<ImmediateLiveCareRepo>(() => ImmediateLiveCareRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt()));
    getIt.registerLazySingleton<EmergencyServicesRepo>(() =>
        EmergencyServicesRepoImpl(
            loggerService: getIt<LoggerService>(), apiClient: getIt()));

    // ViewModels
    // Global/shared VMs → LazySingleton

    getIt.registerLazySingleton<LabViewModel>(
      () => LabViewModel(labRepo: getIt(), errorHandlerService: getIt(), navigationService: 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(), appState: getIt()),
    );

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

    getIt.registerLazySingleton<HabibWalletViewModel>(
      () => HabibWalletViewModel(
        habibWalletRepo: getIt(),
        errorHandlerService: getIt(),
      ),
    );

    getIt.registerLazySingleton<MedicalFileViewModel>(
      () => MedicalFileViewModel(
        medicalFileRepo: getIt(),
        errorHandlerService: getIt(),
      ),
    );

    getIt.registerLazySingleton<BookAppointmentsViewModel>(
      () => BookAppointmentsViewModel(bookAppointmentsRepo: getIt(), errorHandlerService: getIt(), navigationService: getIt(), myAppointmentsViewModel: getIt(), locationUtils: getIt(), dialogService: getIt()),
    );

    getIt.registerLazySingleton<ImmediateLiveCareViewModel>(
      () => ImmediateLiveCareViewModel(
        immediateLiveCareRepo: getIt(),
        errorHandlerService: getIt(),
        navigationService: getIt(),
        myAppointmentsViewModel: getIt(),
      ),
    );
    getIt.registerLazySingleton<AppointmentViaRegionViewmodel>(
      () => AppointmentViaRegionViewmodel(
        navigationService: getIt(),
        appState: getIt(),
      ),
    );

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

    getIt.registerLazySingleton<DateRangeSelectorRangeViewModel>(
      () => DateRangeSelectorRangeViewModel(),
    );

    getIt.registerLazySingleton<DoctorFilterViewModel>(
      () => DoctorFilterViewModel(),
    );

    getIt.registerLazySingleton<EmergencyServicesViewModel>(
      () => EmergencyServicesViewModel(
        locationUtils: getIt(),
        navServices: getIt(),
        viewModel: getIt(),
        appState: getIt(),
      ),
    );
  }
}
