// useChildGrowthArchive：获取子女成长档案（班级均对比线）
// 依据：02-architecture-design.md §4.2 GraphQL 接入
// - 依赖 currentChildId（从 Zustand store 读取）
// - 支持可选 childId 参数（多子女场景）
// - 支持可选 subject 参数（按学科筛选）

"use client";

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

interface ChildGrowthArchiveResponse {
  childGrowthArchive: GrowthArchive;
}

interface UseChildGrowthArchiveOptions {
  childId?: string;
  subject?: string;
}

export function useChildGrowthArchive(
  options?: UseChildGrowthArchiveOptions,
): {
  growthArchive: GrowthArchive | null;
  loading: boolean;
  error: CombinedError | undefined;
} {
  const currentChildId = useChildStore((s) => s.currentChildId);
  const targetChildId = options?.childId ?? currentChildId;

  const [result] = useQuery<ChildGrowthArchiveResponse>({
    query: CHILD_GROWTH_ARCHIVE,
    variables: {
      childId: targetChildId,
      subject: options?.subject ?? null,
    },
    pause: !targetChildId,
  });

  return {
    growthArchive: result.data?.childGrowthArchive ?? null,
    loading: result.fetching,
    error: result.error,
  };
}
