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

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
  async onModuleInit() {
    await this.$connect();
    console.log('✅ Database connected successfully');
  }

  async onModuleDestroy() {
    await this.$disconnect();
    console.log('👋 Database disconnected');
  }

  // 清理测试数据的辅助方法
  async cleanDatabase() {
    if (process.env.NODE_ENV === 'production') {
      throw new Error('Cannot clean database in production');
    }

    // 按照外键依赖顺序删除
    await this.studentRecord.deleteMany();
    await this.lessonFeedback.deleteMany();
    await this.lesson.deleteMany();
    await this.tenantCourse.deleteMany();
    await this.courseScriptPage.deleteMany();
    await this.courseScript.deleteMany();
    await this.courseActivity.deleteMany();
    await this.courseResource.deleteMany();
    await this.course.deleteMany();
    await this.student.deleteMany();
    await this.class.deleteMany();
    await this.teacher.deleteMany();
    await this.tenant.deleteMany();
    await this.tag.deleteMany();
  }
}
