import { get, post } from '@/utils/request'
import type {
  ApiResponse,
  CameraDTO,
  ChannelDTO,
  CameraInfoDTO,
  CameraAddRequest,
  CameraUpdateRequest,
  SwitchChannelRequest,
  PTZAction
} from '@/types'

// ==================== Controller APIs (MVP) ====================

// 获取摄像头列表
export function listCameras(machineId?: string): Promise<ApiResponse<CameraDTO[]>> {
  return get('/camera/list', machineId ? { machineId } : undefined)
}

// 获取摄像头信息
export function getCamera(cameraId: string): Promise<ApiResponse<CameraDTO>> {
  return get(`/camera/${cameraId}`)
}

// 切换摄像头通道 (MVP核心)
export function switchChannel(data: SwitchChannelRequest): Promise<ApiResponse<ChannelDTO>> {
  return post('/camera/switch', data)
}

// 获取当前活动通道
export function getCurrentChannel(machineId: string): Promise<ApiResponse<ChannelDTO>> {
  return get('/camera/current', { machineId })
}

// 开始PTZ控制 (后台专用)
export function ptzStart(cameraId: string, action: PTZAction, speed: number = 50): Promise<ApiResponse<null>> {
  return post(`/camera/${cameraId}/ptz/start`, undefined, {
    params: { action, speed }
  })
}

// 停止PTZ控制 (后台专用)
export function ptzStop(cameraId: string): Promise<ApiResponse<null>> {
  return post(`/camera/${cameraId}/ptz/stop`)
}

// ==================== Admin APIs ====================

// 获取摄像头列表 (管理后台)
export function adminListCameras(machineId?: string): Promise<ApiResponse<CameraInfoDTO[]>> {
  return get('/admin/cameras/list', machineId ? { machineId } : undefined)
}

// 获取摄像头详情
export function adminGetCamera(id: number): Promise<ApiResponse<CameraInfoDTO>> {
  return get('/admin/cameras/detail', { id })
}

// 添加摄像头
export function adminAddCamera(data: CameraAddRequest): Promise<ApiResponse<CameraInfoDTO>> {
  return post('/admin/cameras/add', data)
}

// 更新摄像头
export function adminUpdateCamera(data: CameraUpdateRequest): Promise<ApiResponse<CameraInfoDTO>> {
  return post('/admin/cameras/update', data)
}

// 删除摄像头
export function adminDeleteCamera(id: number): Promise<ApiResponse<null>> {
  return post('/admin/cameras/delete', undefined, {
    params: { id }
  })
}

// 检测摄像头连通性
export function adminCheckCamera(id: number): Promise<ApiResponse<boolean>> {
  return post('/admin/cameras/check', undefined, {
    params: { id }
  })
}

// ==================== 兼容旧代码的别名 ====================

// 获取设备列表 (兼容)
export const listDevice = adminListCameras

// 获取设备详情 (兼容)
export const getDevice = (deviceId: string) => getCamera(deviceId)

// 添加设备 (兼容)
export const addDevice = adminAddCamera

// 修改设备 (兼容)
export const updateDevice = adminUpdateCamera

// 删除设备 (兼容)
export const delDevice = (id: number) => adminDeleteCamera(id)

// PTZ控制 (兼容旧API)
export function ptzControl(
  _deviceId: string,
  channelId: string,
  command: string,
  horizonSpeed?: number,
  _verticalSpeed?: number,
  _zoomSpeed?: number
): Promise<ApiResponse<null>> {
  // 映射旧的命令到新的 action
  const actionMap: Record<string, PTZAction> = {
    up: 'up',
    down: 'down',
    left: 'left',
    right: 'right',
    zoomin: 'zoom_in',
    zoomout: 'zoom_out',
    stop: 'stop'
  }
  const action = actionMap[command.toLowerCase()] || 'stop'
  const speed = horizonSpeed || 50

  // 使用 channelId 关联的 cameraId 进行 PTZ 控制
  return ptzStart(channelId, action, speed)
}
