import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { settingsApi } from '@/api/settings'
import type { BoostBotSettings } from '@/types/settings'

/**
 * Hook for fetching settings, optionally scoped to a language.
 */
export function useSettings(language?: string) {
  return useQuery({
    queryKey: ['settings', language ?? '__global__'],
    queryFn: () => settingsApi.getSettings(language),
  })
}

/**
 * Hook for updating settings, optionally scoped to a language.
 */
export function useUpdateSettings(language?: string) {
  const queryClient = useQueryClient()
  
  return useMutation({
    mutationFn: (data: Partial<BoostBotSettings>) => settingsApi.updateSettings(data, language),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['settings'] })
    },
  })
}

/**
 * Hook for resetting settings
 */
export function useResetSettings() {
  const queryClient = useQueryClient()
  
  return useMutation({
    mutationFn: () => settingsApi.resetSettings(),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['settings'] })
    },
  })
}

// =========================================================================
// BYOK FUNCTIONALITY - FOR FUTURE DELETION
// The following hooks were used for Bring Your Own Key (BYOK) model.
// Now all AI calls go through the credits backend (Week 26+).
// =========================================================================

/*
// BYOK FUNCTIONALITY - FOR FUTURE DELETION
export function useTestApiKey() {
  return useMutation({
    mutationFn: (apiKey: string) => settingsApi.testApiKey(apiKey),
  })
}

// BYOK FUNCTIONALITY - FOR FUTURE DELETION
export function useTestOpenAiKey() {
  return useMutation({
    mutationFn: (apiKey: string) => settingsApi.testOpenAiKey(apiKey),
  })
}

// BYOK FUNCTIONALITY - FOR FUTURE DELETION
export function useClearApiKey() {
  const queryClient = useQueryClient()
  
  return useMutation({
    mutationFn: () => settingsApi.clearApiKey(),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['settings'] })
    },
  })
}

// BYOK FUNCTIONALITY - FOR FUTURE DELETION
export function useClearOpenAiKey() {
  const queryClient = useQueryClient()
  
  return useMutation({
    mutationFn: () => settingsApi.clearOpenAiKey(),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['settings'] })
    },
  })
}
*/
// END BYOK FUNCTIONALITY

