// useChildElective：获取子女选修课列表
// 依据：02-architecture-design.md §4.2 GraphQL 接入
// 对标 /parent/elective 页面（家长只读视角）

"use client";

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

interface ChildElectiveResponse {
  childElective: ElectiveSelection[];
}

export function useChildElective(): {
  electiveSelections: ElectiveSelection[];
  loading: boolean;
  error: CombinedError | undefined;
} {
  const currentChildId = useChildStore((s) => s.currentChildId);

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

  return {
    electiveSelections: result.data?.childElective ?? [],
    loading: result.fetching,
    error: result.error,
  };
}
