/**
 * 教师管理 Hooks
 *
 * contract §2.4：adminTeachers
 */
import { useCallback, useState } from "react";
import { useGraphQuery } from "./use-graphql";
import { ADMIN_TEACHERS_QUERY } from "@/lib/graphql-client";
import type {
  AdminTeacherViewModel,
  PaginatedResult,
  ListFilter,
} from "@/types/view-models";

interface AdminTeachersResponse {
  adminTeachers: PaginatedResult<AdminTeacherViewModel>;
}

export function useTeachers(filter: Partial<ListFilter>) {
  const result = useGraphQuery<AdminTeachersResponse>(ADMIN_TEACHERS_QUERY, {
    filter,
  });
  return {
    ...result,
    data: result.data?.adminTeachers ?? null,
  };
}

export function useTeacherFilter(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 })),
    [],
  );
  const setStatus = useCallback(
    (status: string) => setFilter((f) => ({ ...f, status, page: 1 })),
    [],
  );
  return { filter, setFilter, setPage, setSearch, setStatus };
}
