import request from '@/utils/request'

export type PrivateChatMessageType = 'text' | 'image'

export interface PrivateChatRelationship {
    is_followed_by_me: boolean
    is_following_me: boolean
    is_mutual: boolean
    relationship_text: string
}

export interface PrivateChatUser {
    id: number
    sn?: string
    nickname?: string
    avatar?: string
}

export interface PrivateChatSession {
    id: number
    target_user_id: number
    target_user: PrivateChatUser | null
    last_message_id: number
    last_message_type: PrivateChatMessageType
    last_message_content: string
    last_sender_id: number
    unread_count: number
    last_active_time: string | number
    create_time: string | number
    relationship: PrivateChatRelationship
}

export interface PrivateChatMessage {
    id: number
    session_id: number
    sender_id: number
    receiver_id: number
    message_type: PrivateChatMessageType
    content: string
    is_self: boolean
    read_time: number
    create_time: string | number
}

export interface PrivateChatVoiceToTextResult {
    text: string
    duration_ms: number
}

export function getChatSessions(data?: { page_no?: number; page_size?: number }) {
    return request.get<{
        lists: PrivateChatSession[]
        page_no: number
        page_size: number
        count: number
    }>({ url: '/chat/sessions', data })
}

export function openChat(data: { target_user_id: number }) {
    return request.post<PrivateChatSession>({ url: '/chat/open', data })
}

export function getChatMessages(data: {
    session_id: number
    after_id?: number
    page_no?: number
    page_size?: number
}) {
    return request.get<{
        session: PrivateChatSession
        lists: PrivateChatMessage[]
        page_no: number
        page_size: number
    }>({ url: '/chat/messages', data })
}

export function sendChatMessage(data: {
    session_id: number
    message_type: PrivateChatMessageType
    content: string
}) {
    return request.post<PrivateChatMessage>({ url: '/chat/send', data })
}

export function voiceToText(filePath: string, token?: string) {
    return request.uploadFile({
        url: '/chat/voiceToText',
        filePath,
        name: 'file',
        header: {
            token
        },
        fileType: 'audio'
    }) as Promise<PrivateChatVoiceToTextResult>
}
