// useChildSummary：获取子女仪表盘概览
// 依据：02-architecture-design.md §4.2 GraphQL 接入
// - 依赖 currentChildId（从 Zustand store 读取）
// - 切换子女时自动重新请求

"use client";

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

interface ChildSummaryResponse {
  childSummary: ChildSummary;
}

export function useChildSummary(childId?: string) {
  const currentChildId = useChildStore((s) => s.currentChildId);
  const targetChildId = childId ?? currentChildId;

  const [result] = useQuery<ChildSummaryResponse>({
    query: CHILD_SUMMARY,
    variables: { childId: targetChildId },
    pause: !targetChildId,
  });

  return {
    summary: result.data?.childSummary ?? null,
    loading: result.fetching,
    error: result.error,
  };
}
