import { DynamicModule, Module } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { AnalyticsModule } from './analytics/analytics.module';
import { SearchModule } from './search/search.module';
import { AssistantModule } from './assistant/assistant.module';
import { AssetsModule } from './assets/assets.module';
import { DocumentsModule } from './documents/documents.module';
import { AuthModule } from './auth/auth.module';
import { DemoAuthModule } from './auth/demo/demo-auth.module';
import { JwtAuthGuard } from './auth/jwt-auth.guard';
import { CategoriesModule } from './categories/categories.module';
import { demoModeEnabled } from './config/env';
import { FilesModule } from './files/files.module';
import { HealthModule } from './health/health.module';
import { LocationsModule } from './locations/locations.module';
import { MetersModule } from './meters/meters.module';
import { PartnersModule } from './partners/partners.module';
import { PartsModule } from './parts/parts.module';
import { PortalModule } from './portal/portal.module';
import { PurchaseOrdersModule } from './purchase-orders/purchase-orders.module';
import { PreventiveModule } from './preventive/preventive.module';
import { ReferenceValuesModule } from './reference-values/reference-values.module';
import { RequestsModule } from './requests/requests.module';
import { TeamsModule } from './teams/teams.module';
import { WorkOrdersModule } from './work-orders/work-orders.module';
import { PermissionsGuard } from './permissions/permissions.guard';
import { PermissionsModule } from './permissions/permissions.module';
import { PrismaModule } from './prisma/prisma.module';
import { UsersModule } from './users/users.module';

/**
 * Composé via forRoot() (et non statiquement) pour que l'enregistrement
 * conditionnel de DemoAuthModule (ADR-002) soit décidé au démarrage —
 * et testable dans les deux états (module présent / routes 404).
 */
@Module({})
export class AppModule {
  static forRoot(): DynamicModule {
    return {
      module: AppModule,
      imports: [
        PrismaModule,
        PermissionsModule,
        FilesModule,
        AuthModule,
        UsersModule,
        HealthModule,
        // R1 — référentiel
        CategoriesModule,
        LocationsModule,
        AssetsModule,
        TeamsModule,
        // R2 — exploitation
        WorkOrdersModule,
        RequestsModule,
        ReferenceValuesModule,
        PreventiveModule,
        MetersModule,
        PortalModule,
        // R3 — gestion
        PartnersModule,
        PartsModule,
        PurchaseOrdersModule,
        DocumentsModule,
        AnalyticsModule,
        SearchModule,
        AssistantModule,
        // ADR-002 : hors DEMO_MODE, le module n'est pas enregistré → 404
        ...(demoModeEnabled() ? [DemoAuthModule] : []),
      ],
      providers: [
        // Ordre significatif : authentification puis permissions
        { provide: APP_GUARD, useClass: JwtAuthGuard },
        { provide: APP_GUARD, useClass: PermissionsGuard },
      ],
    };
  }
}
