import { Module, NestModule, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ScheduleModule } from '@nestjs/schedule';
import {
  TenantContextModule,
  TenantContextMiddleware,
  SimpleTenantFinder,
  DEFAULT_TENANT_ID,
} from '@iconsulting/shared';
import { EvolutionModule } from './evolution/evolution.module';
import { AdminModule } from './admin/admin.module';
import { HealthModule } from './health/health.module';
import { AnalyticsModule } from './analytics/analytics.module';
import { TenantModule } from './infrastructure/tenant/tenant.module';
import { SuperAdminModule } from './super-admin/super-admin.module';

@Module({
  imports: [
    // 配置模块
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: ['.env.local', '.env'],
    }),

    // 定时任务模块
    ScheduleModule.forRoot(),

    // 数据库连接
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        type: 'postgres',
        host: config.get('POSTGRES_HOST', 'localhost'),
        port: config.get<number>('POSTGRES_PORT', 5432),
        username: config.get('POSTGRES_USER', 'postgres'),
        password: config.get('POSTGRES_PASSWORD'),
        database: config.get('POSTGRES_DB', 'iconsulting'),
        autoLoadEntities: true,
        // 禁用synchronize，使用init-db.sql初始化schema
        synchronize: false,
        logging: config.get('NODE_ENV') === 'development',
      }),
    }),

    // 租户模块 (提供 TenantFinderService 用于 super-admin 租户管理)
    TenantModule,

    // Tenant context module (global) - 使用 SimpleTenantFinder
    TenantContextModule.forRoot({
      tenantFinder: SimpleTenantFinder,
      middlewareOptions: {
        allowDefaultTenant: true,
        defaultTenantId: DEFAULT_TENANT_ID,
        excludePaths: ['/health', '/health/*', '/super-admin/*'],
      },
      isGlobal: true,
    }),

    // Health check
    HealthModule,

    // 功能模块
    EvolutionModule,
    AdminModule,
    AnalyticsModule,

    // 超级管理员模块 (租户管理)
    SuperAdminModule,
  ],
})
export class AppModule implements NestModule {
  constructor(
    private readonly tenantMiddleware: TenantContextMiddleware,
  ) {}

  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply((req: any, res: any, next: any) => this.tenantMiddleware.use(req, res, next))
      .exclude(
        { path: 'health', method: RequestMethod.GET },
        { path: 'health/(.*)', method: RequestMethod.ALL },
        { path: 'super-admin', method: RequestMethod.ALL },
        { path: 'super-admin/(.*)', method: RequestMethod.ALL },
      )
      .forRoutes('*');
  }
}
