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

// Import core interfaces
import { ITeamRepository } from '@core/racing/domain/repositories/ITeamRepository';
import { ITeamMembershipRepository } from '@core/racing/domain/repositories/ITeamMembershipRepository';
import { IDriverRepository } from '@core/racing/domain/repositories/IDriverRepository';
import { IImageServicePort } from '@core/racing/application/ports/IImageServicePort';
import type { Logger } from '@core/shared/application/Logger';

// Import concrete in-memory implementations
import { InMemoryTeamRepository } from '@adapters/racing/persistence/inmemory/InMemoryTeamRepository';
import { InMemoryTeamMembershipRepository } from '@adapters/racing/persistence/inmemory/InMemoryTeamMembershipRepository';
import { InMemoryDriverRepository } from '@adapters/racing/persistence/inmemory/InMemoryDriverRepository';
import { ConsoleLogger } from '@adapters/logging/ConsoleLogger';

// Import use cases
import { GetAllTeamsUseCase } from '@core/racing/application/use-cases/GetAllTeamsUseCase';
import { GetDriverTeamUseCase } from '@core/racing/application/use-cases/GetDriverTeamUseCase';
import { GetTeamDetailsUseCase } from '@core/racing/application/use-cases/GetTeamDetailsUseCase';
import { GetTeamMembersUseCase } from '@core/racing/application/use-cases/GetTeamMembersUseCase';
import { GetTeamJoinRequestsUseCase } from '@core/racing/application/use-cases/GetTeamJoinRequestsUseCase';
import { CreateTeamUseCase } from '@core/racing/application/use-cases/CreateTeamUseCase';
import { UpdateTeamUseCase } from '@core/racing/application/use-cases/UpdateTeamUseCase';
import { ApproveTeamJoinRequestUseCase } from '@core/racing/application/use-cases/ApproveTeamJoinRequestUseCase';
import { RejectTeamJoinRequestUseCase } from '@core/racing/application/use-cases/RejectTeamJoinRequestUseCase';
import { GetTeamsLeaderboardUseCase } from '@core/racing/application/use-cases/GetTeamsLeaderboardUseCase';

// Import presenters for use case initialization
import { DriverTeamPresenter } from './presenters/DriverTeamPresenter';
import { TeamMembersPresenter } from './presenters/TeamMembersPresenter';
import { TeamJoinRequestsPresenter } from './presenters/TeamJoinRequestsPresenter';
import { TeamsLeaderboardPresenter } from './presenters/TeamsLeaderboardPresenter';

// Tokens
export const TEAM_REPOSITORY_TOKEN = 'ITeamRepository';
export const TEAM_MEMBERSHIP_REPOSITORY_TOKEN = 'ITeamMembershipRepository';
export const DRIVER_REPOSITORY_TOKEN = 'IDriverRepository';
export const IMAGE_SERVICE_TOKEN = 'IImageServicePort';
export const TEAM_GET_ALL_USE_CASE_TOKEN = 'GetAllTeamsUseCase';
export const TEAM_GET_DRIVER_TEAM_USE_CASE_TOKEN = 'GetDriverTeamUseCase';
export const TEAM_GET_DETAILS_USE_CASE_TOKEN = 'GetTeamDetailsUseCase';
export const TEAM_GET_MEMBERS_USE_CASE_TOKEN = 'GetTeamMembersUseCase';
export const TEAM_GET_JOIN_REQUESTS_USE_CASE_TOKEN = 'GetTeamJoinRequestsUseCase';
export const TEAM_CREATE_USE_CASE_TOKEN = 'CreateTeamUseCase';
export const TEAM_UPDATE_USE_CASE_TOKEN = 'UpdateTeamUseCase';
export const TEAM_APPROVE_JOIN_REQUEST_USE_CASE_TOKEN = 'ApproveTeamJoinRequestUseCase';
export const TEAM_REJECT_JOIN_REQUEST_USE_CASE_TOKEN = 'RejectTeamJoinRequestUseCase';
export const TEAM_GET_LEADERBOARD_USE_CASE_TOKEN = 'GetTeamsLeaderboardUseCase';
export const TEAM_LOGGER_TOKEN = 'Logger';

// Simple image service implementation for team module
class SimpleImageService implements IImageServicePort {
  getDriverAvatar(driverId: string): string {
    return `/api/media/avatars/${driverId}`;
  }
  getTeamLogo(teamId: string): string {
    return `/api/media/teams/${teamId}/logo`;
  }
  getLeagueCover(leagueId: string): string {
    return `/api/media/leagues/${leagueId}/cover`;
  }
  getLeagueLogo(leagueId: string): string {
    return `/api/media/leagues/${leagueId}/logo`;
  }
}

export const TeamProviders: Provider[] = [
  TeamService, // Provide the service itself
  {
    provide: TEAM_REPOSITORY_TOKEN,
    useFactory: (logger: Logger) => new InMemoryTeamRepository(logger),
    inject: [TEAM_LOGGER_TOKEN],
  },
  {
    provide: TEAM_MEMBERSHIP_REPOSITORY_TOKEN,
    useFactory: (logger: Logger) => new InMemoryTeamMembershipRepository(logger),
    inject: [TEAM_LOGGER_TOKEN],
  },
  {
    provide: DRIVER_REPOSITORY_TOKEN,
    useFactory: (logger: Logger) => new InMemoryDriverRepository(logger),
    inject: [TEAM_LOGGER_TOKEN],
  },
  {
    provide: IMAGE_SERVICE_TOKEN,
    useClass: SimpleImageService,
  },
  {
    provide: TEAM_LOGGER_TOKEN,
    useClass: ConsoleLogger,
  },
  // Use cases
  {
    provide: TEAM_GET_ALL_USE_CASE_TOKEN,
    useFactory: (teamRepo: ITeamRepository, membershipRepo: ITeamMembershipRepository, logger: Logger) =>
      new GetAllTeamsUseCase(teamRepo, membershipRepo, logger),
    inject: [TEAM_REPOSITORY_TOKEN, TEAM_MEMBERSHIP_REPOSITORY_TOKEN, TEAM_LOGGER_TOKEN],
  },
  {
    provide: TEAM_GET_DRIVER_TEAM_USE_CASE_TOKEN,
    useFactory: (teamRepo: ITeamRepository, membershipRepo: ITeamMembershipRepository, logger: Logger) =>
      new GetDriverTeamUseCase(teamRepo, membershipRepo, logger, new DriverTeamPresenter()),
    inject: [TEAM_REPOSITORY_TOKEN, TEAM_MEMBERSHIP_REPOSITORY_TOKEN, TEAM_LOGGER_TOKEN],
  },
  {
    provide: TEAM_GET_DETAILS_USE_CASE_TOKEN,
    useFactory: (teamRepo: ITeamRepository, membershipRepo: ITeamMembershipRepository) =>
      new GetTeamDetailsUseCase(teamRepo, membershipRepo),
    inject: [TEAM_REPOSITORY_TOKEN, TEAM_MEMBERSHIP_REPOSITORY_TOKEN],
  },
  {
    provide: TEAM_GET_MEMBERS_USE_CASE_TOKEN,
    useFactory: (
      membershipRepo: ITeamMembershipRepository,
      driverRepo: IDriverRepository,
      imageService: IImageServicePort,
      logger: Logger,
    ) => new GetTeamMembersUseCase(membershipRepo, driverRepo, imageService, logger, new TeamMembersPresenter()),
    inject: [TEAM_MEMBERSHIP_REPOSITORY_TOKEN, DRIVER_REPOSITORY_TOKEN, IMAGE_SERVICE_TOKEN, TEAM_LOGGER_TOKEN],
  },
  {
    provide: TEAM_GET_JOIN_REQUESTS_USE_CASE_TOKEN,
    useFactory: (
      membershipRepo: ITeamMembershipRepository,
      driverRepo: IDriverRepository,
      imageService: IImageServicePort,
      logger: Logger,
    ) => new GetTeamJoinRequestsUseCase(membershipRepo, driverRepo, imageService, logger, new TeamJoinRequestsPresenter()),
    inject: [TEAM_MEMBERSHIP_REPOSITORY_TOKEN, DRIVER_REPOSITORY_TOKEN, IMAGE_SERVICE_TOKEN, TEAM_LOGGER_TOKEN],
  },
  {
    provide: TEAM_CREATE_USE_CASE_TOKEN,
    useFactory: (teamRepo: ITeamRepository, membershipRepo: ITeamMembershipRepository) =>
      new CreateTeamUseCase(teamRepo, membershipRepo),
    inject: [TEAM_REPOSITORY_TOKEN, TEAM_MEMBERSHIP_REPOSITORY_TOKEN],
  },
  {
    provide: TEAM_UPDATE_USE_CASE_TOKEN,
    useFactory: (teamRepo: ITeamRepository, membershipRepo: ITeamMembershipRepository) =>
      new UpdateTeamUseCase(teamRepo, membershipRepo),
    inject: [TEAM_REPOSITORY_TOKEN, TEAM_MEMBERSHIP_REPOSITORY_TOKEN],
  },
  {
    provide: TEAM_APPROVE_JOIN_REQUEST_USE_CASE_TOKEN,
    useFactory: (membershipRepo: ITeamMembershipRepository, logger: Logger) =>
      new ApproveTeamJoinRequestUseCase(membershipRepo, logger),
    inject: [TEAM_MEMBERSHIP_REPOSITORY_TOKEN, TEAM_LOGGER_TOKEN],
  },
  {
    provide: TEAM_REJECT_JOIN_REQUEST_USE_CASE_TOKEN,
    useFactory: (membershipRepo: ITeamMembershipRepository) =>
      new RejectTeamJoinRequestUseCase(membershipRepo),
    inject: [TEAM_MEMBERSHIP_REPOSITORY_TOKEN],
  },
  {
    provide: TEAM_GET_LEADERBOARD_USE_CASE_TOKEN,
    useFactory: (
      teamRepo: ITeamRepository,
      membershipRepo: ITeamMembershipRepository,
      driverRepo: IDriverRepository,
      logger: Logger,
    ) => new GetTeamsLeaderboardUseCase(teamRepo, membershipRepo, driverRepo, () => null, logger),
    inject: [TEAM_REPOSITORY_TOKEN, TEAM_MEMBERSHIP_REPOSITORY_TOKEN, DRIVER_REPOSITORY_TOKEN, TEAM_LOGGER_TOKEN],
  },
];
