/**
 * 用户详情页面 Hooks
 */

import { useQuery } from '@tanstack/react-query';
import { userDetailService } from '@/services/userDetailService';
import type {
  LedgerQueryParams,
  WalletLedgerQueryParams,
} from '@/types/userDetail.types';

// Query Keys
export const userDetailKeys = {
  all: ['userDetail'] as const,
  fullDetail: (accountSequence: string) => [...userDetailKeys.all, 'fullDetail', accountSequence] as const,
  referralTree: (accountSequence: string, direction: string, depth: number) =>
    [...userDetailKeys.all, 'referralTree', accountSequence, direction, depth] as const,
  plantingLedger: (accountSequence: string, params: LedgerQueryParams) =>
    [...userDetailKeys.all, 'plantingLedger', accountSequence, params] as const,
  walletLedger: (accountSequence: string, params: WalletLedgerQueryParams) =>
    [...userDetailKeys.all, 'walletLedger', accountSequence, params] as const,
  authorizationDetail: (accountSequence: string) =>
    [...userDetailKeys.all, 'authorizationDetail', accountSequence] as const,
};

/**
 * 获取用户完整信息
 */
export function useUserFullDetail(accountSequence: string) {
  return useQuery({
    queryKey: userDetailKeys.fullDetail(accountSequence),
    queryFn: () => userDetailService.getFullDetail(accountSequence),
    enabled: !!accountSequence,
    staleTime: 60 * 1000, // 1分钟
    gcTime: 5 * 60 * 1000, // 5分钟
  });
}

/**
 * 获取推荐关系树
 */
export function useReferralTree(
  accountSequence: string,
  direction: 'up' | 'down' | 'both' = 'both',
  depth: number = 1
) {
  return useQuery({
    queryKey: userDetailKeys.referralTree(accountSequence, direction, depth),
    queryFn: () => userDetailService.getReferralTree(accountSequence, direction, depth),
    enabled: !!accountSequence,
    staleTime: 60 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}

/**
 * 获取认种分类账
 */
export function usePlantingLedger(
  accountSequence: string,
  params: LedgerQueryParams = {}
) {
  return useQuery({
    queryKey: userDetailKeys.plantingLedger(accountSequence, params),
    queryFn: () => userDetailService.getPlantingLedger(accountSequence, params),
    enabled: !!accountSequence,
    staleTime: 30 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}

/**
 * 获取钱包分类账
 */
export function useWalletLedger(
  accountSequence: string,
  params: WalletLedgerQueryParams = {}
) {
  return useQuery({
    queryKey: userDetailKeys.walletLedger(accountSequence, params),
    queryFn: () => userDetailService.getWalletLedger(accountSequence, params),
    enabled: !!accountSequence,
    staleTime: 30 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}

/**
 * 获取授权信息
 */
export function useAuthorizationDetail(accountSequence: string) {
  return useQuery({
    queryKey: userDetailKeys.authorizationDetail(accountSequence),
    queryFn: () => userDetailService.getAuthorizationDetail(accountSequence),
    enabled: !!accountSequence,
    staleTime: 60 * 1000,
    gcTime: 5 * 60 * 1000,
  });
}
