// Cloudflare Stream API 类型定义

// 视频状态
export type VideoState = 'pendingupload' | 'downloading' | 'queued' | 'inprogress' | 'ready' | 'error'

// 视频信息
export interface CloudflareVideo {
  uid: string
  creator?: string
  thumbnail: string
  thumbnailTimestampPct?: number
  readyToStream: boolean
  readyToStreamAt?: string
  status: {
    state: VideoState
    pctComplete?: string
    errorReasonCode?: string
    errorReasonText?: string
  }
  meta: Record<string, any>
  created: string
  modified: string
  scheduledDeletion?: string
  size: number
  preview: string
  allowedOrigins: string[]
  requireSignedURLs: boolean
  uploaded: string
  uploadExpiry?: string
  maxSizeBytes?: number
  maxDurationSeconds?: number
  duration: number
  input: {
    width: number
    height: number
  }
  playback: {
    hls: string
    dash: string
  }
  watermark?: {
    uid: string
    size: number
    height: number
    width: number
    created: string
    downloadedFrom: string
    name: string
    opacity: number
    padding: number
    scale: number
    position: string
  }
  clippedFrom?: string
  publicDetails?: {
    title?: string
    share_link?: string
    channel_link?: string
    logo?: string
  }
}

// 直播输入（Live Input）
export interface CloudflareLiveInput {
  uid: string
  created: string
  modified: string
  meta: Record<string, any>
  deleteRecordingAfterDays?: number
  status?: {
    current: {
      state: 'connected' | 'disconnected'
      statusEnteredAt?: string
      statusLastChangedAt?: string
    }
  }
  rtmps: {
    url: string
    streamKey: string
  }
  rtmpsPlayback?: {
    url: string
    streamKey: string
  }
  srt?: {
    url: string
    streamId: string
    passphrase: string
  }
  srtPlayback?: {
    url: string
    streamId: string
    passphrase: string
  }
  webRTC?: {
    url: string
  }
  webRTCPlayback?: {
    url: string
  }
  recording?: {
    mode: 'off' | 'automatic'
    requireSignedURLs?: boolean
    allowedOrigins?: string[]
    timeoutSeconds?: number
  }
}

// 直播输出
export interface CloudflareLiveOutput {
  uid: string
  url: string
  streamKey: string
  enabled: boolean
}

// 上传 URL 响应
export interface UploadUrlResponse {
  uploadURL: string
  uid: string
  watermark?: any
}

// 直接上传响应
export interface DirectUploadResponse {
  result: {
    uploadURL: string
    uid: string
  }
}

// API 响应包装
export interface CloudflareApiResponse<T> {
  result: T
  success: boolean
  errors: Array<{ code: number; message: string }>
  messages: Array<{ code: number; message: string }>
}

// 列表响应
export interface CloudflareListResponse<T> {
  result: T[]
  success: boolean
  errors: Array<{ code: number; message: string }>
  messages: Array<{ code: number; message: string }>
  result_info?: {
    page: number
    per_page: number
    count: number
    total_count: number
  }
}

// 签名 URL 参数
export interface SignedUrlParams {
  id: string // 视频 ID
  exp?: number // 过期时间戳
  nbf?: number // 生效时间戳
  downloadable?: boolean // 是否可下载
  accessRules?: Array<{
    type: 'ip.geoip.country' | 'ip.src' | 'any'
    country?: string[]
    ip?: string[]
    action: 'allow' | 'block'
  }>
}

// 视频播放信息
export interface VideoPlaybackInfo {
  uid: string
  preview: string
  thumbnail: string
  playback: {
    hls: string
    dash: string
  }
  iframe: string
}

// 创建直播输入参数
export interface CreateLiveInputParams {
  meta?: Record<string, any>
  recording?: {
    mode: 'off' | 'automatic'
    requireSignedURLs?: boolean
    allowedOrigins?: string[]
    timeoutSeconds?: number
  }
  deleteRecordingAfterDays?: number
}

// 创建上传 URL 参数
export interface CreateUploadUrlParams {
  maxDurationSeconds?: number
  expiry?: string
  creator?: string
  thumbnailTimestampPct?: number
  allowedOrigins?: string[]
  requireSignedURLs?: boolean
  watermark?: { uid: string }
  meta?: Record<string, any>
  scheduledDeletion?: string
}
