// useChildClasses：获取子女所在班级列表
// 依据：02-architecture-design.md §4.2 GraphQL 接入
// 对标 student-portal /my-classes 页面（家长只读视角）

"use client";

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

interface ChildClassesResponse {
  childClasses: ClassInfo[];
}

export function useChildClasses(): {
  classes: ClassInfo[];
  loading: boolean;
  error: CombinedError | undefined;
} {
  const currentChildId = useChildStore((s) => s.currentChildId);

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

  return {
    classes: result.data?.childClasses ?? [],
    loading: result.fetching,
    error: result.error,
  };
}
