import request from '@/utils/request'
import { AI_REQUEST_CONFIG, withAiRequestTimeout } from './aiRequest'
import { requestAiAnalysis } from './ai'

// 帖子列表
export function getCommunityPosts(data?: Record<string, any>) {
    return request.get({ url: '/community/postLists', data })
}

// 帖子详情
export function getCommunityPostDetail(data: { id: number }) {
    return request.get({ url: '/community/postDetail', data })
}

// 发帖
export function publishPost(data: Record<string, any>) {
    return request.post({ url: '/community/publish', data })
}

// 评论列表
export function getCommentLists(data: { post_id: number; page_no?: number; page_size?: number }) {
    return request.get({ url: '/community/commentLists', data })
}

// 发评论
export function addComment(data: {
    post_id: number
    content: string
    parent_id?: number
    reply_user_id?: number
}) {
    return request.post({ url: '/community/addComment', data })
}

// 删除评论
export function deleteComment(data: { comment_id: number }) {
    return request.post({ url: '/community/deleteComment', data })
}

// 删除帖子
export function deletePost(data: { post_id: number }) {
    return request.post({ url: '/community/deletePost', data })
}

// 点赞/取消
export function toggleLike(data: { target_id: number; target_type: number }) {
    return request.post({ url: '/community/like', data })
}

// 关注/取消
export function toggleFollow(data: { follow_user_id: number }) {
    return request.post({ url: '/community/follow', data })
}

// 标签列表
export function getTagLists() {
    return request.get({ url: '/community/tagLists' })
}

// 购买帖子
export function purchasePost(data: { post_id: number; confirm?: number }) {
    return request.post({ url: '/community/purchasePost', data })
}

// AI翻译帖子
export function translatePost(data: { post_id: number; confirm?: number }) {
    return request.post({ url: '/community/translatePost', data })
}

// 帖子AI分析
export function getCommunityPostAiAnalysis(data: { post_id: number; force?: number }) {
    return requestAiAnalysis({ type: 'post', ...data })
}

// 帖子AI分析结果轮询，只查缓存，不触发新的模型分析
export function getCommunityPostAiAnalysisResult(data: { post_id: number }) {
    return request.get(withAiRequestTimeout({ url: '/community/aiAnalysisResult', data }), AI_REQUEST_CONFIG)
}

// 用户主页
export function getUserProfile(data: { user_id: number }) {
    return request.get({ url: '/community/userProfile', data })
}
