import pino from 'pino';
import { Module, RequestMethod, MiddlewareConsumer, Inject } from '@nestjs/common';
import { SharedModule } from './shared/shared.module';
import { ConfigModule } from '@nestjs/config';
import { UserModule } from './modules/user/user.module';
import { PermissionModule } from './modules/permission/permission.module';
import { RoleModule } from './modules/role/role.module';
import { AuthModule } from './modules/auth/auth.module';
import { ArticleModule } from './modules/article/article.module';
import { CategoryModule } from './modules/category/category.module';
import { LoggerModule } from 'nestjs-pino';
import { MenuModule } from './modules/menu/menu.module';
import { HttpLoggerMiddleware } from './common/middleware/httpOperationMiddleware';
import { OperationLogModule } from './modules/operation-log/operation-log.module';
import { WebModule } from './modules/web/web.module';
import { SessionMiddleware } from '@/common/middleware/session.middleware';

@Module({
  imports: [
    LoggerModule.forRoot({
      pinoHttp: {
        stream: pino.destination({
          dest: './helper-log', // omit for stdout
          minLength: 4096, // Buffer before writing
          sync: true, // Asynchronous logging
        }),
      },
    }),
    /* 配置文件模块 */
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: ['.env.local', '.env'],
    }),

    UserModule,
    PermissionModule,
    RoleModule,
    AuthModule,

    SharedModule,

    ArticleModule,

    CategoryModule,

    MenuModule,

    OperationLogModule.forRoot(),

    WebModule,
  ],
  controllers: [],
  providers: [],
})
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(SessionMiddleware).forRoutes({
      path: '*',
      method: RequestMethod.ALL,
    }); // 对所有路由应用中间件
    consumer.apply(HttpLoggerMiddleware).forRoutes({
      path: '*',
      method: RequestMethod.ALL,
    });
  }
}
