import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { HttpModule } from '@nestjs/axios';
import { BullModule } from '@nestjs/bullmq';
import { Module } from '@nestjs/common';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { GraphQLModule } from '@nestjs/graphql';
import { ScheduleModule } from '@nestjs/schedule';
import { PostgresDialect } from 'kysely';
import { join } from 'path';
import { Pool } from 'pg';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from './core/config/config.module';
import { DatabaseModule } from './core/database/database.module';
import { PrismaModule } from './core/database/prisma.module';
import { ApplicationAccountingModule } from './modules/app-accounting/app-accounting.module';
import { ApplicationControlCenterModule } from './modules/app-control-center/app-control-center.module';
import { AppHomeModule } from './modules/app-home/app-home.module';
import { ManagerModule } from './modules/app-manager/app-manager.module';
import { ApplicationTicketSystemModule } from './modules/app-ticket-system/app-ticket-system.module';
import { AnomalyDetectionModule } from './modules/feat-anomalies/anomaly-detection.module';
import { FeatCalculationModule } from './modules/feat-calculation/feat-calculation.module';
import { FeatureEtlModule } from './modules/feat-etl/feat-etl.module';
import { FeatReportsModule } from './modules/feat-reports/feat-reports.module';
import { FeatTicketSystemModule } from './modules/feat-tickets/ticket-system.module';
import { FeatBusinessObjectsModule } from './modules/feat-business-objects/feat-business-objects.module';

@Module({
  imports: [
    ConfigModule,
    // DatabaseModule.forRootAsync({
    //   isGlobal: true,
    //   inject: [ConfigService],
    //   useFactory(config: ConfigService) {
    //     return {
    //       config: {
    //         dialect: new PostgresDialect({
    //           pool: new Pool({
    //             host: config.databaseHost,
    //             port: config.databasePort,
    //             database: config.databaseName,
    //             user: config.databaseUser,
    //             password: config.databasePassword,
    //           }),
    //         }),
    //       },
    //     };
    //   },
    // }),
    DatabaseModule.forRoot({
      // isGlobal: true,
      dialect: new PostgresDialect({
        pool: new Pool({
          host: 'localhost',
          port: 5432,
          database: 'avicenna',
          user: 'johndoe',
          password: 'randompassword',
        }),
      }),
    }),
    BullModule.forRoot({
      connection: {
        host: 'localhost',
        port: 6379,
      },
    }),
    // DatabaseModule.forRoot({
    //   dialect: new PostgresDialect({
    //     pool: new Pool({
    //       host: 'localhost',
    //       port: 54322,
    //       database: 'postgres',
    //       user: 'postgres',
    //       password: 'postgres',
    //     }),
    //   }),
    // }),
    PrismaModule.forRoot(),
    ScheduleModule.forRoot(),
    EventEmitterModule.forRoot(),
    GraphQLModule.forRoot<ApolloDriverConfig>({
      driver: ApolloDriver,
      playground: true,
      autoSchemaFile: join(process.cwd(), 'src/core/database/schema.gql'),
      sortSchema: true,
      subscriptions: {
        'graphql-ws': true,
      },
    }),
    FeatureEtlModule,
    FeatReportsModule,
    FeatCalculationModule,
    FeatTicketSystemModule,
    AnomalyDetectionModule,
    ManagerModule,
    ApplicationAccountingModule,
    ApplicationControlCenterModule,
    ApplicationTicketSystemModule,
    AppHomeModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
