/**
 * 班级管理 Hooks
 *
 * contract §2.4：adminClasses
 */
import { useCallback, useState } from "react";
import { useGraphQuery } from "./use-graphql";
import { ADMIN_CLASSES_QUERY } from "@/lib/graphql-client";
import type {
  AdminClassViewModel,
  PaginatedResult,
  ListFilter,
} from "@/types/view-models";

interface AdminClassesResponse {
  adminClasses: PaginatedResult<AdminClassViewModel>;
}

export function useClasses(filter: Partial<ListFilter>) {
  const result = useGraphQuery<AdminClassesResponse>(ADMIN_CLASSES_QUERY, {
    filter,
  });
  return {
    ...result,
    data: result.data?.adminClasses ?? null,
  };
}

export function useClassFilter(initial?: Partial<ListFilter>) {
  const [filter, setFilter] = useState<Partial<ListFilter>>({
    page: 1,
    pageSize: 20,
    ...initial,
  });
  const setPage = useCallback(
    (page: number) => setFilter((f) => ({ ...f, page })),
    [],
  );
  const setSearch = useCallback(
    (search: string) => setFilter((f) => ({ ...f, search, page: 1 })),
    [],
  );
  return { filter, setFilter, setPage, setSearch };
}
