/**
 * 双人对练 API
 */
import request from '@/api/request'

// ==================== 类型定义 ====================

export interface CreateRoomRequest {
  scene_id?: number
  scene_name?: string
  scene_type?: string
  scene_background?: string
  role_a_name?: string
  role_b_name?: string
  role_a_description?: string
  role_b_description?: string
  host_role?: 'A' | 'B'
  room_name?: string
}

export interface CreateRoomResponse {
  room_code: string
  room_id: number
  room_name: string
  my_role: string
  my_role_name: string
}

export interface JoinRoomResponse {
  room_code: string
  room_id: number
  room_name: string
  status: string
  my_role: string
  my_role_name: string
}

export interface RoomUser {
  id: number
  username: string
  full_name: string
  avatar_url?: string
}

export interface RoomInfo {
  id: number
  room_code: string
  room_name?: string
  scene_id?: number
  scene_name?: string
  scene_type?: string
  scene_background?: string
  role_a_name: string
  role_b_name: string
  role_a_description?: string
  role_b_description?: string
  host_role: string
  status: string
  created_at?: string
  started_at?: string
  ended_at?: string
  duration_seconds: number
  total_turns: number
  role_a_turns: number
  role_b_turns: number
}

export interface RoomDetailResponse {
  room: RoomInfo
  host_user?: RoomUser
  guest_user?: RoomUser
  host_role_name?: string
  guest_role_name?: string
  my_role?: string
  my_role_name?: string
  is_host: boolean
}

export type MessageType = 
  | 'chat' 
  | 'system' 
  | 'join' 
  | 'leave' 
  | 'start' 
  | 'end'
  | 'voice_start'
  | 'voice_offer'
  | 'voice_answer'
  | 'ice_candidate'
  | 'voice_end'

export interface RoomMessage {
  id: number
  room_id: number
  user_id?: number
  message_type: MessageType
  content?: string
  role_name?: string
  sequence: number
  created_at: string
}

export interface WebRTCSignalRequest {
  signal_type: 'voice_start' | 'voice_offer' | 'voice_answer' | 'ice_candidate' | 'voice_end'
  payload: Record<string, any>
}

export interface MessagesResponse {
  messages: RoomMessage[]
  room_status: string
  last_sequence: number
}

export interface RoomListItem {
  id: number
  room_code: string
  room_name?: string
  scene_name?: string
  status: string
  is_host: boolean
  created_at?: string
  duration_seconds: number
  total_turns: number
}

// ==================== API 函数 ====================

/**
 * 创建房间
 */
export function createRoom(data: CreateRoomRequest) {
  return request.post<CreateRoomResponse>('/api/v1/practice/rooms', data)
}

/**
 * 加入房间
 */
export function joinRoom(roomCode: string) {
  return request.post<JoinRoomResponse>('/api/v1/practice/rooms/join', {
    room_code: roomCode
  })
}

/**
 * 获取房间详情
 */
export function getRoomDetail(roomCode: string) {
  return request.get<RoomDetailResponse>(`/api/v1/practice/rooms/${roomCode}`)
}

/**
 * 开始对练
 */
export function startPractice(roomCode: string) {
  return request.post(`/api/v1/practice/rooms/${roomCode}/start`)
}

/**
 * 结束对练
 */
export function endPractice(roomCode: string) {
  return request.post(`/api/v1/practice/rooms/${roomCode}/end`)
}

/**
 * 离开房间
 */
export function leaveRoom(roomCode: string) {
  return request.post(`/api/v1/practice/rooms/${roomCode}/leave`)
}

/**
 * 发送消息
 */
export function sendMessage(roomCode: string, content: string) {
  return request.post<RoomMessage>(`/api/v1/practice/rooms/${roomCode}/message`, {
    content
  })
}

/**
 * 获取消息列表
 */
export function getMessages(roomCode: string, sinceSequence: number = 0) {
  return request.get<MessagesResponse>(`/api/v1/practice/rooms/${roomCode}/messages`, {
    params: { since_sequence: sinceSequence }
  })
}

/**
 * 获取我的房间列表
 */
export function getMyRooms(status?: string, limit: number = 20) {
  return request.get<{ rooms: RoomListItem[] }>('/api/v1/practice/rooms', {
    params: { status, limit }
  })
}

/**
 * 生成分享链接
 */
export function generateShareLink(roomCode: string): string {
  const baseUrl = window.location.origin
  return `${baseUrl}/trainee/duo-practice/join/${roomCode}`
}

/**
 * 发送 WebRTC 信令
 */
export function sendSignal(roomCode: string, signalType: string, payload: Record<string, any>) {
  return request.post(`/api/v1/practice/rooms/${roomCode}/signal`, {
    signal_type: signalType,
    payload
  })
}

/**
 * 获取对练报告
 */
export function getPracticeReport(roomCode: string) {
  return request.get(`/api/v1/practice/rooms/${roomCode}/report`)
}
