import { MiddlewareConsumer, Module } from '@nestjs/common';
import { HealthController } from './health.controller';
import { ServeStaticModule } from '@nestjs/serve-static';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { join } from 'path';
import { LoggerMiddleware } from './common/loggerMiddleware';
import { AuthModule } from './features/auth/auth.module';
import { AuthController } from './features/auth/auth.controller';
import { AuthService } from './features/auth/auth.service';
import { AdB2cModule } from './gateways/adb2c/adb2c.module';
import { AccountsController } from './features/accounts/accounts.controller';
import { AccountsService } from './features/accounts/accounts.service';
import { AccountsModule } from './features/accounts/accounts.module';
import { UsersController } from './features/users/users.controller';
import { UsersService } from './features/users/users.service';
import { UsersModule } from './features/users/users.module';
import { AccountsRepositoryModule } from './repositories/accounts/accounts.repository.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { SendGridModule } from './gateways/sendgrid/sendgrid.module';
import { UsersRepositoryModule } from './repositories/users/users.repository.module';
import { LicensesRepositoryModule } from './repositories/licenses/licenses.repository.module';
import { AudioFilesRepositoryModule } from './repositories/audio_files/audio_files.repository.module';
import { AudioOptionItemsRepositoryModule } from './repositories/audio_option_items/audio_option_items.repository.module';
import { TasksRepositoryModule } from './repositories/tasks/tasks.repository.module';
import { NotificationhubModule } from './gateways/notificationhub/notificationhub.module';
import { NotificationhubService } from './gateways/notificationhub/notificationhub.service';
import { NotificationModule } from './features/notification/notification.module';
import { FilesModule } from './features/files/files.module';
import { FilesController } from './features/files/files.controller';
import { FilesService } from './features/files/files.service';
import { TasksService } from './features/tasks/tasks.service';
import { TasksController } from './features/tasks/tasks.controller';
import { TasksModule } from './features/tasks/tasks.module';
import { AuthGuardsModule } from './common/guards/auth/authguards.module';
import { BlobstorageModule } from './gateways/blobstorage/blobstorage.module';
import { LicensesModule } from './features/licenses/licenses.module';
import { LicensesService } from './features/licenses/licenses.service';
import { LicensesController } from './features/licenses/licenses.controller';
import { CheckoutPermissionsRepositoryModule } from './repositories/checkout_permissions/checkout_permissions.repository.module';
import { UserGroupsRepositoryModule } from './repositories/user_groups/user_groups.repository.module';
import { SortCriteriaRepositoryModule } from './repositories/sort_criteria/sort_criteria.repository.module';
import { TemplateFilesRepositoryModule } from './repositories/template_files/template_files.repository.module';

@Module({
  imports: [
    ServeStaticModule.forRootAsync({
      useFactory: () =>
        process.env.STAGE === 'local'
          ? [
              {
                rootPath: join(__dirname, '..', 'build'),
              },
            ]
          : [],
    }),
    ConfigModule.forRoot({
      envFilePath: ['.env.local', '.env'],
      isGlobal: true,
    }),
    AuthModule,
    AdB2cModule,
    AccountsModule,
    UsersModule,
    FilesModule,
    TasksModule,
    UsersModule,
    SendGridModule,
    LicensesModule,
    AccountsRepositoryModule,
    UsersRepositoryModule,
    LicensesRepositoryModule,
    AudioFilesRepositoryModule,
    AudioOptionItemsRepositoryModule,
    TasksRepositoryModule,
    CheckoutPermissionsRepositoryModule,
    UserGroupsRepositoryModule,
    TemplateFilesRepositoryModule,
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.get('DB_HOST'),
        port: configService.get('DB_PORT'),
        username: configService.get('DB_USERNAME'),
        password: configService.get('DB_PASSWORD'),
        database: configService.get('DB_NAME'),
        autoLoadEntities: true, // forFeature()で登録されたEntityを自動的にロード
        synchronize: false, // trueにすると自動的にmigrationが行われるため注意
      }),
      inject: [ConfigService],
    }),
    NotificationModule,
    NotificationhubModule,
    BlobstorageModule,
    AuthGuardsModule,
    SortCriteriaRepositoryModule,
  ],
  controllers: [
    HealthController,
    AuthController,
    AccountsController,
    UsersController,
    FilesController,
    TasksController,
    UsersController,
    LicensesController,
  ],
  providers: [
    AuthService,
    AccountsService,
    UsersService,
    NotificationhubService,
    FilesService,
    TasksService,
    LicensesService,
  ],
})
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware).forRoutes('');
  }
}
