import { apiRequest } from '../../lib/api'
import type { Message, Page, Session } from '../../lib/types'

export function createSession(title?: string) {
  return apiRequest<Session>('/api/v1/sessions', {
    method: 'POST',
    body: JSON.stringify({ title: title || null }),
  })
}

export function listSessions() {
  return apiRequest<Page<Session>>('/api/v1/sessions?limit=100')
}

export function listMessages(sessionId: string) {
  return apiRequest<Page<Message>>(`/api/v1/sessions/${sessionId}/messages?limit=100`)
}

export function deleteSession(sessionId: string) {
  return apiRequest<void>(`/api/v1/sessions/${sessionId}`, { method: 'DELETE' })
}

export function announceSessionChange() {
  window.dispatchEvent(new Event('kbqa:sessions-changed'))
}
