import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { LlmProvidersConfig, DataSourcesConfig, TestConfigRequest, TestLlmConfigRequest } from '../types/config';
import { client } from '../api/client';

// --- Hooks ---

export function useLlmProviders() {
  return useQuery({
    queryKey: ['llm-providers'],
    queryFn: async () => {
      return await client.get_llm_providers_config();
    }
  });
}

export function useUpdateLlmProviders() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: async (config: LlmProvidersConfig) => {
      return await client.update_llm_providers_config(config);
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['llm-providers'] });
    }
  });
}

export function useDataSources() {
  return useQuery({
    queryKey: ['data-sources'],
    queryFn: async () => {
      return await client.get_data_sources_config();
    }
  });
}

export function useUpdateDataSources() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: async (config: DataSourcesConfig) => {
      return await client.update_data_sources_config(config);
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['data-sources'] });
    }
  });
}

export function useRegisteredProviders() {
  return useQuery({
    queryKey: ['registered-providers'],
    queryFn: async () => {
      return await client.get_registered_providers();
    }
  });
}

export function useAnalysisTemplates() {
  return useQuery({
    queryKey: ['analysis-templates'],
    queryFn: async () => {
      return await client.get_templates();
    }
  });
}

export function useAnalysisTemplate(id: string | null) {
  return useQuery({
    queryKey: ['analysis-template', id],
    queryFn: async () => {
      if (!id) return null;
      return await client.get_template_by_id({ params: { id } });
    },
    enabled: !!id
  });
}

export function useSaveAnalysisTemplate() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: async ({ id, template }: { id: string, template: AnalysisTemplateSet }) => {
      return await client.update_template(template, { params: { id } });
    },
    onSuccess: (data, variables) => {
      queryClient.invalidateQueries({ queryKey: ['analysis-templates'] });
      queryClient.invalidateQueries({ queryKey: ['analysis-template', variables.id] });
    }
  });
}

export function useDeleteAnalysisTemplate() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: async (id: string) => {
      return await client.delete_template(undefined, { params: { id } });
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['analysis-templates'] });
    }
  });
}

export type AnalysisTemplateSet = import('../api/schema.gen').AnalysisTemplateSet;


export function useDiscoverModels() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: async (providerId: string) => {
      return await client.discover_models({ params: { provider_id: providerId } });
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['llm-providers'] });
    }
  });
}

export function useTestDataSource() {
  return useMutation({
    mutationFn: async (config: TestConfigRequest) => {
      return await client.test_data_source_config(config);
    }
  });
}

export function useTestLlmConfig() {
  return useMutation({
    mutationFn: async (config: TestLlmConfigRequest) => {
      return await client.test_llm_config(config);
    }
  });
}
