import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { tradingApi } from '../api/trading.api';
import type { AdminOrdersParams, AdminTradesParams } from '../api/trading.api';
import { useToast } from '@/lib/hooks/use-toast';

export function useTradingStatus() {
  return useQuery({
    queryKey: ['trading', 'status'],
    queryFn: () => tradingApi.getTradingStatus(),
    refetchInterval: 30000,
  });
}

export function useActivateTrading() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: () => tradingApi.activateTrading(),
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: ['trading'] });
      toast({ title: data.message || '交易系统已激活', variant: 'success' as any });
    },
    onError: (error: any) => {
      toast({
        title: '激活失败',
        description: error?.response?.data?.message || '请稍后重试',
        variant: 'destructive',
      });
    },
  });
}

export function useDeactivateTrading() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: () => tradingApi.deactivateTrading(),
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: ['trading'] });
      toast({ title: data.message || '交易系统已关闭', variant: 'success' as any });
    },
    onError: (error: any) => {
      toast({
        title: '关闭失败',
        description: error?.response?.data?.message || '请稍后重试',
        variant: 'destructive',
      });
    },
  });
}

export function useBuyEnabled() {
  return useQuery({
    queryKey: ['trading', 'buy-enabled'],
    queryFn: () => tradingApi.getBuyEnabled(),
    refetchInterval: 30000,
  });
}

export function useSetBuyEnabled() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: (enabled: boolean) => tradingApi.setBuyEnabled(enabled),
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: ['trading'] });
      toast({ title: data.message || (data.enabled ? '买入功能已开启' : '买入功能已关闭'), variant: 'success' as any });
    },
    onError: (error: any) => {
      toast({
        title: '设置失败',
        description: error?.response?.data?.message || '请稍后重试',
        variant: 'destructive',
      });
    },
  });
}

export function useBurnStatus() {
  return useQuery({
    queryKey: ['trading', 'burn-status'],
    queryFn: () => tradingApi.getBurnStatus(),
    refetchInterval: 60000,
  });
}

export function useBurnRecords(
  page: number = 1,
  pageSize: number = 20,
  sourceType?: 'MINUTE_BURN' | 'SELL_BURN'
) {
  return useQuery({
    queryKey: ['trading', 'burn-records', page, pageSize, sourceType],
    queryFn: () => tradingApi.getBurnRecords(page, pageSize, sourceType),
  });
}

export function useAdminOrders(params: AdminOrdersParams) {
  return useQuery({
    queryKey: ['trading', 'admin-orders', params],
    queryFn: () => tradingApi.getAdminOrders(params),
  });
}

export function useAdminTrades(params: AdminTradesParams) {
  return useQuery({
    queryKey: ['trading', 'admin-trades', params],
    queryFn: () => tradingApi.getAdminTrades(params),
  });
}

export function useMarketOverview() {
  return useQuery({
    queryKey: ['trading', 'market-overview'],
    queryFn: () => tradingApi.getMarketOverview(),
    refetchInterval: 60000,
  });
}
