/**
 * Skill 状态管理 (Zustand)
 */
import { create } from 'zustand'
import { skillService, Skill } from '@/services/skillService'

interface SkillStore {
  skills: Skill[]
  builtinSkills: Skill[]
  userSkills: Skill[]
  loading: boolean
  error: string | null

  // Actions
  fetchSkills: () => Promise<void>
  testSkill: (skillId: string, input: string, context?: any) => Promise<string>
  createSkill: (data: { id: string; name: string; content: string; category?: string }) => Promise<Skill>
  updateSkill: (skillId: string, data: { name?: string; content?: string }) => Promise<Skill>
  deleteSkill: (skillId: string) => Promise<void>
}

export const useSkillStore = create<SkillStore>((set, get) => ({
  skills: [],
  builtinSkills: [],
  userSkills: [],
  loading: false,
  error: null,

  fetchSkills: async () => {
    set({ loading: true, error: null })
    try {
      const skills = await skillService.listSkills()
      const builtinSkills = skills.filter(s => s.type === 'builtin')
      const userSkills = skills.filter(s => s.type === 'user')
      set({ skills, builtinSkills, userSkills, loading: false })
    } catch (error) {
      set({ error: (error as Error).message, loading: false })
    }
  },

  testSkill: async (skillId, input, context) => {
    set({ loading: true, error: null })
    try {
      const result = await skillService.testSkill(skillId, {
        test_input: input,
        context,
        temperature: 0.7
      })
      set({ loading: false })
      return result.response
    } catch (error) {
      set({ error: (error as Error).message, loading: false })
      throw error
    }
  },

  createSkill: async (data) => {
    set({ loading: true, error: null })
    try {
      const newSkill = await skillService.createSkill(data)
      const { skills, userSkills } = get()
      set({
        skills: [...skills, newSkill],
        userSkills: [...userSkills, newSkill],
        loading: false
      })
      return newSkill
    } catch (error) {
      set({ error: (error as Error).message, loading: false })
      throw error
    }
  },

  updateSkill: async (skillId, data) => {
    set({ loading: true, error: null })
    try {
      const updatedSkill = await skillService.updateSkill(skillId, data)
      const { skills, userSkills } = get()
      set({
        skills: skills.map(s => s.id === skillId ? updatedSkill : s),
        userSkills: userSkills.map(s => s.id === skillId ? updatedSkill : s),
        loading: false
      })
      return updatedSkill
    } catch (error) {
      set({ error: (error as Error).message, loading: false })
      throw error
    }
  },

  deleteSkill: async (skillId) => {
    set({ loading: true, error: null })
    try {
      await skillService.deleteSkill(skillId)
      const { skills, userSkills } = get()
      set({
        skills: skills.filter(s => s.id !== skillId),
        userSkills: userSkills.filter(s => s.id !== skillId),
        loading: false
      })
    } catch (error) {
      set({ error: (error as Error).message, loading: false })
      throw error
    }
  }
}))
