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';
import { EmailVerification } from './domain/entities/email-verification.entity';
import { EmailLog } from './domain/entities/email-log.entity';
import { SocialAccount } from './domain/entities/social-account.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';
import { EMAIL_VERIFICATION_REPOSITORY } from './domain/repositories/email-verification.repository.interface';
import { EMAIL_LOG_REPOSITORY } from './domain/repositories/email-log.repository.interface';
import { SOCIAL_ACCOUNT_REPOSITORY } from './domain/repositories/social-account.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 { EmailVerificationRepository } from './infrastructure/persistence/email-verification.repository';
import { EmailLogRepository } from './infrastructure/persistence/email-log.repository';
import { SocialAccountRepository } from './infrastructure/persistence/social-account.repository';
import { JwtStrategy } from './infrastructure/strategies/jwt.strategy';
import { TokenBlacklistService } from './infrastructure/redis/token-blacklist.service';
import { SmsCodeService } from './infrastructure/redis/sms-code.service';
import { EmailCodeService } from './infrastructure/redis/email-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';

// Email Provider
import { EMAIL_PROVIDER } from './infrastructure/email/email-provider.interface';
import { ConsoleEmailProvider } from './infrastructure/email/console-email.provider';
import { GmailProvider } from './infrastructure/email/gmail.provider';

// WeChat
import { WechatProvider } from './infrastructure/wechat/wechat.provider';

// Alipay
import { AlipayProvider } from './infrastructure/alipay/alipay.provider';

// Google
import { GoogleProvider } from './infrastructure/google/google.provider';

// Apple
import { AppleProvider } from './infrastructure/apple/apple.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 { EmailService } from './application/services/email.service';
import { WechatService } from './application/services/wechat.service';
import { AlipayService } from './application/services/alipay.service';
import { GoogleService } from './application/services/google.service';
import { AppleService } from './application/services/apple.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, EmailVerification, EmailLog, SocialAccount]),
    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 },
    { provide: EMAIL_VERIFICATION_REPOSITORY, useClass: EmailVerificationRepository },
    { provide: EMAIL_LOG_REPOSITORY, useClass: EmailLogRepository },
    { provide: SOCIAL_ACCOUNT_REPOSITORY, useClass: SocialAccountRepository },

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

    // Email Provider: toggle by EMAIL_ENABLED env var
    {
      provide: EMAIL_PROVIDER,
      useClass:
        process.env.EMAIL_ENABLED === 'true'
          ? GmailProvider
          : ConsoleEmailProvider,
    },

    // Infrastructure
    JwtStrategy,
    TokenBlacklistService,
    SmsCodeService,
    EmailCodeService,
    WechatProvider,
    AlipayProvider,
    GoogleProvider,
    AppleProvider,

    // Application services
    AuthService,
    TokenService,
    SmsService,
    EmailService,
    WechatService,
    AlipayService,
    GoogleService,
    AppleService,
    EventPublisherService,
  ],
  exports: [AuthService, TokenService, SmsService, EmailService, WechatService, AlipayService, GoogleService, AppleService],
})
export class AuthModule {}
