import { Global, Module } from "@nestjs/common";
import { Pool } from "pg";
import { drizzle, NodePgDatabase } from "drizzle-orm/node-postgres";
import { ConfigModule, ConfigService } from "@nestjs/config";
import * as schema from '../../drizzle/schema';

@Global()
@Module({
  imports: [ConfigModule], 
  providers: [
    {
      provide: "DRIZZLE",
      useFactory: (configService: ConfigService): NodePgDatabase<typeof schema> => {
        const pool = new Pool({
          connectionString: configService.get<string>('PG_DATABASE_URL')
        });
        return drizzle(pool, { schema: schema });
      },
      inject: [ConfigService]
    }
  ],
  exports: ["DRIZZLE"]
})
export class DbModule {}