import { create } from 'zustand'
import type { ApiEvent } from '../hooks/useNovaEvents'
import type { ExtendedEvent, TaskArtifact } from '../types'

export interface EventStoreState {
  /** 所有事件 */
  events: ApiEvent[]
  /** processEvents */
  processEvents: ExtendedEvent[]
  /** 所有 artifacts（文件和工具调用） */
  artifacts: TaskArtifact[]
  /** 选中的事件（用于预览） */
  selectedEvent: ApiEvent | null

  /** 设置事件列表 */
  setEvents: (events: ApiEvent[]) => void
  /** 设置 artifacts 列表 */
  setArtifacts: (artifacts: TaskArtifact[]) => void
  /** 选中一个事件 */
  selectEvent: (event: ApiEvent | null) => void
  /** 清空所有数据 */
  reset: () => void
  /**  */
  setProcessEvents: (processEvents: ExtendedEvent[]) => void
}

/**
 * 事件状态管理 Store
 * 用于在组件之间传递和共享事件数据和 artifacts
 */
export const useEventStore = create<EventStoreState>((set) => ({
  events: [],
  processEvents: [],
  artifacts: [],
  selectedEvent: null,

  setEvents: (events) => set({ events }),

  setArtifacts: (artifacts) => set({ artifacts }),

  selectEvent: (event) => set({ selectedEvent: event }),

  reset: () => set({ events: [], artifacts: [], selectedEvent: null }),

  setProcessEvents: (processEvents) => set({ processEvents }),
}))

export default useEventStore
