// useChildDiagnostic：获取子女诊断报告数据（掌握度摘要 + 诊断报告列表）
// 依据：02-architecture-design.md §4.2 GraphQL 接入
// 同时发起两个查询，合并为统一返回值

"use client";

import { useQuery, type CombinedError } from "urql";
import {
  CHILD_MASTERY_SUMMARY,
  CHILD_DIAGNOSTIC_REPORTS,
} from "@/lib/graphql/operations";
import type { MasterySummary, DiagnosticReport } from "@/types";
import { useChildStore } from "@/store/child-store";

interface ChildMasterySummaryResponse {
  childMasterySummary: MasterySummary;
}

interface ChildDiagnosticReportsResponse {
  childDiagnosticReports: DiagnosticReport[];
}

export function useChildDiagnostic(): {
  masterySummary: MasterySummary | null;
  reports: DiagnosticReport[];
  loading: boolean;
  error: CombinedError | undefined;
} {
  const currentChildId = useChildStore((s) => s.currentChildId);

  const [masteryResult] = useQuery<ChildMasterySummaryResponse>({
    query: CHILD_MASTERY_SUMMARY,
    variables: { childId: currentChildId },
    pause: !currentChildId,
  });

  const [reportsResult] = useQuery<ChildDiagnosticReportsResponse>({
    query: CHILD_DIAGNOSTIC_REPORTS,
    variables: { childId: currentChildId },
    pause: !currentChildId,
  });

  return {
    masterySummary: masteryResult.data?.childMasterySummary ?? null,
    reports: reportsResult.data?.childDiagnosticReports ?? [],
    loading: masteryResult.fetching || reportsResult.fetching,
    error: masteryResult.error ?? reportsResult.error,
  };
}
