/**
 * [2026-02-17] 预种计划管理 React Query Hooks
 *
 * 为 admin-web 预种管理页面提供数据获取 hooks。
 * 遵循项目的 React Query 模式：
 * - Query key factory（层级化、参数化）
 * - 每个查询对应一个 hook
 * - useMutation 用于开关切换等写操作
 *
 * === 使用方式 ===
 * import { usePrePlantingConfig, usePrePlantingStats, ... } from '@/hooks';
 */

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

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

export const prePlantingKeys = {
  all: ['pre-planting'] as const,
  config: () => [...prePlantingKeys.all, 'config'] as const,
  stats: () => [...prePlantingKeys.all, 'stats'] as const,
  orders: (params: PrePlantingListParams) =>
    [...prePlantingKeys.all, 'orders', params] as const,
  positions: (params: PrePlantingListParams) =>
    [...prePlantingKeys.all, 'positions', params] as const,
  merges: (params: PrePlantingListParams) =>
    [...prePlantingKeys.all, 'merges', params] as const,
};

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

/** 获取预种开关配置 */
export function usePrePlantingConfig() {
  return useQuery({
    queryKey: prePlantingKeys.config(),
    queryFn: () => prePlantingService.getConfig(),
    staleTime: 30 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}

/** 获取预种统计汇总 */
export function usePrePlantingStats() {
  return useQuery({
    queryKey: prePlantingKeys.stats(),
    queryFn: () => prePlantingService.getStats(),
    staleTime: 30 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}

/** 获取预种订单列表 */
export function usePrePlantingOrders(params: PrePlantingListParams = {}) {
  return useQuery({
    queryKey: prePlantingKeys.orders(params),
    queryFn: () => prePlantingService.getOrders(params),
    staleTime: 30 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}

/** 获取预种持仓列表 */
export function usePrePlantingPositions(params: PrePlantingListParams = {}) {
  return useQuery({
    queryKey: prePlantingKeys.positions(params),
    queryFn: () => prePlantingService.getPositions(params),
    staleTime: 30 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}

/** 获取合并记录列表 */
export function usePrePlantingMerges(params: PrePlantingListParams = {}) {
  return useQuery({
    queryKey: prePlantingKeys.merges(params),
    queryFn: () => prePlantingService.getMerges(params),
    staleTime: 30 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}

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

/** 切换预种开关 */
export function useTogglePrePlantingConfig() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (isActive: boolean) =>
      prePlantingService.toggleConfig(isActive),
    onSuccess: () => {
      // 开关切换后刷新配置和统计
      queryClient.invalidateQueries({ queryKey: prePlantingKeys.config() });
      queryClient.invalidateQueries({ queryKey: prePlantingKeys.stats() });
    },
  });
}
