/**
 * 录制器 API 接口
 *
 * @module api/recorder
 * @author czj
 * @date 2026-07-09
 */

import request from '@/utils/request'

/**
 * 录制器 API 服务
 */
export const recorderApi = {
  /**
   * 启动录制会话
   */
  async start(data: { url: string; module_id: string; headless?: boolean }): Promise<{
    session_id: string
    message: string
  }> {
    const response = await request.post('/api/recorder/start', data)
    return response as any
  },

  /**
   * 获取已录制步骤
   */
  async getSteps(sessionId: string): Promise<{
    session_id: string
    steps: any[]
    total: number
  }> {
    const response = await request.get('/api/recorder/steps', {
      params: { session_id: sessionId }
    })
    return response as any
  },

  /**
   * 添加步骤
   */
  async addStep(sessionId: string, step: any): Promise<any> {
    const response = await request.post('/api/recorder/step', {
      session_id: sessionId,
      step: step,
    })
    return response as any
  },

  /**
   * 添加断言
   */
  async addAssertion(sessionId: string, data: {
    selector: string
    type: string
    expected?: string
  }): Promise<any> {
    const response = await request.post('/api/recorder/assertion', {
      session_id: sessionId,
      ...data,
    })
    return response as any
  },

  /**
   * 停止录制
   */
  async stop(sessionId: string): Promise<any> {
    const response = await request.post('/api/recorder/stop', null, {
      params: { session_id: sessionId }
    })
    return response as any
  },

  /**
   * 保存为用例
   */
  async save(data: {
    session_id: string
    name: string
    module_id: string
    description?: string
    priority?: string
    tags?: string[]
  }): Promise<any> {
    const response = await request.post('/api/recorder/save', data)
    return response as any
  },
}