import 'package:flutter_modular/flutter_modular.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'data/datasources/locale_local_data_source.dart';
import 'data/repositories_impl/locale_repository_impl.dart';
import 'domain/repositories/locale_repository_interface.dart';
import 'domain/usecases/get_default_locale_use_case.dart';
import 'domain/usecases/get_locale_use_case.dart';
import 'domain/usecases/get_supported_locales_use_case.dart';
import 'domain/usecases/set_locale_use_case.dart';
import 'bloc/locale_bloc.dart';

/// A [ModularModule] that manages localization dependencies.
///
/// This module registers all necessary data sources, repositories, use cases,
/// and the BLoC required for application-wide localization management.
class LocalizationModule extends Module {
  @override
  void binds(Injector i) {
    // External Dependencies
    i.addInstance<SharedPreferencesAsync>(SharedPreferencesAsync());

    // Data Sources
    i.addLazySingleton<LocaleLocalDataSource>(
      () => LocaleLocalDataSourceImpl(i.get<SharedPreferencesAsync>()),
    );

    // Repositories
    i.addLazySingleton<LocaleRepositoryInterface>(
      () => LocaleRepositoryImpl(localDataSource: i.get<LocaleLocalDataSource>()),
    );

    // Use Cases
    i.addLazySingleton<GetLocaleUseCase>(
      () => GetLocaleUseCase(i.get<LocaleRepositoryInterface>()),
    );
    i.addLazySingleton<SetLocaleUseCase>(
      () => SetLocaleUseCase(i.get<LocaleRepositoryInterface>()),
    );
    i.addLazySingleton<GetSupportedLocalesUseCase>(
      () => GetSupportedLocalesUseCase(i.get<LocaleRepositoryInterface>()),
    );
    i.addLazySingleton<GetDefaultLocaleUseCase>(
      () => GetDefaultLocaleUseCase(i.get<LocaleRepositoryInterface>()),
    );

    // BLoCs
    i.add<LocaleBloc>(
      () => LocaleBloc(
        getLocaleUseCase: i.get<GetLocaleUseCase>(),
        setLocaleUseCase: i.get<SetLocaleUseCase>(),
        getSupportedLocalesUseCase: i.get<GetSupportedLocalesUseCase>(),
        getDefaultLocaleUseCase: i.get<GetDefaultLocaleUseCase>(),
      ),
    );
  }
}
