import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { LlmProvidersConfig, DataSourcesConfig, AnalysisTemplateSets, DataSourceProvider } from '../types/config';

// MOCK DATA
const MOCK_PROVIDERS: LlmProvidersConfig = {
  "openai_official": {
    name: "OpenAI Official",
    api_base_url: "https://api.openai.com/v1",
    api_key: "sk-****************",
    models: [
      { model_id: "gpt-4o", name: "GPT-4o", is_active: true },
      { model_id: "gpt-3.5-turbo", name: "GPT-3.5 Turbo", is_active: true }
    ]
  },
  "local_ollama": {
    name: "Local Ollama",
    api_base_url: "http://localhost:11434/v1",
    api_key: "ollama",
    models: [
      { model_id: "llama3", name: "Llama 3", is_active: true }
    ]
  }
};

const MOCK_DATA_SOURCES: DataSourcesConfig = {
  "tushare_main": {
    provider: DataSourceProvider.Tushare,
    api_key: "89******************************",
    enabled: true
  },
  "finnhub_backup": {
    provider: DataSourceProvider.Finnhub,
    api_key: "c1****************",
    enabled: false
  }
};

const MOCK_TEMPLATES: AnalysisTemplateSets = {
  "quick_scan": {
    name: "快速扫描 (Quick Scan)",
    modules: {
      "summary": {
        name: "概览",
        provider_id: "openai_official",
        model_id: "gpt-4o",
        prompt_template: "Analyze this...",
        dependencies: []
      }
    }
  }
};

// --- Hooks ---

export function useLlmProviders() {
  return useQuery({
    queryKey: ['llm-providers'],
    queryFn: async () => {
      // TODO: Replace with actual API call
      // const { data } = await apiClient.get<LlmProvidersConfig>('/configs/llm_providers');
      // return data;
      await new Promise(resolve => setTimeout(resolve, 500)); // Simulate delay
      return MOCK_PROVIDERS;
    }
  });
}

export function useDataSources() {
  return useQuery({
    queryKey: ['data-sources'],
    queryFn: async () => {
      await new Promise(resolve => setTimeout(resolve, 500));
      return MOCK_DATA_SOURCES;
    }
  });
}

export function useAnalysisTemplates() {
  return useQuery({
    queryKey: ['analysis-templates'],
    queryFn: async () => {
      await new Promise(resolve => setTimeout(resolve, 500));
      return MOCK_TEMPLATES;
    }
  });
}

