import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { AppSettings } from '@/types'

export const useSettingsStore = defineStore('settings', () => {
  const settings = ref<AppSettings>({
    apiKey: '',
    baseUrl: 'https://api.siliconflow.cn/v1',
    model: '',
    shortcut: 'Alt+Q',
    theme: 'light',
    fontSize: 14,
    proxy: '',
    timeout: 120,
    screenshotSavePath: ''
  })

  async function loadSettings() {
    if (window.electronAPI) {
      const result = await window.electronAPI.settings.get()
      if (result) {
        settings.value = { ...settings.value, ...result }
      }
    }
  }

  async function saveSettings(partial: Partial<AppSettings>) {
    Object.assign(settings.value, partial)
    if (window.electronAPI) {
      await window.electronAPI.settings.set(partial)
    }
  }

  return {
    settings,
    loadSettings,
    saveSettings
  }
})
