import { Module } from '@nestjs/common';
import { Kysely, PostgresDialect } from 'kysely';
import type { Pool } from 'pg';
import { PostgresModule } from '../../infrastructure/src';
import { POSTGRES_POOL } from '../../infrastructure/src';
import type { Database } from './schema';

export const KYSELY_DB = Symbol('KYSELY_DB');

export const kyselyDbProvider = {
  provide: KYSELY_DB,
  inject: [POSTGRES_POOL],
  useFactory: (pool: Pool) =>
    new Kysely<Database>({ dialect: new PostgresDialect({ pool }) }),
};

@Module({
  imports: [PostgresModule],
  providers: [kyselyDbProvider],
  exports: [kyselyDbProvider],
})
export class DatabaseModule {}
