import { Injectable, OnModuleInit, OnModuleDestroy, Logger } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService
  extends PrismaClient
  implements OnModuleInit, OnModuleDestroy
{
  private readonly logger = new Logger(PrismaService.name);

  constructor() {
    super({
      log: [
        { emit: 'event', level: 'query' },
        { emit: 'stdout', level: 'info' },
        { emit: 'stdout', level: 'warn' },
        { emit: 'stdout', level: 'error' },
      ],
    });
  }

  async onModuleInit(): Promise<void> {
    this.logger.log('Verbinde mit PostgreSQL (platform_core)...');
    await this.$connect();
    this.logger.log('PostgreSQL Verbindung hergestellt.');
  }

  async onModuleDestroy(): Promise<void> {
    this.logger.log('Trenne PostgreSQL Verbindung...');
    await this.$disconnect();
  }
}
