import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';

// Domain entities
import { User } from './domain/entities/user.entity';
import { RefreshToken } from './domain/entities/refresh-token.entity';
import { SmsVerification } from './domain/entities/sms-verification.entity';
import { SmsLog } from './domain/entities/sms-log.entity';

// Domain repository interfaces
import { USER_REPOSITORY } from './domain/repositories/user.repository.interface';
import { REFRESH_TOKEN_REPOSITORY } from './domain/repositories/refresh-token.repository.interface';
import { SMS_VERIFICATION_REPOSITORY } from './domain/repositories/sms-verification.repository.interface';
import { SMS_LOG_REPOSITORY } from './domain/repositories/sms-log.repository.interface';

// Infrastructure implementations
import { UserRepository } from './infrastructure/persistence/user.repository';
import { RefreshTokenRepository } from './infrastructure/persistence/refresh-token.repository';
import { SmsVerificationRepository } from './infrastructure/persistence/sms-verification.repository';
import { SmsLogRepository } from './infrastructure/persistence/sms-log.repository';
import { JwtStrategy } from './infrastructure/strategies/jwt.strategy';
import { TokenBlacklistService } from './infrastructure/redis/token-blacklist.service';
import { SmsCodeService } from './infrastructure/redis/sms-code.service';

// SMS Provider
import { SMS_PROVIDER } from './infrastructure/sms/sms-provider.interface';
import { ConsoleSmsProvider } from './infrastructure/sms/console-sms.provider';
import { AliyunSmsProvider } from './infrastructure/sms/aliyun-sms.provider';

// Application services
import { AuthService } from './application/services/auth.service';
import { TokenService } from './application/services/token.service';
import { SmsService } from './application/services/sms.service';
import { EventPublisherService } from './application/services/event-publisher.service';

// Interface controllers
import { AuthController } from './interface/http/controllers/auth.controller';
import { AdminSmsController } from './interface/http/controllers/admin-sms.controller';

@Module({
  imports: [
    TypeOrmModule.forFeature([User, RefreshToken, SmsVerification, SmsLog]),
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.register({
      secret: process.env.JWT_ACCESS_SECRET || 'dev-access-secret',
      signOptions: { expiresIn: process.env.JWT_ACCESS_EXPIRY || '15m' },
    }),
  ],
  controllers: [AuthController, AdminSmsController],
  providers: [
    // Infrastructure -> Domain port binding
    { provide: USER_REPOSITORY, useClass: UserRepository },
    { provide: REFRESH_TOKEN_REPOSITORY, useClass: RefreshTokenRepository },
    { provide: SMS_VERIFICATION_REPOSITORY, useClass: SmsVerificationRepository },
    { provide: SMS_LOG_REPOSITORY, useClass: SmsLogRepository },

    // SMS Provider: toggle by SMS_ENABLED env var
    {
      provide: SMS_PROVIDER,
      useClass:
        process.env.SMS_ENABLED === 'true'
          ? AliyunSmsProvider
          : ConsoleSmsProvider,
    },

    // Infrastructure
    JwtStrategy,
    TokenBlacklistService,
    SmsCodeService,

    // Application services
    AuthService,
    TokenService,
    SmsService,
    EventPublisherService,
  ],
  exports: [AuthService, TokenService, SmsService],
})
export class AuthModule {}
