// useChildExams：获取子女考试列表
// 依据：02-architecture-design.md §4.2 GraphQL 接入
// 对标 student-portal /my-exams 页面（家长只读视角）

"use client";

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

interface ChildExamsResponse {
  childExams: ExamListItem[];
}

export function useChildExams(): {
  exams: ExamListItem[];
  loading: boolean;
  error: CombinedError | undefined;
} {
  const currentChildId = useChildStore((s) => s.currentChildId);

  const [result] = useQuery<ChildExamsResponse>({
    query: CHILD_EXAMS,
    variables: { childId: currentChildId },
    pause: !currentChildId,
  });

  return {
    exams: result.data?.childExams ?? [],
    loading: result.fetching,
    error: result.error,
  };
}
