/**
 * 视频流管理 API（通过后端代理）
 *
 * 推荐使用这个封装，通过后端代理调用 Cloudflare API
 * 避免在前端暴露 API Token
 */

import { get, post, put, del } from '@/utils/request'
import type { ApiResponse } from '@/types'
import type {
  CloudflareVideo,
  CloudflareLiveInput,
  CreateLiveInputParams,
  CreateUploadUrlParams,
  VideoPlaybackInfo
} from '@/types/cloudflare'

// ==================== 视频管理 ====================

/**
 * 获取视频列表
 */
export function listVideos(params?: {
  pageNum?: number
  pageSize?: number
  search?: string
  status?: string
}): Promise<ApiResponse<{ rows: CloudflareVideo[]; total: number }>> {
  return get('/stream/video/list', params)
}

/**
 * 获取视频详情
 */
export function getVideo(videoId: string): Promise<ApiResponse<CloudflareVideo>> {
  return get(`/stream/video/${videoId}`)
}

/**
 * 删除视频
 */
export function deleteVideo(videoId: string): Promise<ApiResponse<null>> {
  return del(`/stream/video/${videoId}`)
}

/**
 * 从 URL 导入视频
 */
export function importVideoFromUrl(data: {
  url: string
  name?: string
  meta?: Record<string, any>
}): Promise<ApiResponse<CloudflareVideo>> {
  return post('/stream/video/import', data)
}

/**
 * 获取上传 URL
 */
export function getUploadUrl(params?: CreateUploadUrlParams): Promise<
  ApiResponse<{
    uploadURL: string
    uid: string
  }>
> {
  return post('/stream/video/upload-url', params)
}

/**
 * 获取视频播放信息
 */
export function getVideoPlayback(videoId: string): Promise<ApiResponse<VideoPlaybackInfo>> {
  return get(`/stream/video/${videoId}/playback`)
}

// ==================== 直播管理 ====================

/**
 * 获取直播输入列表
 */
export function listLiveInputs(params?: {
  pageNum?: number
  pageSize?: number
}): Promise<ApiResponse<{ rows: CloudflareLiveInput[]; total: number }>> {
  return get('/stream/live/list', params)
}

/**
 * 创建直播输入
 */
export function createLiveInput(data?: CreateLiveInputParams): Promise<ApiResponse<CloudflareLiveInput>> {
  return post('/stream/live', data)
}

/**
 * 获取直播输入详情
 */
export function getLiveInput(liveInputId: string): Promise<ApiResponse<CloudflareLiveInput>> {
  return get(`/stream/live/${liveInputId}`)
}

/**
 * 更新直播输入
 */
export function updateLiveInput(
  liveInputId: string,
  data: CreateLiveInputParams
): Promise<ApiResponse<CloudflareLiveInput>> {
  return put(`/stream/live/${liveInputId}`, data)
}

/**
 * 删除直播输入
 */
export function deleteLiveInput(liveInputId: string): Promise<ApiResponse<null>> {
  return del(`/stream/live/${liveInputId}`)
}

/**
 * 获取直播播放信息
 */
export function getLivePlayback(liveInputId: string): Promise<ApiResponse<VideoPlaybackInfo>> {
  return get(`/stream/live/${liveInputId}/playback`)
}

/**
 * 获取直播录像列表
 */
export function listLiveRecordings(
  liveInputId: string,
  params?: {
    pageNum?: number
    pageSize?: number
  }
): Promise<ApiResponse<{ rows: CloudflareVideo[]; total: number }>> {
  return get(`/stream/live/${liveInputId}/recordings`, params)
}

// ==================== 转推管理 ====================

/**
 * 获取转推输出列表
 */
export function listLiveOutputs(liveInputId: string): Promise<ApiResponse<any[]>> {
  return get(`/stream/live/${liveInputId}/outputs`)
}

/**
 * 创建转推输出
 */
export function createLiveOutput(
  liveInputId: string,
  data: {
    url: string
    streamKey: string
    enabled?: boolean
  }
): Promise<ApiResponse<any>> {
  return post(`/stream/live/${liveInputId}/outputs`, data)
}

/**
 * 删除转推输出
 */
export function deleteLiveOutput(liveInputId: string, outputId: string): Promise<ApiResponse<null>> {
  return del(`/stream/live/${liveInputId}/outputs/${outputId}`)
}
