import { request } from '@/utils/request'
import type { PageResult, PageParams } from '@/types/api'

/**
 * 客服聊天（管理端）
 *
 * 后端契约：com.snack.server.module.chat.controller.ChatAdminController
 *
 * 会话：
 *   GET    /api/admin/chat/session/page                → Page<ChatSessionVO>
 *   GET    /api/admin/chat/session/{id}                → ChatSessionDetailVO
 *   POST   /api/admin/chat/session/{id}/accept         → ChatSessionVO       接管
 *   POST   /api/admin/chat/session/{id}/transfer?toAdminId= → ChatSessionVO  转接
 *   PUT    /api/admin/chat/session/{id}/status/{status}→ Void
 *   PUT    /api/admin/chat/session/{id}/close          → Void
 *
 * 消息：
 *   GET    /api/admin/chat/session/{id}/messages?afterSeq&limit → ChatMessageVO[]
 *   POST   /api/admin/chat/session/{id}/message        → ChatMessageVO
 *   PUT    /api/admin/chat/session/{id}/ack            → Void
 *
 * 快捷回复：
 *   GET    /api/admin/chat/quick-reply/list?category=  → QuickReplyVO[]
 *   GET    /api/admin/chat/quick-reply/{id}            → QuickReplyVO
 *   POST   /api/admin/chat/quick-reply                 → Long
 *   PUT    /api/admin/chat/quick-reply                 → Void
 *   DELETE /api/admin/chat/quick-reply/{id}            → Void
 *
 * 在线：
 *   POST   /api/admin/chat/heartbeat                   → Void
 *   GET    /api/admin/chat/admin/online/count          → Long
 */

export interface ChatSession {
  id: number
  sessionNo: string
  userId: number
  username?: string
  nickname?: string
  avatar?: string
  adminId?: number
  adminName?: string
  status: 0 | 1 | 2 | 3
  statusText?: string
  unreadCount: number
  lastMessage?: string
  lastTime?: string
  createTime: string
}

export interface ChatSessionDetail {
  session: ChatSession
  userSeq: number
  adminSeq: number
  closeTime?: string
}

export interface ChatMessage {
  id: number
  sessionId: number
  seq: number
  senderId: number
  senderType: 0 | 1 | 2
  type: 'text' | 'image' | 'file' | 'product' | 'system'
  content: string
  extra?: string
  isRecalled: 0 | 1
  createTime: string
}

export interface QuickReply {
  id: number
  category?: string
  title: string
  content: string
  sort: number
  status: 0 | 1
  creatorId?: number
  createTime: string
}

export interface ChatSessionPageQuery extends PageParams {
  status?: 0 | 1 | 2 | 3
  keyword?: string
}

// ==================== 会话 ====================

export function getChatSessionPageApi(params: ChatSessionPageQuery) {
  return request<PageResult<ChatSession>>({
    url: '/api/admin/chat/session/page',
    method: 'GET',
    params
  })
}

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

export function acceptChatSessionApi(id: number) {
  return request<ChatSession>({
    url: `/api/admin/chat/session/${id}/accept`,
    method: 'POST'
  })
}

export function transferChatSessionApi(id: number, toAdminId: number) {
  return request<ChatSession>({
    url: `/api/admin/chat/session/${id}/transfer`,
    method: 'POST',
    params: { toAdminId }
  })
}

export function updateChatSessionStatusApi(id: number, status: 0 | 1 | 2 | 3) {
  return request<void>({
    url: `/api/admin/chat/session/${id}/status/${status}`,
    method: 'PUT'
  })
}

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

// ==================== 消息 ====================

export function getChatMessagesApi(sessionId: number, params: { afterSeq?: number; limit?: number } = {}) {
  return request<ChatMessage[]>({
    url: `/api/admin/chat/session/${sessionId}/messages`,
    method: 'GET',
    params
  })
}

export function sendChatMessageApi(sessionId: number, data: { type?: string; content: string; extra?: string }) {
  return request<ChatMessage>({
    url: `/api/admin/chat/session/${sessionId}/message`,
    method: 'POST',
    data
  })
}

export function ackChatMessagesApi(sessionId: number, data: { seq: number }) {
  return request<void>({
    url: `/api/admin/chat/session/${sessionId}/ack`,
    method: 'PUT',
    data
  })
}

// ==================== 快捷回复 ====================

export function getQuickReplyListApi(category?: string) {
  return request<QuickReply[]>({
    url: '/api/admin/chat/quick-reply/list',
    method: 'GET',
    params: category ? { category } : undefined
  })
}

export function getQuickReplyDetailApi(id: number) {
  return request<QuickReply>({
    url: `/api/admin/chat/quick-reply/${id}`,
    method: 'GET'
  })
}

export function createQuickReplyApi(data: Omit<QuickReply, 'id' | 'createTime'>) {
  return request<number>({
    url: '/api/admin/chat/quick-reply',
    method: 'POST',
    data
  })
}

export function updateQuickReplyApi(data: QuickReply) {
  return request<void>({
    url: '/api/admin/chat/quick-reply',
    method: 'PUT',
    data
  })
}

export function deleteQuickReplyApi(id: number) {
  return request<void>({
    url: `/api/admin/chat/quick-reply/${id}`,
    method: 'DELETE'
  })
}

// ==================== 在线 ====================

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

export function getOnlineAdminCountApi() {
  return request<number>({
    url: '/api/admin/chat/admin/online/count',
    method: 'GET'
  })
}
