import { MikroORM } from '@mikro-orm/core';
import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs';
// import { SqlHighlighter } from '@mikro-orm/sql-highlighter';
import { DynamicModule, Module, OnModuleInit } from '@nestjs/common';

import { getBbOptions } from './helpers';

@Module({})
export class DatabaseModule implements OnModuleInit {
    constructor(private readonly orm: MikroORM) {}

    async onModuleInit(): Promise<void> {
        await this.orm.getMigrator().up();
    }

    static forRoot(options: MikroOrmModuleSyncOptions = {}): DynamicModule {
        return {
            global: true,
            module: DatabaseModule,
            imports: [
                MikroOrmModule.forRootAsync({
                    useFactory: () => getBbOptions<MikroOrmModuleSyncOptions>(options),
                }),
            ],
            exports: [MikroOrmModule],
        };
    }
}
