import 'package:dio/dio.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:get_it/get_it.dart';
import '../core/config/environment.dart';
import '../data/datasources/remote/api_client.dart';
import '../data/datasources/remote/auth_remote_datasource.dart';
import '../data/datasources/remote/dashboard_remote_datasource.dart';
import '../data/datasources/remote/equipment_remote_datasource.dart';
import '../data/datasources/remote/company_remote_datasource.dart';
import '../data/datasources/remote/user_remote_datasource.dart';
import '../data/datasources/remote/license_remote_datasource.dart';
import '../data/datasources/remote/warehouse_remote_datasource.dart';
import '../services/auth_service.dart';
import '../services/dashboard_service.dart';
import '../services/equipment_service.dart';
import '../services/company_service.dart';
import '../services/user_service.dart';
import '../services/license_service.dart';
import '../services/warehouse_service.dart';

/// GetIt 인스턴스
final getIt = GetIt.instance;

/// 의존성 주입 설정
Future<void> setupDependencies() async {
  // 환경 초기화
  await Environment.initialize();
  
  // 외부 라이브러리
  getIt.registerLazySingleton(() => Dio());
  getIt.registerLazySingleton(() => const FlutterSecureStorage());
  
  // API 클라이언트
  getIt.registerLazySingleton(() => ApiClient());
  
  // 데이터소스
  getIt.registerLazySingleton<AuthRemoteDataSource>(
    () => AuthRemoteDataSourceImpl(getIt()),
  );
  getIt.registerLazySingleton<DashboardRemoteDataSource>(
    () => DashboardRemoteDataSourceImpl(getIt()),
  );
  getIt.registerLazySingleton<EquipmentRemoteDataSource>(
    () => EquipmentRemoteDataSourceImpl(),
  );
  getIt.registerLazySingleton<CompanyRemoteDataSource>(
    () => CompanyRemoteDataSourceImpl(getIt()),
  );
  getIt.registerLazySingleton<UserRemoteDataSource>(
    () => UserRemoteDataSource(),
  );
  getIt.registerLazySingleton<LicenseRemoteDataSource>(
    () => LicenseRemoteDataSourceImpl(apiClient: getIt()),
  );
  getIt.registerLazySingleton<WarehouseRemoteDataSource>(
    () => WarehouseRemoteDataSourceImpl(apiClient: getIt()),
  );
  
  // 서비스
  getIt.registerLazySingleton<AuthService>(
    () => AuthServiceImpl(getIt(), getIt()),
  );
  getIt.registerLazySingleton<DashboardService>(
    () => DashboardServiceImpl(getIt()),
  );
  getIt.registerLazySingleton<EquipmentService>(
    () => EquipmentService(),
  );
  getIt.registerLazySingleton<CompanyService>(
    () => CompanyService(getIt()),
  );
  getIt.registerLazySingleton<UserService>(
    () => UserService(),
  );
  getIt.registerLazySingleton<LicenseService>(
    () => LicenseService(getIt()),
  );
  getIt.registerLazySingleton<WarehouseService>(
    () => WarehouseService(),
  );
  
  // 리포지토리
  // TODO: Repositories will be registered here
  
  // 유스케이스
  // TODO: Use cases will be registered here
  
  // 컨트롤러/프로바이더
  // TODO: Controllers will be registered here
}

/// 의존성 리셋 (테스트용)
Future<void> resetDependencies() async {
  await getIt.reset();
}

/// 특정 타입의 의존성 가져오기
T inject<T extends Object>() => getIt.get<T>();

/// 특정 타입의 의존성이 등록되어 있는지 확인
bool isRegistered<T extends Object>() => getIt.isRegistered<T>();

/// 의존성 등록 헬퍼 함수들
extension GetItHelpers on GetIt {
  /// 싱글톤 등록 헬퍼
  void registerSingletonIfNotRegistered<T extends Object>(
    T Function() factory,
  ) {
    if (!isRegistered<T>()) {
      registerLazySingleton<T>(factory);
    }
  }
  
  /// 팩토리 등록 헬퍼
  void registerFactoryIfNotRegistered<T extends Object>(
    T Function() factory,
  ) {
    if (!isRegistered<T>()) {
      registerFactory<T>(factory);
    }
  }
}