// useChildCoursePlanDetail：获取子女课程计划详情（含章节）
// 依据：02-architecture-design.md §4.2 GraphQL 接入
// 对标 /parent/course-plans/[id] 页面（家长只读视角）

"use client";

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

interface ChildCoursePlanDetailResponse {
  childCoursePlanDetail: CoursePlanDetail;
}

export function useChildCoursePlanDetail(planId: string): {
  coursePlan: CoursePlanDetail | null;
  loading: boolean;
  error: CombinedError | undefined;
} {
  const currentChildId = useChildStore((s) => s.currentChildId);

  const [result] = useQuery<ChildCoursePlanDetailResponse>({
    query: CHILD_COURSE_PLAN_DETAIL,
    variables: { childId: currentChildId, planId },
    pause: !currentChildId || !planId,
  });

  return {
    coursePlan: result.data?.childCoursePlanDetail ?? null,
    loading: result.fetching,
    error: result.error,
  };
}
