/**
 * Settings Store (Zustand)
 * Manages account and module settings
 */
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import {
  fetchAccountSettings,
  fetchAccountSetting,
  createAccountSetting,
  updateAccountSetting,
  deleteAccountSetting,
  fetchModuleSettings,
  createModuleSetting,
  updateModuleSetting,
  AccountSetting,
  ModuleSetting,
} from '../services/api';

interface SettingsState {
  accountSettings: Record<string, AccountSetting>;
  moduleSettings: Record<string, Record<string, ModuleSetting>>;
  loading: boolean;
  error: string | null;
  
  // Actions
  loadAccountSettings: () => Promise<void>;
  loadAccountSetting: (key: string) => Promise<void>;
  updateAccountSetting: (key: string, value: any) => Promise<void>;
  loadModuleSettings: (moduleName: string) => Promise<void>;
  updateModuleSetting: (moduleName: string, key: string, value: any) => Promise<void>;
  reset: () => void;
}

export const useSettingsStore = create<SettingsState>()(
  persist<SettingsState>(
    (set, get) => ({
      accountSettings: {},
      moduleSettings: {},
      loading: false,
      error: null,
      
      loadAccountSettings: async () => {
        set({ loading: true, error: null });
        try {
          const response = await fetchAccountSettings();
          const settingsMap: Record<string, AccountSetting> = {};
          response.results.forEach(setting => {
            settingsMap[setting.key] = setting;
          });
          set({ accountSettings: settingsMap, loading: false });
        } catch (error: any) {
          set({ error: error.message, loading: false });
        }
      },
      
      loadAccountSetting: async (key: string) => {
        try {
          const setting = await fetchAccountSetting(key);
          set(state => ({
            accountSettings: { ...state.accountSettings, [key]: setting }
          }));
        } catch (error: any) {
          set({ error: error.message });
        }
      },
      
      updateAccountSetting: async (key: string, value: any) => {
        set({ loading: true, error: null });
        try {
          const existing = get().accountSettings[key];
          let setting: AccountSetting;
          
          if (existing) {
            setting = await updateAccountSetting(key, { config: value });
          } else {
            setting = await createAccountSetting({ key, config: value });
          }
          
          set(state => ({
            accountSettings: { ...state.accountSettings, [key]: setting },
            loading: false
          }));
        } catch (error: any) {
          set({ error: error.message, loading: false });
          throw error;
        }
      },
      
      loadModuleSettings: async (moduleName: string) => {
        set({ loading: true, error: null });
        try {
          const settings = await fetchModuleSettings(moduleName);
          const settingsMap: Record<string, ModuleSetting> = {};
          settings.forEach(setting => {
            settingsMap[setting.key] = setting;
          });
          set(state => ({
            moduleSettings: {
              ...state.moduleSettings,
              [moduleName]: settingsMap
            },
            loading: false
          }));
        } catch (error: any) {
          set({ error: error.message, loading: false });
        }
      },
      
      updateModuleSetting: async (moduleName: string, key: string, value: any) => {
        set({ loading: true, error: null });
        try {
          const existing = get().moduleSettings[moduleName]?.[key];
          let setting: ModuleSetting;
          
          if (existing) {
            setting = await updateModuleSetting(moduleName, key, { config: value });
          } else {
            setting = await createModuleSetting({ module_name: moduleName, key, config: value });
          }
          
          set(state => ({
            moduleSettings: {
              ...state.moduleSettings,
              [moduleName]: {
                ...(state.moduleSettings[moduleName] || {}),
                [key]: setting
              }
            },
            loading: false
          }));
        } catch (error: any) {
          set({ error: error.message, loading: false });
          throw error;
        }
      },
      
      reset: () => {
        set({
          accountSettings: {},
          moduleSettings: {},
          loading: false,
          error: null,
        });
      },
    }),
    {
      name: 'settings-storage',
      partialize: (state) => ({
        accountSettings: state.accountSettings,
        moduleSettings: state.moduleSettings,
      }),
    }
  )
);

