// useChildGrades：获取子女成绩列表
// 依据：02-architecture-design.md §4.2 GraphQL 接入

"use client";

import { useQuery } from "urql";
import { CHILD_GRADES } from "@/lib/graphql/operations";
import type { GradeDataPoint } from "@/types";
import { useChildStore } from "@/store/child-store";

interface ChildGradesResponse {
  childGrades: GradeDataPoint[];
}

export function useChildGrades(subject?: string) {
  const currentChildId = useChildStore((s) => s.currentChildId);

  const [result] = useQuery<ChildGradesResponse>({
    query: CHILD_GRADES,
    variables: { childId: currentChildId, subject: subject ?? null },
    pause: !currentChildId,
  });

  return {
    grades: result.data?.childGrades ?? [],
    loading: result.fetching,
    error: result.error,
  };
}
