import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {

        return {
          type: 'mysql',
          host: configService.get<string>('DB_HOST'),
          port: configService.get<number>('DB_PORT'),
          username: configService.get<string>('DB_USERNAME'),
          password: configService.get<string>('DB_PASSWORD'),
          database: configService.get<string>('DB_NAME'),
          charset: 'utf8mb4_unicode_ci',
          entities: ['dist/**/*.entity{.ts,.js}'],
          synchronize:
            configService.get<string>('ENVIRONMENT') === 'prod' ? false : true,
        }
      },
    }),
  ],
})
export class DatabasesModule {}
