import { request } from '@/utils/request'
import type {
  ChatSession,
  ChatSessionDetail,
  ChatMessage
} from '@/types'

/** 创建/恢复会话 */
export function createOrRestoreChatSessionApi() {
  return request<ChatSessionDetail>({
    url: '/api/chat/session',
    method: 'POST'
  })
}

/** 我的会话列表 */
export function getMyChatSessionsApi() {
  return request<ChatSession[]>({
    url: '/api/chat/sessions',
    method: 'GET'
  })
}

/** 会话详情 */
export function getChatSessionDetailApi(id: number) {
  return request<ChatSessionDetail>({
    url: `/api/chat/session/${id}`,
    method: 'GET'
  })
}

/** 历史消息（分页） */
export function getChatMessagesApi(id: number, params: { afterSeq?: number; limit?: number } = {}) {
  return request<ChatMessage[]>({
    url: `/api/chat/session/${id}/messages`,
    method: 'GET',
    params
  })
}

/** 发送消息（HTTP 兜底） */
export function sendChatMessageApi(id: number, data: { type?: string; content: string; extra?: string }) {
  return request<ChatMessage>({
    url: `/api/chat/session/${id}/message`,
    method: 'POST',
    data
  })
}

/** 已读确认 */
export function ackChatMessagesApi(id: number, data: { seq: number }) {
  return request<void>({
    url: `/api/chat/session/${id}/ack`,
    method: 'PUT',
    data
  })
}

/** 关闭会话 */
export function closeChatSessionApi(id: number) {
  return request<void>({
    url: `/api/chat/session/${id}/close`,
    method: 'PUT'
  })
}

/** 客服是否在线 */
export function isAdminOnlineApi() {
  return request<boolean>({
    url: '/api/chat/admin/online',
    method: 'GET'
  })
}

/** 心跳 */
export function chatHeartbeatApi() {
  return request<void>({
    url: '/api/chat/heartbeat',
    method: 'POST'
  })
}
