// useChildLessonPlans：获取子女备课列表（按学科筛选）
// 依据：02-architecture-design.md §4.2 GraphQL 接入
// 对标 /parent/lesson-plans 页面（家长只读视角）

"use client";

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

interface ChildLessonPlansResponse {
  childLessonPlans: LessonPlan[];
}

export function useChildLessonPlans(subject?: string): {
  lessonPlans: LessonPlan[];
  loading: boolean;
  error: CombinedError | undefined;
} {
  const currentChildId = useChildStore((s) => s.currentChildId);

  const [result] = useQuery<ChildLessonPlansResponse>({
    query: CHILD_LESSON_PLANS,
    variables: { childId: currentChildId, subject: subject || null },
    pause: !currentChildId,
  });

  return {
    lessonPlans: result.data?.childLessonPlans ?? [],
    loading: result.fetching,
    error: result.error,
  };
}
