import { Module } from "@nestjs/common";
import { JwtModule } from "@nestjs/jwt";
import { PassportModule } from "@nestjs/passport";
import { ConfigService } from "@nestjs/config";
import { APP_GUARD } from "@nestjs/core";
import { AuthFacade } from "./application/auth.facade";
import { AuthController } from "./presentation/http/auth.controller";
import { UsersModule } from "@bff/modules/users/users.module";
import { MappingsModule } from "@bff/modules/id-mappings/mappings.module";
import { IntegrationsModule } from "@bff/integrations/integrations.module";
import { JwtStrategy } from "./presentation/strategies/jwt.strategy";
import { LocalStrategy } from "./presentation/strategies/local.strategy";
import { GlobalAuthGuard } from "./presentation/http/guards/global-auth.guard";
import { TokenBlacklistService } from "./infra/token/token-blacklist.service";
import { EmailModule } from "@bff/infra/email/email.module";
import { CacheModule } from "@bff/infra/cache/cache.module";
import { AuthTokenService } from "./infra/token/token.service";
import { SignupWorkflowService } from "./infra/workflows/workflows/signup-workflow.service";
import { PasswordWorkflowService } from "./infra/workflows/workflows/password-workflow.service";
import { WhmcsLinkWorkflowService } from "./infra/workflows/workflows/whmcs-link-workflow.service";
import { FailedLoginThrottleGuard } from "./presentation/http/guards/failed-login-throttle.guard";
import { LoginResultInterceptor } from "./presentation/http/interceptors/login-result.interceptor";
import { AuthRateLimitService } from "./infra/rate-limiting/auth-rate-limit.service";

@Module({
  imports: [
    PassportModule,
    JwtModule.registerAsync({
      useFactory: (configService: ConfigService) => ({
        secret: configService.get("JWT_SECRET"),
        signOptions: { expiresIn: configService.get("JWT_EXPIRES_IN", "7d") },
      }),
      inject: [ConfigService],
    }),
    UsersModule,
    MappingsModule,
    IntegrationsModule,
    EmailModule,
    CacheModule,
  ],
  controllers: [AuthController],
  providers: [
    AuthFacade,
    JwtStrategy,
    LocalStrategy,
    TokenBlacklistService,
    AuthTokenService,
    SignupWorkflowService,
    PasswordWorkflowService,
    WhmcsLinkWorkflowService,
    FailedLoginThrottleGuard,
    AuthRateLimitService,
    LoginResultInterceptor,
    {
      provide: APP_GUARD,
      useClass: GlobalAuthGuard,
    },
  ],
  exports: [AuthFacade, TokenBlacklistService, AuthTokenService],
})
export class AuthModule {}
