/**
 * 系统设置 Hooks
 *
 * contract §2.4：systemSettings / updateSystemSettings
 */
import { useCallback } from "react";
import { useGraphMutation, useGraphQuery } from "./use-graphql";
import {
  SYSTEM_SETTINGS_QUERY,
  UPDATE_SYSTEM_SETTINGS_MUTATION,
} from "@/lib/graphql-client";
import type { SystemSettingsViewModel } from "@/types/view-models";

interface SystemSettingsResponse {
  systemSettings: SystemSettingsViewModel;
}

interface UpdateSystemSettingsResponse {
  updateSystemSettings: {
    schoolName: string;
    schoolCode: string;
    academicYear: string;
    semester: string;
  };
}

export function useSystemSettings() {
  const result = useGraphQuery<SystemSettingsResponse>(SYSTEM_SETTINGS_QUERY);
  return {
    ...result,
    data: result.data?.systemSettings ?? null,
  };
}

export function useUpdateSystemSettings() {
  const [run, state] = useGraphMutation<
    UpdateSystemSettingsResponse,
    { input: Partial<SystemSettingsViewModel> }
  >(UPDATE_SYSTEM_SETTINGS_MUTATION);
  const update = useCallback(
    async (input: Partial<SystemSettingsViewModel>) => {
      const res = await run({ input });
      return res.data?.updateSystemSettings ?? null;
    },
    [run],
  );
  return [update, state] as const;
}
