import { Module, forwardRef } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProgramSession, SessionCommunicationStatus, BackgroundJob } from 'src/common/entities';
import { AuthModule } from 'src/auth/auth.module';
import { UserModule } from 'src/user/user.module';
import { OnlineSessionController } from './controllers/online-session.controller';
import { OnlineSessionService } from './services/online-session.service';
import { OnlineSessionProviderRegistry } from './services/online-session-provider.registry';
import { OnlineSessionRepository } from './repositories/online-session.repository';
import { JoinLinkSchedulerModule } from 'src/join-link-scheduler/join-link-scheduler.module';
import { ProgramModule } from 'src/program/program.module';
import { FeatureFlagModule } from 'src/feature-flag/feature-flag.module';

/**
 * Provider-agnostic orchestrator. Owns the neutral API, the facade, and the
 * provider registry. Provider modules (zoom, ...) import this module and
 * self-register their implementation — this module never imports them.
 */
@Module({
  imports: [
    // SessionCommunicationStatus is registered here for a read-only projection on the
    // GET responses (Invite/Absent bulk-comm status). Read directly rather than via
    // SessionCommunicationModule to avoid the module cycle
    // OnlineSession → SessionCommunication → Queue → JoinLinkGeneration → OnlineSession.
    TypeOrmModule.forFeature([ProgramSession, SessionCommunicationStatus, BackgroundJob]),
    forwardRef(() => AuthModule),
    forwardRef(() => UserModule),
    JoinLinkSchedulerModule, // schedules bulk link generation on session create/update/delete
    // forwardRef defensively, matching this file's existing Auth/User pattern — ProgramModule's
    // own import list is broad enough that a cycle back here isn't ruled out.
    forwardRef(() => ProgramModule),
    FeatureFlagModule,
  ],
  controllers: [OnlineSessionController],
  providers: [OnlineSessionService, OnlineSessionProviderRegistry, OnlineSessionRepository],
  exports: [OnlineSessionService, OnlineSessionProviderRegistry, OnlineSessionRepository],
})
export class OnlineSessionModule {}
