import { apiClient } from './client'
import type {
  ScriptBuilderSessionSummary,
  ScriptBuilderSessionDetail,
  ScriptBuilderMessageResponse,
  SaveToLibraryRequest,
} from '@/types'
import type { ScriptTemplateDetail } from '@/types'

export const scriptBuilderApi = {
  async createSession(language: string): Promise<ScriptBuilderSessionDetail> {
    const { data } = await apiClient.post('/scripts/builder/sessions', { language })
    return data
  },

  async listSessions(limit = 20, offset = 0): Promise<ScriptBuilderSessionSummary[]> {
    const { data } = await apiClient.get('/scripts/builder/sessions', {
      params: { limit, offset },
    })
    return data
  },

  async getSession(sessionId: string): Promise<ScriptBuilderSessionDetail> {
    const { data } = await apiClient.get(`/scripts/builder/sessions/${sessionId}`)
    return data
  },

  async sendMessage(sessionId: string, content: string): Promise<ScriptBuilderMessageResponse> {
    const { data } = await apiClient.post(
      `/scripts/builder/sessions/${sessionId}/messages`,
      { content }
    )
    return data
  },

  async deleteSession(sessionId: string): Promise<void> {
    await apiClient.delete(`/scripts/builder/sessions/${sessionId}`)
  },

  async saveToLibrary(sessionId: string, req: SaveToLibraryRequest): Promise<ScriptTemplateDetail> {
    const { data } = await apiClient.post(
      `/scripts/builder/sessions/${sessionId}/save`,
      req
    )
    return data
  },
}
