import { Injectable } from "@nestjs/common";
import { classesRepository } from "./classes.repository.js";
import { NotFoundError } from "../shared/errors/application-error.js";
import type { Class } from "./classes.schema.js";

@Injectable()
export class ClassesService {
  async getClass(id: string): Promise<Class> {
    const cls = await classesRepository.findById(id);
    if (!cls) {
      throw new NotFoundError(`Class ${id} not found`);
    }
    return cls;
  }

  async getClassesByTeacher(teacherId: string): Promise<Class[]> {
    return classesRepository.findByTeacherId(teacherId);
  }

  async batchGetClasses(ids: string[]): Promise<Class[]> {
    return classesRepository.findByIds(ids);
  }

  /**
   * List students by class.
   * P3: This requires IAM service integration via gRPC.
   * Currently returns empty array as placeholder - IAM consumer will populate
   * teacher_associations and the actual student list comes from IAM.
   */
  async listStudentsByClass(
    classId: string,
  ): Promise<Array<{ id: string; name: string; classId: string }>> {
    // Verify class exists
    await this.getClass(classId);
    // P3: Student list comes from IAM service (gRPC call to iam.GetUserByClass)
    // For now, return empty - this will be implemented when IAM gRPC client is available
    return [];
  }
}
