import "server-only"

import { enrollStudentByInvitationCode } from "@/modules/classes/data-access"

export type ClassRegistrationResult = {
  success: boolean
  error?: string
}

/**
 * 通过邀请码将学生注册到班级。
 *
 * 委托给 classes 模块的 data-access，避免跨模块直接写入 classEnrollments 表。
 * 返回结构化结果而非抛出异常，便于批量导入场景累计错误。
 */
export async function registerStudentByInvitationCode(
  studentId: string,
  invitationCode: string
): Promise<ClassRegistrationResult> {
  try {
    await enrollStudentByInvitationCode(studentId, invitationCode)
    return { success: true }
  } catch (e) {
    const msg = e instanceof Error ? e.message : "注册失败"
    return { success: false, error: msg }
  }
}
