import { DatabaseType } from 'typeorm';
import { Module } from '@nestjs/common';
import { TypeOrmModule, TypeOrmModuleAsyncOptions } from '@nestjs/typeorm';
import { MySQLConfigModule } from '../../../config/database/mysql/config.module';
import { MySQLConfigService } from '../../../config/database/mysql/config.service';

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [MySQLConfigModule],
      useFactory: async (mySQLConfigService: MySQLConfigService) => ({
        type: 'mysql' as DatabaseType,
        host: mySQLConfigService.host,
        port: mySQLConfigService.port,
        username: mySQLConfigService.username,
        password: mySQLConfigService.password,
        database: mySQLConfigService.database,
        entities: [`${__dirname}/../../../**/*.entity{.ts,.js}`],
        timezone: mySQLConfigService.timezone,
      }),
      inject: [MySQLConfigService],
    } as TypeOrmModuleAsyncOptions),
  ],
})
export class MySQLDatabaseProviderModule {}
