/**
 * 学生管理 Hooks
 *
 * contract §2.4：adminStudents
 */
import { useCallback, useState } from "react";
import { useGraphQuery } from "./use-graphql";
import { ADMIN_STUDENTS_QUERY } from "@/lib/graphql-client";
import type {
  AdminStudentViewModel,
  PaginatedResult,
  ListFilter,
} from "@/types/view-models";

interface AdminStudentsResponse {
  adminStudents: PaginatedResult<AdminStudentViewModel>;
}

export function useStudents(filter: Partial<ListFilter>) {
  const result = useGraphQuery<AdminStudentsResponse>(ADMIN_STUDENTS_QUERY, {
    filter,
  });
  return {
    ...result,
    data: result.data?.adminStudents ?? null,
  };
}

export function useStudentFilter(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 };
}
