// useChildExamResult：获取子女考试结果（逐题回顾）
// 依据：02-architecture-design.md §4.2 GraphQL 接入
// 对标 student-portal /my-exams/[id]/result 页面（家长只读视角）

"use client";

import { useQuery, type CombinedError } from "urql";
import { CHILD_EXAM_RESULT } from "@/lib/graphql/operations";
import type { ExamResult } from "@/types";
import { useChildStore } from "@/store/child-store";

interface ChildExamResultResponse {
  childExamResult: ExamResult;
}

export function useChildExamResult(examId: string): {
  result: ExamResult | null;
  loading: boolean;
  error: CombinedError | undefined;
} {
  const currentChildId = useChildStore((s) => s.currentChildId);

  const [res] = useQuery<ChildExamResultResponse>({
    query: CHILD_EXAM_RESULT,
    variables: { childId: currentChildId, examId },
    pause: !currentChildId || !examId,
  });

  return {
    result: res.data?.childExamResult ?? null,
    loading: res.fetching,
    error: res.error,
  };
}
