// useReportCard：获取子女报告卡
// 依据：02-architecture-design.md §4.2 GraphQL 接入

"use client";

import { useQuery, type CombinedError } from "urql";
import { CHILD_REPORT_CARD } from "@/lib/graphql/operations";
import type { ReportCard } from "@/types";

interface ChildReportCardResponse {
  childReportCard: ReportCard;
}

export function useReportCard(
  childId: string | null,
  academicYearId: string | null,
  semester: number,
): {
  reportCard: ReportCard | null;
  loading: boolean;
  error: CombinedError | undefined;
} {
  const [result] = useQuery<ChildReportCardResponse>({
    query: CHILD_REPORT_CARD,
    variables: { childId, academicYearId, semester },
    pause: !childId || !academicYearId,
  });

  return {
    reportCard: result.data?.childReportCard ?? null,
    loading: result.fetching,
    error: result.error,
  };
}
