import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { c2cBotApi } from '../api/c2c-bot.api';
import { toast } from '@/lib/hooks/use-toast';

export function useC2cBotStatus() {
  return useQuery({
    queryKey: ['c2cBot', 'status'],
    queryFn: () => c2cBotApi.getStatus(),
    refetchInterval: 10000,
  });
}

export function useEnableC2cBot() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: () => c2cBotApi.enable(),
    onSuccess: (data) => {
      toast({ title: '成功', description: data.message });
      queryClient.invalidateQueries({ queryKey: ['c2cBot'] });
    },
    onError: (error: any) => {
      toast({ title: '错误', description: error.response?.data?.message || '开启失败', variant: 'destructive' });
    },
  });
}

export function useDisableC2cBot() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: () => c2cBotApi.disable(),
    onSuccess: (data) => {
      toast({ title: '成功', description: data.message });
      queryClient.invalidateQueries({ queryKey: ['c2cBot'] });
    },
    onError: (error: any) => {
      toast({ title: '错误', description: error.response?.data?.message || '关闭失败', variant: 'destructive' });
    },
  });
}

export function useC2cBotOrders(page: number = 1, pageSize: number = 20) {
  return useQuery({
    queryKey: ['c2cBot', 'orders', page, pageSize],
    queryFn: () => c2cBotApi.getOrders(page, pageSize),
    refetchInterval: 10000,
  });
}
