/**
 * 推流服务 API
 *
 * 本地视频推流 Controller:
 * - POST /stream/local/start       启动本地视频推流
 * - POST /stream/local/stop        停止本地视频推流
 * - GET  /stream/local/{name}      获取推流状态
 * - GET  /stream/local/list        获取所有本地视频推流
 *
 * 推流服务 Controller:
 * - POST /stream/start             启动推流任务
 * - POST /stream/stop              停止推流任务
 * - GET  /stream/task/{streamSn}   获取任务状态
 * - GET  /stream/tasks             获取 LSS 推流任务列表
 * - GET  /stream/tasks/active      获取所有活动任务
 * - GET  /stream/channels          获取推流通道列表
 * - GET  /stream/playback/{streamSn} 获取播放信息
 * - POST /stream/switch            切换推流源
 */

import { get, post } from '@/utils/request'
import type { IBaseResponse, IListResponse } from '@/types'
import type {
  LocalVideoStreamDTO,
  StartLocalStreamRequest,
  StreamTaskDTO,
  StartStreamTaskRequest,
  StopStreamTaskRequest,
  SwitchStreamSourceRequest,
  StreamPushChannelDTO,
  PlaybackInfoDTO
} from '@/types'

// ==================== 本地视频推流 ====================

/**
 * 启动本地视频推流
 */
export function startLocalStream(data: StartLocalStreamRequest): Promise<IBaseResponse<LocalVideoStreamDTO>> {
  return post('/stream/local/start', data)
}

/**
 * 停止本地视频推流
 */
export function stopLocalStream(streamName: string): Promise<IBaseResponse<void>> {
  return post('/stream/local/stop', null, { params: { streamName } })
}

/**
 * 获取本地视频推流状态
 */
export function getLocalStreamStatus(streamName: string): Promise<IBaseResponse<LocalVideoStreamDTO>> {
  return get(`/stream/local/${streamName}`)
}

/**
 * 获取所有本地视频推流
 */
export function listLocalStreams(): Promise<IListResponse<LocalVideoStreamDTO>> {
  return get('/stream/local/list')
}

// ==================== 推流服务 ====================

/**
 * 启动推流任务
 */
export function startStreamTask(data: StartStreamTaskRequest): Promise<IBaseResponse<StreamTaskDTO>> {
  return post('/stream/start', data)
}

/**
 * 停止推流任务
 */
export function stopStreamTask(data: StopStreamTaskRequest): Promise<IBaseResponse<void>> {
  return post('/stream/stop', data)
}

/**
 * 获取任务状态
 */
export function getStreamTaskStatus(streamSn: string): Promise<IBaseResponse<StreamTaskDTO>> {
  return get(`/stream/task/${streamSn}`)
}

/**
 * 获取 LSS 推流任务列表
 */
export function listLssStreamTasks(lssId: string): Promise<IListResponse<StreamTaskDTO>> {
  return get('/stream/tasks', { lssId })
}

/**
 * 获取所有活动任务
 */
export function listActiveStreamTasks(): Promise<IListResponse<StreamTaskDTO>> {
  return get('/stream/tasks/active')
}

/**
 * 获取推流通道列表
 */
export function listStreamPushChannels(): Promise<IListResponse<StreamPushChannelDTO>> {
  return get('/stream/channels')
}

/**
 * 获取播放信息
 */
export function getStreamPlayback(streamSn: string): Promise<IBaseResponse<PlaybackInfoDTO>> {
  return get(`/stream/playback/${streamSn}`)
}

/**
 * 切换推流源
 */
export function switchStreamSource(data: SwitchStreamSourceRequest): Promise<IBaseResponse<StreamTaskDTO>> {
  return post('/stream/switch', data)
}
