import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { wsManager } from '../composables/useWebSocket'

export interface ChannelInfo {
  type: string
  connectedAt: number
  capabilities: {
    supportsSync: boolean
    supportsImages: boolean
    supportsMarkdown: boolean
    supportsStreaming: boolean
    supportsTradingViewEmbed: boolean
  }
}

export interface ChannelState {
  connected: Record<string, ChannelInfo>
}

export const useChannelStore = defineStore('channelState', () => {
  const connected = ref<Record<string, ChannelInfo>>({})

  // Session readiness — computed getters backed by wsManager refs.
  // Using computed (not ref) keeps these out of $state so useStateSync won't
  // pick them up and try to send patches to the gateway.
  const sessionStatus = computed(() => wsManager.sessionStatus.value)
  const statusMessage = computed(() => wsManager.statusMessage.value)
  const isReady = computed(() => wsManager.sessionStatus.value === 'ready')

  return { connected, sessionStatus, statusMessage, isReady }
})
