// useChildLearningPath：获取子女学习路径（知识点掌握）
// 依据：02-architecture-design.md §4.2 GraphQL 接入
// 对标 student-portal /learning-path 页面（家长只读视角）

"use client";

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

interface ChildLearningPathResponse {
  childLearningPath: LearningPathNode[];
}

export function useChildLearningPath(): {
  nodes: LearningPathNode[];
  loading: boolean;
  error: CombinedError | undefined;
} {
  const currentChildId = useChildStore((s) => s.currentChildId);

  const [result] = useQuery<ChildLearningPathResponse>({
    query: CHILD_LEARNING_PATH,
    variables: { childId: currentChildId },
    pause: !currentChildId,
  });

  return {
    nodes: result.data?.childLearningPath ?? [],
    loading: result.fetching,
    error: result.error,
  };
}
