import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AccessControlModule } from 'nest-access-control';
import {
  // utilities as nestWinstonModuleUtilities,
  WinstonModule,
} from 'nest-winston';
import { PrismaModule } from 'nestjs-prisma';
import * as winston from 'winston';
import config from '../config';
import { roles as appRoles } from '../config/ac.config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import modules from './modules';

import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true, load: [config] }),

    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'www'),
      exclude: ['/api/(.*)'],
    }),

    AccessControlModule.forRoles(appRoles),

    WinstonModule.forRootAsync({
      useFactory: (configService: ConfigService) => {
        const loggerConfig = configService.get('logger');
        return {
          level: loggerConfig.level,
          transports: [
            new winston.transports.Console({
              format: winston.format.combine(
                // nestWinstonModuleUtilities.format.nestLike(),
                winston.format.splat(),
                winston.format.timestamp(),
                // winston.format.ms(),
                winston.format.json(),
                // nestWinstonModuleUtilities.format.nestLike('MyApp', { prettyPrint: true }),
              ),
            }),
          ],
        };
      },
      inject: [ConfigService],
    }),

    PrismaModule.forRootAsync({
      isGlobal: true,
      useFactory: async (configService: ConfigService) => {
        const prismaConfig = configService.get('prisma');
        return {
          prismaOptions: {
            log: prismaConfig.log,
          },
          explicitConnect: prismaConfig.explicit,
        };
      },
      inject: [ConfigService],
    }),

    ...modules,
  ],

  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
