import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TransformInterceptor } from 'src/interceptors/transform.interceptor';
import { AuthorizationController } from './authorization.controller';
import { AuthorizationService } from './authorization.service';
import { UsersService } from '../backend/users/users.service';

import { JwtModule } from '@nestjs/jwt';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { PassportModule } from '@nestjs/passport';
import { JwtStrategy } from './jwt.strategy';
/**
 * AuthorizationModule - Handles authentication and authorization logic
 */
@Module({
  imports: [
    /**
     * Global config (loads .env)
     */
    ConfigModule.forRoot({
      isGlobal: true,
    }),

    /**
     * Database entities (uncomment when ready)
     */
    TypeOrmModule.forFeature([
      // UsersEntity,
      // RolesEntity,
      // UserSettingsEntity,
      // PasswordResetsEntity,
    ]),

    /**
     * ✅ JWT Configuration (FIXED ISSUE HERE)
     */
    JwtModule.registerAsync({
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        secret: config.get<string>('JWT_SECRET') || 'fallback_secret',
        signOptions: {
          expiresIn: config.get<string>('JWT_EXPIRES_IN') || '1d',
        },
      }),
    }),
    PassportModule.register({ defaultStrategy: 'jwt' }),
    /**
     * HTTP Module
     */
    HttpModule.register({
      timeout: 5000,
      maxRedirects: 5,
    }),

    /**
     * Event system
     */
    EventEmitterModule.forRoot(),
  ],

  controllers: [AuthorizationController],

  providers: [
    /**
     * Core services
     */
    AuthorizationService,
    UsersService,
    JwtStrategy,
    /**
     * Global response interceptor
     */
    {
      provide: APP_INTERCEPTOR,
      useClass: TransformInterceptor,
    },
  ],

  /**
   * Export JWT if needed in other modules
   */
  exports: [JwtModule, AuthorizationService],
})
export class AuthorizationModule {}