import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
import { BaseService } from '../base/base.service';
import {
  UserProfile,
  db,
  ObjectType,
  Prisma,
  EnrollmentStatus
} from '@nice/common';
import { z } from 'zod';
import { EnrollSchema, UnenrollSchema } from './enroll.schema';
import EventBus, { CrudOperation } from '@server/utils/event-bus';

@Injectable()
export class EnrollmentService extends BaseService<Prisma.EnrollmentDelegate> {
  constructor() {
    super(db, ObjectType.COURSE);
  }
  async enroll(params: z.infer<typeof EnrollSchema>) {
    const { studentId, courseId } = params
    const result = await db.$transaction(async tx => {
      // 检查是否已经报名
      const existing = await tx.enrollment.findUnique({
        where: {
          studentId_courseId: {
            studentId: studentId,
            courseId: courseId,
          },
        },
      });
      if (existing) {
        throw new ConflictException('Already enrolled in this course');
      }
      // 创建报名记录
      const enrollment = await tx.enrollment.create({
        data: {
          studentId: studentId,
          courseId: courseId,
          status: EnrollmentStatus.ACTIVE,
        },
      });

      return enrollment;
    });
 
    EventBus.emit('dataChanged', {
      type: ObjectType.ENROLLMENT,
      operation: CrudOperation.CREATED,
      data: result,
    });
    return result
  }
  async unenroll(params: z.infer<typeof UnenrollSchema>) {
    const { studentId, courseId } = params
    const result = await db.enrollment.update({
      where: {
        studentId_courseId: {
          studentId,
          courseId,
        },
      },
      data: {
        status: EnrollmentStatus.CANCELLED
      }
    });
  
    EventBus.emit('dataChanged', {
      type: ObjectType.ENROLLMENT,
      operation: CrudOperation.UPDATED,
      data: result,
    });
    return result
  }
}
