import type { ActionState } from "@/shared/types/action-state"

import {
  generateStudentReportAction,
  generateClassReportAction,
  generateGradeReportAction,
  publishReportAction,
  deleteReportAction,
  exportDiagnosticReportAction,
  getClassStudentsByKnowledgePointAction,
} from "../actions"
import type {
  DiagnosticService,
  ExportResult,
  KnowledgePointStudent,
} from "./diagnostic-service"

/**
 * v2-P1-4: 诊断模块默认服务实现。
 *
 * 绑定现有 Server Actions，作为 DiagnosticServiceProvider 的默认注入值。
 * 测试时可替换为 mock 实现以隔离组件测试。
 */
export const defaultDiagnosticService: DiagnosticService = {
  async generateStudentReport(
    studentId: string,
    period: string,
  ): Promise<ActionState<string>> {
    const formData = new FormData()
    formData.set("studentId", studentId)
    formData.set("period", period)
    return generateStudentReportAction(null, formData)
  },

  async generateClassReport(
    classId: string,
    period: string,
  ): Promise<ActionState<string>> {
    const formData = new FormData()
    formData.set("classId", classId)
    formData.set("period", period)
    return generateClassReportAction(null, formData)
  },

  async generateGradeReport(
    gradeId: string,
    period: string,
  ): Promise<ActionState<string>> {
    const formData = new FormData()
    formData.set("gradeId", gradeId)
    formData.set("period", period)
    return generateGradeReportAction(null, formData)
  },

  async publishReport(id: string): Promise<ActionState<null>> {
    const formData = new FormData()
    formData.set("id", id)
    const result = await publishReportAction(null, formData)
    return { success: result.success, message: result.message }
  },

  async deleteReport(id: string): Promise<ActionState<null>> {
    const formData = new FormData()
    formData.set("id", id)
    const result = await deleteReportAction(null, formData)
    return { success: result.success, message: result.message }
  },

  async exportReport(reportId: string): Promise<ActionState<ExportResult>> {
    return exportDiagnosticReportAction(reportId)
  },

  async getClassStudentsByKp(
    classId: string,
    knowledgePointId: string,
    threshold?: number,
  ): Promise<ActionState<KnowledgePointStudent[]>> {
    return getClassStudentsByKnowledgePointAction({
      classId,
      knowledgePointId,
      threshold,
    })
  },
}
