/**
 * [2026-02-19] 树转让管理 React Query Hooks（纯新增）
 *
 * 为 admin-web 转让管理页面提供数据获取 hooks。
 * 复用项目的 React Query 模式（Query key factory + hooks）。
 *
 * === 回滚方式 ===
 * 删除此文件并从 hooks/index.ts 移除 export
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
  transferService,
  type TransferListParams,
} from '@/services/transferService';

// ============================================
// Query Key Factory
// ============================================

export const transferKeys = {
  all: ['transfers'] as const,
  stats: () => [...transferKeys.all, 'stats'] as const,
  list: (params: TransferListParams) =>
    [...transferKeys.all, 'list', params] as const,
  detail: (transferOrderNo: string) =>
    [...transferKeys.all, 'detail', transferOrderNo] as const,
};

// ============================================
// Query Hooks
// ============================================

/** 获取转让统计汇总 */
export function useTransferStats() {
  return useQuery({
    queryKey: transferKeys.stats(),
    queryFn: () => transferService.getStats(),
    staleTime: 30 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}

/** 获取转让订单列表 */
export function useTransferList(params: TransferListParams = {}) {
  return useQuery({
    queryKey: transferKeys.list(params),
    queryFn: () => transferService.getList(params),
    staleTime: 30 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}

/** 获取转让订单详情 */
export function useTransferDetail(transferOrderNo: string) {
  return useQuery({
    queryKey: transferKeys.detail(transferOrderNo),
    queryFn: () => transferService.getDetail(transferOrderNo),
    enabled: !!transferOrderNo,
    staleTime: 30 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}

// ============================================
// Mutation Hooks
// ============================================

/** 强制取消转让订单 */
export function useForceCancel() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ transferOrderNo, reason }: { transferOrderNo: string; reason: string }) =>
      transferService.forceCancel(transferOrderNo, reason),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: transferKeys.all });
    },
  });
}
