/**
 * Settings Store
 *
 * Zustand store for managing plugin settings state.
 * Handles Google fonts local loading and AI provider configuration.
 */

import { create } from "zustand";

/**
 * AI Provider types
 */
export type AIProvider = "none" | "openai" | "gemini" | "anthropic";

/**
 * AI Model options per provider
 */
export const AI_MODELS: Record<Exclude<AIProvider, "none">, { value: string; label: string }[]> = {
  openai: [
    { value: "gpt-5.5", label: "GPT-5.5 (Latest)" },
    { value: "gpt-5", label: "GPT-5" },
    { value: "gpt-4.1", label: "GPT-4.1" },
  ],
  gemini: [  
    { value: "gemini-3.6-flash", label: "Gemini 3.6 Flash (Recommended)" },
    { value: "gemini-3.5-flash", label: "Gemini 3.5 Flash" },
    { value: "gemini-3.1-pro-preview", label: "Gemini 3.1 Pro" },
    { value: "gemini-3-pro-preview", label: "Gemini 3 Pro" },
  ],
  anthropic: [
    { value: "claude-opus-4-5-20251101", label: "Claude Opus 4.5 (Recommended)" },
    { value: "claude-sonnet-5-20260509", label: "Claude Sonnet 5 (Latest)" },
    { value: "claude-sonnet-4-5-20250929", label: "Claude Sonnet 4.5" },
    
  ],
};

/**
 * Font file info with weight and style
 */
export interface FontFileInfo {
  file: string;
  weight: string;
  style: string;
}

/**
 * Cached font info
 */
export interface FontCacheEntry {
  name: string;
  slug: string;
  files: FontFileInfo[];
  css: string;
}

/**
 * Settings state interface
 */
export interface SettingsState {
  // Google Fonts
  googleFontsLocal: boolean;
  googleFontsCdn: boolean;
  googleFontsDisabled: boolean;
  googleFontsCache: Record<string, FontCacheEntry>;

  // AI Configuration
  aiProvider: AIProvider;
  aiApiKey: string;
  aiApiKeyDraft: string;
  aiApiKeyClearRequested: boolean;
  aiModel: string;
  hasApiKey: boolean;

  // Other settings
  defaultScenario: string;

  // Loading states
  isLoading: boolean;
  isSaving: boolean;
  isUpdatingFonts: boolean;

  // Actions
  setSettings: (settings: Partial<SettingsState>) => void;
  setGoogleFontsLocal: (enabled: boolean) => void;
  setGoogleFontsCdn: (enabled: boolean) => void;
  setGoogleFontsDisabled: (disabled: boolean) => void;
  setAiProvider: (provider: AIProvider) => void;
  setAiApiKey: (key: string) => void;
  setAiApiKeyDraft: (key: string) => void;
  setAiApiKeyClearRequested: (clear: boolean) => void;
  setAiModel: (model: string) => void;
  setIsLoading: (loading: boolean) => void;
  setIsSaving: (saving: boolean) => void;
  setIsUpdatingFonts: (updating: boolean) => void;
  updateFontsCache: (cache: Record<string, FontCacheEntry>) => void;
}

/**
 * Create the settings store
 */
export const useSettingsStore = create<SettingsState>((set) => ({
  // Initial state
  googleFontsLocal: true,
  googleFontsCdn: false,
  googleFontsDisabled: false,
  googleFontsCache: {},
  aiProvider: "none",
  aiApiKey: "",
  aiApiKeyDraft: "",
  aiApiKeyClearRequested: false,
  aiModel: "",
  hasApiKey: false,
  defaultScenario: "simple",
  isLoading: false,
  isSaving: false,
  isUpdatingFonts: false,

  // Actions
  setSettings: (settings) =>
    set((state) => ({
      ...state,
      ...(settings.googleFontsLocal !== undefined && { googleFontsLocal: settings.googleFontsLocal }),
      ...(settings.googleFontsCdn !== undefined && { googleFontsCdn: settings.googleFontsCdn }),
      ...(settings.googleFontsDisabled !== undefined && { googleFontsDisabled: settings.googleFontsDisabled }),
      ...(settings.googleFontsCache !== undefined && { googleFontsCache: settings.googleFontsCache }),
      ...(settings.aiProvider !== undefined && { aiProvider: settings.aiProvider }),
      ...(settings.aiApiKey !== undefined && { aiApiKey: settings.aiApiKey }),
      ...(settings.aiApiKeyDraft !== undefined && { aiApiKeyDraft: settings.aiApiKeyDraft }),
      ...(settings.aiApiKeyClearRequested !== undefined && { aiApiKeyClearRequested: settings.aiApiKeyClearRequested }),
      ...(settings.aiModel !== undefined && { aiModel: settings.aiModel }),
      ...(settings.hasApiKey !== undefined && { hasApiKey: settings.hasApiKey }),
      ...(settings.defaultScenario !== undefined && { defaultScenario: settings.defaultScenario }),
    })),

  setGoogleFontsLocal: (enabled) =>
    set((state) => ({
      googleFontsLocal: enabled,
      googleFontsCdn: enabled ? false : state.googleFontsCdn,
    })),

  setGoogleFontsCdn: (enabled) =>
    set((state) => ({
      googleFontsCdn: enabled,
      googleFontsLocal: enabled ? false : state.googleFontsLocal,
    })),

  setGoogleFontsDisabled: (disabled) => set({ googleFontsDisabled: disabled }),

  setAiProvider: (provider) =>
    set((state) => ({
      aiProvider: provider,
      aiModel: provider !== "none" ? (AI_MODELS[provider]?.[0]?.value ?? "") : "",
    })),

  setAiApiKey: (key) => set({ aiApiKey: key }),

  setAiApiKeyDraft: (key) =>
    set((state) => ({
      aiApiKeyDraft: key,
      aiApiKeyClearRequested: key !== "" ? false : state.aiApiKeyClearRequested,
    })),

  setAiApiKeyClearRequested: (clear) => set({ aiApiKeyClearRequested: clear }),

  setAiModel: (model) => set({ aiModel: model }),

  setIsLoading: (loading) => set({ isLoading: loading }),

  setIsSaving: (saving) => set({ isSaving: saving }),

  setIsUpdatingFonts: (updating) => set({ isUpdatingFonts: updating }),

  updateFontsCache: (cache) =>
    set((state) => ({
      googleFontsCache: { ...state.googleFontsCache, ...cache },
    })),
}));

/**
 * Selector: Check if AI is configured
 */
export const useIsAiConfigured = () => useSettingsStore((state) => Boolean(state.aiProvider && state.hasApiKey && state.aiModel));

/**
 * Selector: Get AI provider display name
 */
export const useAiProviderName = () =>
  useSettingsStore((state) => {
    const names: Record<AIProvider, string> = {
      none: "Not configured",
      openai: "OpenAI",
      gemini: "Google Gemini",
      anthropic: "Anthropic",
    };
    return names[state.aiProvider] || state.aiProvider;
  });
