package com.jeesharp.business.ai.controller;

import com.jeesharp.business.modules.ai.application.AiChatMobileApplicationService;
import com.jeesharp.business.modules.ai.application.AiFaqApplicationService;
import com.jeesharp.business.modules.ai.dto.AiChatFeedbackDTO;
import com.jeesharp.business.modules.ai.dto.AiChatMessageDTO;
import com.jeesharp.business.modules.ai.dto.AiChatSessionCreateDTO;
import com.jeesharp.business.modules.ai.vo.AiChatAnswerVO;
import com.jeesharp.business.modules.ai.vo.AiChatHistoryVO;
import com.jeesharp.business.modules.ai.vo.AiChatSessionCreateVO;
import com.jeesharp.business.modules.ai.vo.AiFaqListVO;
import com.jeesharp.framework.pojo.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序 AI 客服接口。
 */
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/ai")
@Tag(name = "小程序-AI客服")
@AllArgsConstructor
public class AiMobileController {

    private final AiFaqApplicationService aiFaqApplicationService;
    private final AiChatMobileApplicationService aiChatMobileApplicationService;

    @Operation(summary = "FAQ 列表")
    @GetMapping("/faq/list")
    public AjaxResult<List<AiFaqListVO>> faqList(@RequestParam(value = "category", required = false) String category,
                                                 @RequestParam(value = "keyword", required = false) String keyword) {
        return AjaxResult.okData(aiFaqApplicationService.listForMobile(category, keyword));
    }

    @Operation(summary = "创建会话")
    @PostMapping("/chat/session/create")
    public AjaxResult<AiChatSessionCreateVO> createSession(@RequestBody AiChatSessionCreateDTO dto) {
        return AjaxResult.okData(aiChatMobileApplicationService.createSession(dto));
    }

    @Operation(summary = "发送消息")
    @PostMapping("/chat/message")
    public AjaxResult<AiChatAnswerVO> message(@RequestBody @Valid AiChatMessageDTO dto) {
        return AjaxResult.okData(aiChatMobileApplicationService.sendMessage(dto));
    }

    @Operation(summary = "会话历史")
    @GetMapping("/chat/history/{sessionId}")
    public AjaxResult<AiChatHistoryVO> history(@PathVariable("sessionId") String sessionId) {
        return AjaxResult.okData(aiChatMobileApplicationService.getHistory(sessionId));
    }

    @Operation(summary = "会话反馈")
    @PostMapping("/chat/feedback")
    public AjaxResult<Void> feedback(@RequestBody @Valid AiChatFeedbackDTO dto) {
        aiChatMobileApplicationService.feedback(dto);
        return AjaxResult.ok("反馈成功");
    }
}
