import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ScheduleModule } from '@nestjs/schedule';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ApiModule } from './api/api.module';
import { ElasticsearchModule } from './elasticsearch/elasticsearch.module';
import { UploadModule } from './upload/upload.module';
import { ChatModule } from './chat/chat.module';
import { AuthModule } from './auth/auth.module';
import { I18nModule } from './i18n/i18n.module';
import { JwtAuthGuard } from './auth/jwt-auth.guard';
import { CombinedAuthGuard } from './auth/combined-auth.guard';
import { KnowledgeBaseModule } from './knowledge-base/knowledge-base.module';
import { ModelConfigModule } from './model-config/model-config.module';
import { UserModule } from './user/user.module';
import { TikaModule } from './tika/tika.module';
import { VisionModule } from './vision/vision.module';
import { LibreOfficeModule } from './libreoffice/libreoffice.module';
import { Pdf2ImageModule } from './pdf2image/pdf2image.module';
import { VisionPipelineModule } from './vision-pipeline/vision-pipeline.module';
import { KnowledgeGroupModule } from './knowledge-group/knowledge-group.module';
import { SearchHistoryModule } from './search-history/search-history.module';
import { NoteModule } from './note/note.module';
import { PodcastModule } from './podcasts/podcast.module';
import { ImportTaskModule } from './import-task/import-task.module';
import { I18nMiddleware } from './i18n/i18n.middleware';
import { TenantMiddleware } from './tenant/tenant.middleware';
import { User } from './user/user.entity';
import { UserSetting } from './user/user-setting.entity';
import { ModelConfig } from './model-config/model-config.entity';
import { KnowledgeBase } from './knowledge-base/knowledge-base.entity';
import { KnowledgeGroup } from './knowledge-group/knowledge-group.entity';
import { SearchHistory } from './search-history/search-history.entity';
import { ChatMessage } from './search-history/chat-message.entity';
import { Note } from './note/note.entity';
import { NoteCategory } from './note/note-category.entity';
import { PodcastEpisode } from './podcasts/entities/podcast-episode.entity';
import { ImportTask } from './import-task/import-task.entity';
import { Tenant } from './tenant/tenant.entity';
import { TenantSetting } from './tenant/tenant-setting.entity';
import { ApiKey } from './auth/entities/api-key.entity';
import { TenantMember } from './tenant/tenant-member.entity';
import { TenantModule } from './tenant/tenant.module';
import { SuperAdminModule } from './super-admin/super-admin.module';
import { AdminModule } from './admin/admin.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      ignoreEnvFile: false,
    }),
    ServeStaticModule.forRoot({
      rootPath: join(process.cwd(), 'uploads'),
      serveRoot: '/uploads',
    }),
    ScheduleModule.forRoot(),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        type: 'better-sqlite3',
        database: configService.get<string>('DATABASE_PATH'),
        entities: [
          User,
          UserSetting,
          ModelConfig,
          KnowledgeBase,
          KnowledgeGroup,
          SearchHistory,
          ChatMessage,
          Note,
          NoteCategory,
          PodcastEpisode,
          ImportTask,
          Tenant,
          TenantSetting,
          TenantMember,
          ApiKey,
        ],
        synchronize: true, // Auto-create database schema. Disable in production.
      }),
    }),
    AuthModule,
    I18nModule,
    UserModule,
    TenantModule,
    ModelConfigModule,
    KnowledgeBaseModule,
    KnowledgeGroupModule,
    SearchHistoryModule,
    NoteModule,
    PodcastModule,
    TikaModule,
    VisionModule,
    LibreOfficeModule,
    Pdf2ImageModule,
    VisionPipelineModule,
    ApiModule,
    ElasticsearchModule,
    UploadModule,
    ChatModule,
    ImportTaskModule,
    SuperAdminModule,
    AdminModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_GUARD,
      useClass: CombinedAuthGuard,
    },
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(I18nMiddleware, TenantMiddleware)
      .forRoutes('*');
  }
}
// Trigger restart correct
