/**
 * React Query hooks for Bots
 * @since 1.1.0
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { botsApi } from '@/api/bots'
import { useLanguage } from '@/contexts/LanguageContext'
import type { BotFormData } from '@/types/bot'

/**
 * Get all bots (filtered by current admin language)
 */
export function useBots(page = 1, perPage = 50) {
    const { currentLanguage } = useLanguage()
    return useQuery({
        queryKey: ['bots', page, perPage, currentLanguage],
        queryFn: () => botsApi.getBots(page, perPage, currentLanguage),
    })
}

/**
 * Get a single bot
 */
export function useBot(id: number, options: { enabled?: boolean } = {}) {
    return useQuery({
        queryKey: ['bot', id],
        queryFn: () => botsApi.getBot(id),
        enabled: options.enabled !== false && id > 0,
    })
}

/**
 * Get bot statistics
 */
export function useBotStats(options: { enabled?: boolean } = {}) {
    return useQuery({
        queryKey: ['bots', 'stats'],
        queryFn: () => botsApi.getBotStats(),
        enabled: options.enabled !== false,
    })
}

/**
 * Create a new bot
 */
export function useCreateBot() {
    const queryClient = useQueryClient()

    return useMutation({
        mutationFn: (data: Partial<BotFormData>) => botsApi.createBot(data),
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['bots'] })
        },
    })
}

/**
 * Update an existing bot
 */
export function useUpdateBot() {
    const queryClient = useQueryClient()

    return useMutation({
        mutationFn: ({ id, data }: { id: number; data: Partial<BotFormData> }) =>
            botsApi.updateBot(id, data),
        onSuccess: (_, { id }) => {
            queryClient.invalidateQueries({ queryKey: ['bots'] })
            queryClient.invalidateQueries({ queryKey: ['bot', id] })
        },
    })
}

/**
 * Delete a bot
 */
export function useDeleteBot() {
    const queryClient = useQueryClient()

    return useMutation({
        mutationFn: (id: number) => botsApi.deleteBot(id),
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['bots'] })
        },
    })
}

/**
 * Duplicate a bot
 */
export function useDuplicateBot() {
    const queryClient = useQueryClient()

    return useMutation({
        mutationFn: (id: number) => botsApi.duplicateBot(id),
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['bots'] })
        },
    })
}

/**
 * Set a bot as default
 */
export function useSetDefaultBot() {
    const queryClient = useQueryClient()

    return useMutation({
        mutationFn: (id: number) => botsApi.setDefaultBot(id),
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['bots'] })
        },
    })
}

/**
 * Get bots that override a given bot due to more specific display conditions
 */
export function useOverridingBots(id: number, options: { enabled?: boolean } = {}) {
    return useQuery({
        queryKey: ['bots', id, 'overriding'],
        queryFn: () => botsApi.getOverridingBots(id),
        enabled: options.enabled !== false && id > 0,
    })
}

/**
 * Get bots that a given bot overrides due to its more specific display conditions
 */
export function useOverriddenBots(id: number, options: { enabled?: boolean } = {}) {
    return useQuery({
        queryKey: ['bots', id, 'overridden'],
        queryFn: () => botsApi.getOverriddenBots(id),
        enabled: options.enabled !== false && id > 0,
    })
}








