import { create } from 'zustand';
import type { AnalysisTemplateSets, DataSourcesConfig, LlmProvidersConfig } from '@/types';

// 根据设计文档定义配置项的类型（兼容 Legacy / 新结构）
export interface DatabaseConfig {
  url?: string | null;
}

export interface NewApiConfig {
  api_key?: string | null;
  base_url?: string | null;
  provider_id?: string | null;
  provider_name?: string | null;
  model_count?: number;
}

export interface SystemConfig {
  database?: DatabaseConfig | null;
  new_api?: NewApiConfig | null;
  data_sources?: DataSourcesConfig;
  llm_providers?: LlmProvidersConfig;
  analysis_template_sets?: AnalysisTemplateSets;
}

interface ConfigState {
  config: SystemConfig | null;
  loading: boolean;
  error: string | null;
  setConfig: (config: SystemConfig) => void;
  setLoading: (loading: boolean) => void;
  setError: (error: string | null) => void;
}

export const useConfigStore = create<ConfigState>((set) => ({
  config: null,
  loading: true,
  error: null,
  setConfig: (config) => set({ config, loading: false, error: null }),
  setLoading: (loading) => set({ loading }),
  setError: (error) => set({ error, loading: false }),
}));
