import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { usersApi } from '../api/users.api';
import type { PaginationParams } from '@/types/api';

export function useUsers(params: PaginationParams & { keyword?: string }) {
  return useQuery({
    queryKey: ['users', params],
    queryFn: () => usersApi.getList(params),
  });
}

export function useUserDetail(accountSequence: string) {
  return useQuery({
    queryKey: ['users', accountSequence],
    queryFn: () => usersApi.getDetail(accountSequence),
    enabled: !!accountSequence,
  });
}

export function useContributionRecords(accountSequence: string, params: PaginationParams & { sortBy?: string; sortOrder?: 'asc' | 'desc' }) {
  return useQuery({
    queryKey: ['users', accountSequence, 'contributions', params],
    queryFn: () => usersApi.getContributionRecords(accountSequence, params),
    enabled: !!accountSequence,
  });
}

export function useMiningRecords(accountSequence: string, params: PaginationParams) {
  return useQuery({
    queryKey: ['users', accountSequence, 'mining-records', params],
    queryFn: () => usersApi.getMiningRecords(accountSequence, params),
    enabled: !!accountSequence,
  });
}

export function useTradeOrders(accountSequence: string, params: PaginationParams) {
  return useQuery({
    queryKey: ['users', accountSequence, 'orders', params],
    queryFn: () => usersApi.getTradeOrders(accountSequence, params),
    enabled: !!accountSequence,
  });
}

// 从 admin-web 复用的 hooks
export function useReferralTree(
  accountSequence: string,
  direction: 'up' | 'down' | 'both' = 'both',
  depth: number = 1
) {
  return useQuery({
    queryKey: ['users', accountSequence, 'referral-tree', direction, depth],
    queryFn: () => usersApi.getReferralTree(accountSequence, direction, depth),
    enabled: !!accountSequence,
  });
}

export function usePlantingLedger(accountSequence: string, params: PaginationParams) {
  return useQuery({
    queryKey: ['users', accountSequence, 'planting-ledger', params],
    queryFn: () => usersApi.getPlantingLedger(accountSequence, params),
    enabled: !!accountSequence,
  });
}

export function useWalletLedger(accountSequence: string, params: PaginationParams) {
  return useQuery({
    queryKey: ['users', accountSequence, 'wallet-ledger', params],
    queryFn: () => usersApi.getWalletLedger(accountSequence, params),
    enabled: !!accountSequence,
  });
}

export function useBatchMiningRecords(accountSequence: string, params: PaginationParams) {
  return useQuery({
    queryKey: ['users', accountSequence, 'batch-mining-records', params],
    queryFn: () => usersApi.getBatchMiningRecords(accountSequence, params),
    enabled: !!accountSequence,
  });
}

export function useP2pTransfers(params: PaginationParams & { search?: string }) {
  return useQuery({
    queryKey: ['p2p-transfers', params],
    queryFn: () => usersApi.getP2pTransfers(params),
  });
}

// ========== Capability 权限管理 Hooks ==========

export function useCapabilities(accountSequence: string) {
  return useQuery({
    queryKey: ['users', accountSequence, 'capabilities'],
    queryFn: () => usersApi.getCapabilities(accountSequence),
    enabled: !!accountSequence,
  });
}

export function useSetCapability(accountSequence: string) {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (dto: { capability: string; enabled: boolean; reason?: string; expiresAt?: string }) =>
      usersApi.setCapability(accountSequence, dto),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['users', accountSequence, 'capabilities'] });
      queryClient.invalidateQueries({ queryKey: ['users', accountSequence, 'capability-logs'] });
    },
  });
}

export function useCapabilityLogs(accountSequence: string, params: PaginationParams) {
  return useQuery({
    queryKey: ['users', accountSequence, 'capability-logs', params],
    queryFn: () => usersApi.getCapabilityLogs(accountSequence, params),
    enabled: !!accountSequence,
  });
}

// ========== 预种卖出限制 Hooks ==========

export function usePrePlantingRestriction(accountSequence: string) {
  return useQuery({
    queryKey: ['users', accountSequence, 'pre-planting-restriction'],
    queryFn: () => usersApi.getPrePlantingRestriction(accountSequence),
    enabled: !!accountSequence,
  });
}

export function useUnlockPrePlantingRestriction(accountSequence: string) {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (reason?: string) => usersApi.unlockPrePlantingRestriction(accountSequence, reason),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['users', accountSequence, 'pre-planting-restriction'] });
    },
  });
}
