import { Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JwtModule, JwtSignOptions } from '@nestjs/jwt';
import { MongooseModule } from '@nestjs/mongoose';
import { EmailModule } from '../email/email.module';
import { UsersModule } from '../users/users.module';
import { AuthController } from './auth.controller';
import { AuthRepository } from './auth.repository';
import { AuthService } from './auth.service';
import { GoogleAuthGuard } from './guards/google-auth.guard';
import {
  EmailVerificationCode,
  EmailVerificationCodeSchema,
} from './schemas/email-verification-code.schema';
import {
  PasswordResetCode,
  PasswordResetCodeSchema,
} from './schemas/password-reset-code.schema';
import { RefreshToken, RefreshTokenSchema } from './schemas/refresh-token.schema';
import { SignupDraft, SignupDraftSchema } from './schemas/signup-draft.schema';
import {
  SuperAdminRefreshToken,
  SuperAdminRefreshTokenSchema,
} from './schemas/super-admin-refresh-token.schema';
import { GoogleStrategy } from './strategies/google.strategy';
import { JwtRefreshStrategy } from './strategies/jwt-refresh.strategy';
import { JwtStrategy } from './strategies/jwt.strategy';
import { SuperAdminJwtStrategy } from './strategies/super-admin-jwt.strategy';

@Module({
  imports: [
    JwtModule.registerAsync({
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        secret: configService.get<string>('jwt.accessSecret', { infer: true }),
        signOptions: {
          expiresIn: configService.get<string>('jwt.accessExpiresIn', { infer: true }) as JwtSignOptions['expiresIn'],
        },
      }),
    }),
    MongooseModule.forFeature([
      {
        name: RefreshToken.name,
        schema: RefreshTokenSchema,
      },
      {
        name: SuperAdminRefreshToken.name,
        schema: SuperAdminRefreshTokenSchema,
      },
      {
        name: PasswordResetCode.name,
        schema: PasswordResetCodeSchema,
      },
      {
        name: EmailVerificationCode.name,
        schema: EmailVerificationCodeSchema,
      },
      {
        name: SignupDraft.name,
        schema: SignupDraftSchema,
      },
    ]),
    EmailModule,
    UsersModule,
  ],
  controllers: [AuthController],
  providers: [
    AuthService,
    AuthRepository,
    JwtStrategy,
    JwtRefreshStrategy,
    GoogleStrategy,
    GoogleAuthGuard,
    SuperAdminJwtStrategy,
  ],
  exports: [AuthService],
})
export class AuthModule {}
