import { request } from '../base'
import type { ResultDomain, PageRequest } from '../../types'
import type { TbWorkcaseDTO } from '../../types/workcase/workcase'
import type {
    TbChatRoomDTO,
    TbChatRoomMemberDTO,
    TbChatRoomMessageDTO,
    TbCustomerServiceDTO,
    TbWordCloudDTO,
    ChatRoomVO,
    ChatMemberVO,
    ChatRoomMessageVO,
    CustomerServiceVO
} from '../../types/workcase'
import type {
    TbChat,
    TbChatMessage,
    CreateChatParam,
    ChatPrepareData,
    StopChatParam,
    CommentMessageParam,
    ChatListParam,
    ChatMessageListParam,
    SSECallbacks,
    SSETask,
    SSEMessageData
} from '../../types/ai/aiChat'

/* eslint-disable @typescript-eslint/no-explicit-any */
declare const uni: {
    getStorageSync: (key: string) => any
    request: (options: any) => any
}

// API 基础配置
const BASE_URL = 'http://localhost:8180'

/**
 * @description    工单对话相关接口
 * @filename    workcaseChat.ts
 * @author    cascade
 * @copyright xyzh
 * @since 2025-12-22
 */
export const workcaseChatAPI = {
    baseUrl: '/urban-lifeline/workcase/chat',

    // ====================== ChatRoom聊天室管理 ======================

    /**
     * 创建聊天室
     */
    createChatRoom(chatRoom: TbChatRoomDTO): Promise<ResultDomain<TbChatRoomDTO>> {
        return request<TbChatRoomDTO>({ url: `${this.baseUrl}/room`, method: 'POST', data: chatRoom })
    },

    /**
     * 更新聊天室
     */
    updateChatRoom(chatRoom: TbChatRoomDTO): Promise<ResultDomain<TbChatRoomDTO>> {
        return request<TbChatRoomDTO>({ url: `${this.baseUrl}/room`, method: 'PUT', data: chatRoom })
    },

    /**
     * 关闭聊天室
     */
    closeChatRoom(roomId: string, closedBy: string): Promise<ResultDomain<boolean>> {
        return request<boolean>({ url: `${this.baseUrl}/room/${roomId}/close?closedBy=${closedBy}`, method: 'POST' })
    },

    /**
     * 获取聊天室详情
     */
    getChatRoomById(roomId: string): Promise<ResultDomain<TbChatRoomDTO>> {
        return request<TbChatRoomDTO>({ url: `${this.baseUrl}/room/${roomId}`, method: 'GET' })
    },

    /**
     * 分页查询聊天室
     */
    getChatRoomPage(pageRequest: PageRequest<TbChatRoomDTO>): Promise<ResultDomain<ChatRoomVO>> {
        return request<ChatRoomVO>({ url: `${this.baseUrl}/room/page`, method: 'POST', data: pageRequest })
    },

    // ====================== ChatRoom成员管理 ======================

    /**
     * 添加聊天室成员
     */
    addChatRoomMember(member: TbChatRoomMemberDTO): Promise<ResultDomain<TbChatRoomMemberDTO>> {
        return request<TbChatRoomMemberDTO>({ url: `${this.baseUrl}/room/member`, method: 'POST', data: member })
    },

    /**
     * 移除聊天室成员
     */
    removeChatRoomMember(memberId: string): Promise<ResultDomain<boolean>> {
        return request<boolean>({ url: `${this.baseUrl}/room/member/${memberId}`, method: 'DELETE' })
    },

    /**
     * 获取聊天室成员列表
     */
    getChatRoomMemberList(roomId: string): Promise<ResultDomain<ChatMemberVO>> {
        return request<ChatMemberVO>({ url: `${this.baseUrl}/room/${roomId}/members`, method: 'GET' })
    },

    /**
     * 获取当前用户在指定聊天室的未读消息数
     */
    getUnreadCount(roomId: string, userId: string): Promise<ResultDomain<number>> {
        return request<number>({ url: `${this.baseUrl}/room/${roomId}/unread?userId=${userId}`, method: 'GET' })
    },

    // ====================== ChatRoom消息管理 ======================

    /**
     * 发送聊天室消息
     */
    sendMessage(message: TbChatRoomMessageDTO): Promise<ResultDomain<TbChatRoomMessageDTO>> {
        return request<TbChatRoomMessageDTO>({ url: `${this.baseUrl}/room/message`, method: 'POST', data: message })
    },

    /**
     * 分页查询聊天室消息
     */
    getChatMessagePage(pageRequest: PageRequest<TbChatRoomMessageDTO>): Promise<ResultDomain<ChatRoomMessageVO>> {
        return request<ChatRoomMessageVO>({ url: `${this.baseUrl}/room/message/page`, method: 'POST', data: pageRequest })
    },

    /**
     * 删除聊天室消息
     */
    deleteMessage(messageId: string): Promise<ResultDomain<boolean>> {
        return request<boolean>({ url: `${this.baseUrl}/room/message/${messageId}`, method: 'DELETE' })
    },

    // ====================== 客服人员管理 ======================

    /**
     * 添加客服人员
     */
    addCustomerService(customerService: TbCustomerServiceDTO): Promise<ResultDomain<TbCustomerServiceDTO>> {
        return request<TbCustomerServiceDTO>({ url: `${this.baseUrl}/customer-service`, method: 'POST', data: customerService })
    },

    /**
     * 更新客服人员
     */
    updateCustomerService(customerService: TbCustomerServiceDTO): Promise<ResultDomain<TbCustomerServiceDTO>> {
        return request<TbCustomerServiceDTO>({ url: `${this.baseUrl}/customer-service`, method: 'PUT', data: customerService })
    },

    /**
     * 删除客服人员
     */
    deleteCustomerService(userId: string): Promise<ResultDomain<boolean>> {
        return request<boolean>({ url: `${this.baseUrl}/customer-service/${userId}`, method: 'DELETE' })
    },

    /**
     * 分页查询客服人员
     */
    getCustomerServicePage(pageRequest: PageRequest<TbCustomerServiceDTO>): Promise<ResultDomain<CustomerServiceVO>> {
        return request<CustomerServiceVO>({ url: `${this.baseUrl}/customer-service/page`, method: 'POST', data: pageRequest })
    },

    /**
     * 更新客服在线状态
     */
    updateCustomerServiceStatus(userId: string, status: string): Promise<ResultDomain<boolean>> {
        return request<boolean>({ url: `${this.baseUrl}/customer-service/${userId}/status/${status}`, method: 'POST' })
    },

    /**
     * 获取可接待客服列表
     */
    getAvailableCustomerServices(): Promise<ResultDomain<CustomerServiceVO>> {
        return request<CustomerServiceVO>({ url: `${this.baseUrl}/customer-service/available`, method: 'GET' })
    },

    /**
     * 自动分配客服
     */
    assignCustomerService(roomId: string): Promise<ResultDomain<CustomerServiceVO>> {
        return request<CustomerServiceVO>({ url: `${this.baseUrl}/room/${roomId}/assign`, method: 'POST' })
    },

    // ====================== 词云管理 ======================

    /**
     * 添加词云
     */
    addWordCloud(wordCloud: TbWordCloudDTO): Promise<ResultDomain<TbWordCloudDTO>> {
        return request<TbWordCloudDTO>({ url: `${this.baseUrl}/wordcloud`, method: 'POST', data: wordCloud })
    },

    /**
     * 更新词云
     */
    updateWordCloud(wordCloud: TbWordCloudDTO): Promise<ResultDomain<TbWordCloudDTO>> {
        return request<TbWordCloudDTO>({ url: `${this.baseUrl}/wordcloud`, method: 'PUT', data: wordCloud })
    },

    /**
     * 查询词云列表
     */
    getWordCloudList(filter: TbWordCloudDTO): Promise<ResultDomain<TbWordCloudDTO>> {
        return request<TbWordCloudDTO>({ url: `${this.baseUrl}/wordcloud/list`, method: 'POST', data: filter })
    },

    /**
     * 分页查询词云
     */
    getWordCloudPage(pageRequest: PageRequest<TbWordCloudDTO>): Promise<ResultDomain<TbWordCloudDTO>> {
        return request<TbWordCloudDTO>({ url: `${this.baseUrl}/wordcloud/page`, method: 'POST', data: pageRequest })
    },

    // ====================== 视频会议管理（Jitsi Meet） ======================

    /**
     * 创建视频会议
     */
    createVideoMeeting(params: {
        roomId: string
        workcaseId?: string
        meetingName: string
        maxParticipants?: number
    }): Promise<ResultDomain<any>> {
        return request({ url: `${this.baseUrl}/meeting/create`, method: 'POST', data: params })
    },

    /**
     * 获取会议信息
     */
    getMeetingInfo(meetingId: string): Promise<ResultDomain<any>> {
        return request({ url: `${this.baseUrl}/meeting/${meetingId}`, method: 'GET' })
    },

    /**
     * 获取聊天室活跃会议
     */
    getActiveMeeting(roomId: string): Promise<ResultDomain<any>> {
        return request({ url: `${this.baseUrl}/meeting/room/${roomId}/active`, method: 'GET' })
    },

    /**
     * 加入会议（生成用户专属JWT）
     */
    joinMeeting(meetingId: string): Promise<ResultDomain<any>> {
        return request({ url: `${this.baseUrl}/meeting/${meetingId}/join`, method: 'POST' })
    },

    /**
     * 开始会议
     */
    startVideoMeeting(meetingId: string): Promise<ResultDomain<any>> {
        return request({ url: `${this.baseUrl}/meeting/${meetingId}/start`, method: 'POST' })
    },

    /**
     * 结束会议
     */
    endVideoMeeting(meetingId: string): Promise<ResultDomain<any>> {
        return request({ url: `${this.baseUrl}/meeting/${meetingId}/end`, method: 'POST' })
    },

    // ====================== 聊天室评分管理 ======================

    /**
     * 提交聊天室服务评分
     * @param roomId 聊天室ID
     * @param commentLevel 评分（1-5星）
     */
    submitComment(roomId: string, commentLevel: number): Promise<ResultDomain<boolean>> {
        return request({
            url: `/urban-lifeline/workcase/chat/room/${roomId}/comment`,
            method: 'POST',
            data: { commentLevel }
        })
    }
}
