import { Provider } from '@nestjs/common';

// Import core interfaces
import type { MediaResolverPort } from '@core/ports/media/MediaResolverPort';
import { DriverExtendedProfileProvider } from '@core/racing/application/ports/DriverExtendedProfileProvider';
import { DriverRepository } from '@core/racing/domain/repositories/DriverRepository';
import { LiveryRepository } from '@core/racing/domain/repositories/LiveryRepository';
import { RaceRegistrationRepository } from '@core/racing/domain/repositories/RaceRegistrationRepository';
import type { ResultRepository } from '@core/racing/domain/repositories/ResultRepository';
import type { StandingRepository } from '@core/racing/domain/repositories/StandingRepository';
import type { TeamMembershipRepository } from '@core/racing/domain/repositories/TeamMembershipRepository';
import type { TeamRepository } from '@core/racing/domain/repositories/TeamRepository';
import type { Logger } from '@core/shared/domain/Logger';
import type { SocialGraphRepository } from '@core/social/domain/repositories/SocialGraphRepository';

// Import use cases
import { CompleteDriverOnboardingUseCase } from '@core/racing/application/use-cases/CompleteDriverOnboardingUseCase';
import { GetDriverLiveriesUseCase } from '@core/racing/application/use-cases/GetDriverLiveriesUseCase';
import { GetDriversLeaderboardUseCase } from '@core/racing/application/use-cases/GetDriversLeaderboardUseCase';
import { GetDriverUseCase } from '@core/racing/application/use-cases/GetDriverUseCase';
import { GetProfileOverviewUseCase } from '@core/racing/application/use-cases/GetProfileOverviewUseCase';
import { GetTotalDriversUseCase } from '@core/racing/application/use-cases/GetTotalDriversUseCase';
import { IsDriverRegisteredForRaceUseCase } from '@core/racing/application/use-cases/IsDriverRegisteredForRaceUseCase';
import { UpdateDriverProfileUseCase } from '@core/racing/application/use-cases/UpdateDriverProfileUseCase';

// Import concrete implementations
import { ConsoleLogger } from '@adapters/logging/ConsoleLogger';
import { InMemoryImageServiceAdapter } from '@adapters/media/ports/InMemoryImageServiceAdapter';
import { InMemoryNotificationPreferenceRepository } from '@adapters/notifications/persistence/inmemory/InMemoryNotificationPreferenceRepository';
import { InMemoryLiveryRepository } from '@adapters/racing/persistence/inmemory/InMemoryLiveryRepository';
import { InMemoryDriverExtendedProfileProvider } from '@adapters/racing/ports/InMemoryDriverExtendedProfileProvider';
import { InMemoryDriverRatingProvider } from '@adapters/racing/ports/InMemoryDriverRatingProvider';
// Import new use cases
// Import new repositories
// Import MediaResolverAdapter
import { MediaResolverAdapter } from '@adapters/media/MediaResolverAdapter';
// Import repository tokens
import { DriverStatsRepository } from '@core/racing/domain/repositories/DriverStatsRepository';
// Import use case interfaces
import { DriverStatsUseCase } from '@core/racing/application/use-cases/DriverStatsUseCase';
import { RankingUseCase } from '@core/racing/application/use-cases/RankingUseCase';

// Import presenters
import { CompleteOnboardingPresenter } from './presenters/CompleteOnboardingPresenter';
import { DriverPresenter } from './presenters/DriverPresenter';
import { DriverProfilePresenter } from './presenters/DriverProfilePresenter';
import { DriverRegistrationStatusPresenter } from './presenters/DriverRegistrationStatusPresenter';
import { DriversLeaderboardPresenter } from './presenters/DriversLeaderboardPresenter';
import { DriverStatsPresenter } from './presenters/DriverStatsPresenter';
import { GetDriverLiveriesPresenter } from './presenters/GetDriverLiveriesPresenter';

import {
    COMPLETE_DRIVER_ONBOARDING_USE_CASE_TOKEN,
    DRIVER_EXTENDED_PROFILE_PROVIDER_TOKEN,
    DRIVER_RATING_PROVIDER_TOKEN,
    DRIVER_REPOSITORY_TOKEN,
    DRIVER_STATS_REPOSITORY_TOKEN,
    DRIVER_STATS_SERVICE_TOKEN,
    GET_DRIVER_LIVERIES_USE_CASE_TOKEN,
    GET_DRIVER_USE_CASE_TOKEN,
    GET_DRIVERS_LEADERBOARD_USE_CASE_TOKEN,
    GET_PROFILE_OVERVIEW_USE_CASE_TOKEN,
    GET_TOTAL_DRIVERS_USE_CASE_TOKEN,
    IMAGE_SERVICE_PORT_TOKEN,
    IS_DRIVER_REGISTERED_FOR_RACE_USE_CASE_TOKEN,
    LIVERY_REPOSITORY_TOKEN,
    LOGGER_TOKEN,
    MEDIA_RESOLVER_TOKEN,
    NOTIFICATION_PREFERENCE_REPOSITORY_TOKEN,
    RACE_REGISTRATION_REPOSITORY_TOKEN,
    RANKING_SERVICE_TOKEN,
    SOCIAL_GRAPH_REPOSITORY_TOKEN,
    TEAM_MEMBERSHIP_REPOSITORY_TOKEN,
    TEAM_REPOSITORY_TOKEN,
    UPDATE_DRIVER_PROFILE_USE_CASE_TOKEN,
} from './DriverTokens';

export * from './DriverTokens';

// Import logging infrastructure
import { InitializationLogger } from '../../shared/logging/InitializationLogger';
import { createLoggedProviders } from '../../shared/logging/LoggedProvider';

// Initialize logger
const initLogger = InitializationLogger.getInstance();

export const DriverProviders: Provider[] = createLoggedProviders([
   
  // Presenters
  {
    provide: DriversLeaderboardPresenter,
    useFactory: (mediaResolver: MediaResolverPort) => {
      const presenter = new DriversLeaderboardPresenter();
      presenter.setMediaResolver(mediaResolver);
      return presenter;
    },
    inject: [MEDIA_RESOLVER_TOKEN],
  },
  DriverStatsPresenter,
  CompleteOnboardingPresenter,
  DriverRegistrationStatusPresenter,
  {
    provide: DriverPresenter,
    useFactory: (driverStatsRepository: DriverStatsRepository, mediaResolver: MediaResolverPort) => {
      const presenter = new DriverPresenter(driverStatsRepository, mediaResolver);
      return presenter;
    },
    inject: [DRIVER_STATS_REPOSITORY_TOKEN, MEDIA_RESOLVER_TOKEN],
  },
  {
    provide: DriverProfilePresenter,
    useFactory: (mediaResolver: MediaResolverPort) => {
      const presenter = new DriverProfilePresenter(mediaResolver);
      return presenter;
    },
    inject: [MEDIA_RESOLVER_TOKEN],
  },
  GetDriverLiveriesPresenter,

  // Logger
  {
    provide: LOGGER_TOKEN,
    useClass: ConsoleLogger,
  },

  // Media Resolver (real adapter, path-only)
  {
    provide: MEDIA_RESOLVER_TOKEN,
    useFactory: () => new MediaResolverAdapter({}),
  },

  // Repositories (racing + social repos are provided by imported persistence modules)
  {
    provide: RANKING_SERVICE_TOKEN,
    useFactory: (
      standingRepo: StandingRepository,
      driverRepo: DriverRepository,
      driverStatsRepo: DriverStatsRepository,
      logger: Logger
    ) => new RankingUseCase(standingRepo, driverRepo, driverStatsRepo, logger),
    inject: ['IStandingRepository', DRIVER_REPOSITORY_TOKEN, DRIVER_STATS_REPOSITORY_TOKEN, LOGGER_TOKEN],
  },
  {
    provide: DRIVER_STATS_SERVICE_TOKEN,
    useFactory: (
      resultRepo: ResultRepository,
      standingRepo: StandingRepository,
      driverStatsRepo: DriverStatsRepository,
      logger: Logger
    ) => new DriverStatsUseCase(resultRepo, standingRepo, driverStatsRepo, logger),
    inject: ['IResultRepository', 'IStandingRepository', DRIVER_STATS_REPOSITORY_TOKEN, LOGGER_TOKEN],
  },
  {
    provide: DRIVER_RATING_PROVIDER_TOKEN,
    useFactory: (logger: Logger) => new InMemoryDriverRatingProvider(logger),
    inject: [LOGGER_TOKEN],
  },
  {
    provide: DRIVER_EXTENDED_PROFILE_PROVIDER_TOKEN,
    useFactory: (logger: Logger) => new InMemoryDriverExtendedProfileProvider(logger),
    inject: [LOGGER_TOKEN],
  },
  {
    provide: IMAGE_SERVICE_PORT_TOKEN,
    useFactory: (logger: Logger) => new InMemoryImageServiceAdapter(logger),
    inject: [LOGGER_TOKEN],
  },
  {
    provide: NOTIFICATION_PREFERENCE_REPOSITORY_TOKEN,
    useFactory: (logger: Logger) => new InMemoryNotificationPreferenceRepository(logger),
    inject: [LOGGER_TOKEN],
  },
  {
    provide: LIVERY_REPOSITORY_TOKEN,
    useFactory: (logger: Logger) => new InMemoryLiveryRepository(logger),
    inject: [LOGGER_TOKEN],
  },

  // Use cases
  {
    provide: GET_DRIVERS_LEADERBOARD_USE_CASE_TOKEN,
    useFactory: (
      driverRepo: DriverRepository,
      rankingUseCase: RankingUseCase,
      driverStatsUseCase: DriverStatsUseCase,
      logger: Logger,
    ) => new GetDriversLeaderboardUseCase(
      driverRepo,
      rankingUseCase,
      driverStatsUseCase,
      logger
    ),
    inject: [DRIVER_REPOSITORY_TOKEN, RANKING_SERVICE_TOKEN, DRIVER_STATS_SERVICE_TOKEN, LOGGER_TOKEN],
  },
  {
    provide: GET_TOTAL_DRIVERS_USE_CASE_TOKEN,
    useFactory: (driverRepo: DriverRepository) => new GetTotalDriversUseCase(driverRepo),
    inject: [DRIVER_REPOSITORY_TOKEN],
  },
  {
    provide: COMPLETE_DRIVER_ONBOARDING_USE_CASE_TOKEN,
    useFactory: (driverRepo: DriverRepository, logger: Logger) => new CompleteDriverOnboardingUseCase(driverRepo, logger),
    inject: [DRIVER_REPOSITORY_TOKEN, LOGGER_TOKEN],
  },
  {
    provide: IS_DRIVER_REGISTERED_FOR_RACE_USE_CASE_TOKEN,
    useFactory: (registrationRepo: RaceRegistrationRepository, logger: Logger) =>
      new IsDriverRegisteredForRaceUseCase(registrationRepo, logger),
    inject: [RACE_REGISTRATION_REPOSITORY_TOKEN, LOGGER_TOKEN],
  },
  {
    provide: UPDATE_DRIVER_PROFILE_USE_CASE_TOKEN,
    useFactory: (driverRepo: DriverRepository, logger: Logger) =>
      new UpdateDriverProfileUseCase(driverRepo, logger),
    inject: [DRIVER_REPOSITORY_TOKEN, LOGGER_TOKEN],
  },
  {
    provide: GET_PROFILE_OVERVIEW_USE_CASE_TOKEN,
    useFactory: (
      driverRepo: DriverRepository,
      teamRepository: TeamRepository,
      teamMembershipRepository: TeamMembershipRepository,
      socialRepository: SocialGraphRepository,
      driverExtendedProfileProvider: DriverExtendedProfileProvider,
      driverStatsUseCase: DriverStatsUseCase,
      rankingUseCase: RankingUseCase,
    ) =>
      new GetProfileOverviewUseCase(
        driverRepo,
        teamRepository,
        teamMembershipRepository,
        socialRepository,
        driverExtendedProfileProvider,
        driverStatsUseCase,
        rankingUseCase,
      ),
    inject: [
      DRIVER_REPOSITORY_TOKEN,
      TEAM_REPOSITORY_TOKEN,
      TEAM_MEMBERSHIP_REPOSITORY_TOKEN,
      SOCIAL_GRAPH_REPOSITORY_TOKEN,
      DRIVER_EXTENDED_PROFILE_PROVIDER_TOKEN,
      DRIVER_STATS_SERVICE_TOKEN,
      RANKING_SERVICE_TOKEN,
    ],
  },
  {
    provide: GET_DRIVER_LIVERIES_USE_CASE_TOKEN,
    useFactory: (liveryRepository: LiveryRepository, logger: Logger) =>
      new GetDriverLiveriesUseCase(liveryRepository, logger),
    inject: [LIVERY_REPOSITORY_TOKEN, LOGGER_TOKEN],
  },
  {
    provide: GET_DRIVER_USE_CASE_TOKEN,
    useFactory: (driverRepo: DriverRepository) => new GetDriverUseCase(driverRepo),
    inject: [DRIVER_REPOSITORY_TOKEN],
  },
], initLogger);
