import React from 'react'
import {useQuery} from '@tanstack/react-query'

import useApiUrlStore from '../app/store/apiStore'
import {BotClient, BotStatus, type TradingBotResponse} from '../generated/ManagingApi'

type UseBotsProps = {
    status?: BotStatus
    callback?: (data: TradingBotResponse[]) => void | undefined
}

const useBots = ({status = BotStatus.Saved, callback}: UseBotsProps) => {
    const {apiUrl} = useApiUrlStore()
    const botClient = new BotClient({}, apiUrl)

    const query = useQuery({
        queryFn: () => botClient.bot_GetBotsByStatus(status),
        queryKey: ['bots', status],
        refetchInterval: 5000, // Refetch every 5 seconds for real-time updates
    })

    // Call callback when data changes
    React.useEffect(() => {
        if (query.data && callback) {
            callback(query.data)
        }
    }, [query.data, callback])

    return query
}

export default useBots 