import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { join } from 'node:path';
import { AdminController } from './controllers/admin.controller';
import { AuthController } from './controllers/auth.controller';
import { RbacController } from './controllers/rbac.controller';
import { SecurityController } from './controllers/security.controller';
import { SettingsController } from './controllers/settings.controller';
import { HealthController } from './controllers/health.controller';
import { PublicSettingsController } from './controllers/public-settings.controller';
import { ProfileController } from './controllers/profile.controller';
import { DocumentsController } from './controllers/documents.controller';
import { AddressesController } from './controllers/addresses.controller';
import { OAuthController } from './controllers/oauth.controller';
import { WellKnownController, OAuthAuthorizeDiscoveryController } from './controllers/well-known.controller';
import { AdvancedAuthController } from './controllers/advanced-auth.controller';
import { FamilyController } from './controllers/family.controller';
import { ChatController } from './controllers/chat.controller';
import { NotificationsController } from './controllers/notifications.controller';
import { MediaController } from './controllers/media.controller';
import { BotController } from './controllers/bot.controller';
import { AdminBotController } from './controllers/admin-bot.controller';
import { TelegramBotApiController } from './controllers/telegram-bot-api.controller';
import { CoreGrpcService } from './core-grpc.service';
import { AdminGuard, RbacManageGuard, SuperAdminGuard } from './guards/admin.guard';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    JwtModule.register({}),
    ClientsModule.registerAsync([
      {
        name: 'SSO_CORE',
        imports: [ConfigModule],
        inject: [ConfigService],
        useFactory: (config: ConfigService) => ({
          transport: Transport.GRPC,
          options: {
            package: ['auth', 'admin', 'rbac', 'security', 'profile', 'documents', 'addresses', 'identity', 'media', 'notifications', 'chat', 'bot'],
            protoPath: [
              join(__dirname, '../../../shared/proto/auth.proto'),
              join(__dirname, '../../../shared/proto/admin.proto'),
              join(__dirname, '../../../shared/proto/rbac.proto'),
              join(__dirname, '../../../shared/proto/security.proto'),
              join(__dirname, '../../../shared/proto/profile.proto'),
              join(__dirname, '../../../shared/proto/documents.proto'),
              join(__dirname, '../../../shared/proto/addresses.proto'),
              join(__dirname, '../../../shared/proto/identity.proto'),
              join(__dirname, '../../../shared/proto/media.proto'),
              join(__dirname, '../../../shared/proto/notifications.proto'),
              join(__dirname, '../../../shared/proto/chat.proto'),
              join(__dirname, '../../../shared/proto/bot.proto')
            ],
            url: config.get<string>('SSO_CORE_GRPC_URL', 'localhost:50051')
          }
        })
      }
    ])
  ],
  controllers: [AuthController, AdminController, RbacController, SecurityController, SettingsController, PublicSettingsController, HealthController, ProfileController, DocumentsController, AddressesController, OAuthController, WellKnownController, OAuthAuthorizeDiscoveryController, AdvancedAuthController, FamilyController, ChatController, NotificationsController, MediaController, BotController, AdminBotController, TelegramBotApiController],
  providers: [CoreGrpcService, AdminGuard, RbacManageGuard, SuperAdminGuard]
})
export class AppModule {}
