import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { APP_GUARD } from '@nestjs/core';
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';
import { ScheduleModule } from '@nestjs/schedule';
import { HealthModule } from './health/health.module';
import { PrismaModule } from './prisma/prisma.module';
import { RedisModule } from './redis/redis.module';
import { AuthModule } from './core/auth/auth.module';
import { UsersModule } from './core/users/users.module';
import { TenantsModule } from './core/tenants/tenants.module';
import { ExpertProfileModule } from './core/expert-profile/expert-profile.module';
import { SettingsModule } from './core/settings/settings.module';
import { IntegrationsModule } from './core/integrations/integrations.module';
import { JwtAuthGuard } from './common/guards/jwt-auth.guard';
import { validateConfig } from './config/env.validation';

@Module({
  imports: [
    // Konfiguration (.env)
    ConfigModule.forRoot({
      isGlobal: true,
      validate: validateConfig,
    }),

    // Rate Limiting
    ThrottlerModule.forRoot([
      {
        ttl: parseInt(process.env.THROTTLE_TTL ?? '60000', 10),
        limit: parseInt(process.env.THROTTLE_LIMIT ?? '200', 10),
      },
    ]),

    // Cron-Jobs
    ScheduleModule.forRoot(),

    // Infrastruktur-Module
    PrismaModule,
    RedisModule,

    // Feature-Module
    HealthModule,
    AuthModule,
    UsersModule,
    TenantsModule,
    ExpertProfileModule,
    SettingsModule,
    IntegrationsModule,
  ],
  providers: [
    // Global Guards: Alle Routen sind standardmaessig geschuetzt
    // Oeffentliche Routen muessen mit @Public() dekoriert werden
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
    {
      provide: APP_GUARD,
      useClass: ThrottlerGuard,
    },
  ],
})
export class AppModule {}
