// useChildCoursePlans：获取子女课程计划列表
// 依据：02-architecture-design.md §4.2 GraphQL 接入
// 对标 /parent/course-plans 页面（家长只读视角）

"use client";

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

interface ChildCoursePlansResponse {
  childCoursePlans: CoursePlan[];
}

export function useChildCoursePlans(): {
  coursePlans: CoursePlan[];
  loading: boolean;
  error: CombinedError | undefined;
} {
  const currentChildId = useChildStore((s) => s.currentChildId);

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

  return {
    coursePlans: result.data?.childCoursePlans ?? [],
    loading: result.fetching,
    error: result.error,
  };
}
