// useChildHomework：获取子女作业列表
// 依据：02-architecture-design.md §4.2 GraphQL 接入

"use client";

import { useQuery } from "urql";
import { CHILD_HOMEWORK } from "@/lib/graphql/operations";
import type { HomeworkItem } from "@/types";
import { useChildStore } from "@/store/child-store";

interface ChildHomeworkResponse {
  childHomework: HomeworkItem[];
}

export function useChildHomework(status?: string) {
  const currentChildId = useChildStore((s) => s.currentChildId);

  const [result] = useQuery<ChildHomeworkResponse>({
    query: CHILD_HOMEWORK,
    variables: { childId: currentChildId, status: status ?? null },
    pause: !currentChildId,
  });

  return {
    homework: result.data?.childHomework ?? [],
    loading: result.fetching,
    error: result.error,
  };
}
