import { create } from 'zustand';
import client from '@/api/client';

export interface OciConfig {
  id: string;
  tenancy_name: string;
  region: string;
  compartment_id?: string;
  auth_type?: string;
}

export interface GenAiConfig {
  id: string;
  name: string;
  model_id: string;
  model_ocid?: string;
  oci_config_id: string;
  compartment_id?: string;
  genai_region?: string;
  endpoint?: string;
  is_default?: boolean;
}

export interface ModelInfo {
  id: string;
  name: string;
  provider: string;
}

export interface EmbeddingModelInfo {
  name: string;
  dims: number;
}

export interface McpServer {
  id: string;
  name: string;
  description?: string;
  server_type: string;
  command?: string;
  args?: string[];
  url?: string;
  module_path?: string;
  is_active: boolean;
  tools?: McpTool[];
  linked_adb_id?: string;
  created_at?: string;
}

export interface McpTool {
  name: string;
  description?: string;
  input_schema?: Record<string, unknown>;
}

export interface AdbTable {
  id: string;
  table_name: string;
  description?: string;
  is_active: boolean;
}

export interface AdbConfig {
  id: string;
  config_name: string;
  dsn: string;
  username: string;
  table_name?: string;
  use_mtls: boolean;
  is_active: boolean;
  wallet_dir?: string;
  genai_config_id?: string;
  embedding_model_id?: string;
  tables?: AdbTable[];
  created_at?: string;
}

interface AppState {
  // Shared config data
  ociCfg: OciConfig[];
  genaiCfg: GenAiConfig[];
  models: Record<string, ModelInfo>;
  regions: string[];
  ociRegions: Record<string, string[]>;
  embModels: Record<string, EmbeddingModelInfo>;
  mcpSvr: McpServer[];
  adbCfg: AdbConfig[];

  // UI
  sidebarOpen: boolean;
  toggleSidebar: () => void;

  // Load all shared data after auth
  loadData: () => Promise<void>;
}

export const useAppStore = create<AppState>((set) => ({
  ociCfg: [],
  genaiCfg: [],
  models: {},
  regions: [],
  ociRegions: {},
  embModels: {},
  mcpSvr: [],
  adbCfg: [],
  sidebarOpen: true,

  toggleSidebar: () => set((s) => ({ sidebarOpen: !s.sidebarOpen })),

  loadData: async () => {
    try {
      const [ociCfg, genaiCfg, mcpSvr] = await Promise.all([
        client.get('/oci/configs'),
        client.get('/genai/configs'),
        client.get('/mcp/servers'),
      ]) as unknown as [OciConfig[], GenAiConfig[], McpServer[]];
      set({ ociCfg, genaiCfg, mcpSvr });

      try {
        const m = await client.get('/genai/models') as unknown as {
          models: Record<string, ModelInfo>;
          regions: string[];
          oci_regions: Record<string, string[]>;
          embedding_models: Record<string, EmbeddingModelInfo>;
        };
        set({
          models: m.models,
          regions: m.regions,
          ociRegions: m.oci_regions || {},
          embModels: m.embedding_models || {},
        });
      } catch {}

      try {
        const adbCfg = await client.get('/adb/configs') as unknown as AdbConfig[];
        set({ adbCfg });
      } catch {
        set({ adbCfg: [] });
      }
    } catch {}
  },
}));
