package com.example.controller;

import com.example.common.Result;
import com.example.common.config.TokenUtils;
import com.example.entity.Account;
import com.example.service.AIChatService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * AI聊天控制器
 */
@RestController
@RequestMapping("/aiChat")
public class AIChatController {

    @Resource
    private AIChatService aiChatService;
    
    /**
     * 发送消息到AI并获取回复
     */
    @PostMapping("/sendMessage")
    public Result sendMessage(@RequestBody Map<String, String> params) {
        String message = params.get("message");
        
        // 获取当前登录用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null) {
            return Result.error("401", "请先登录");
        }
        
        // 获取AI回复
        String aiReply = aiChatService.sendMessage(message);
        
        Map<String, Object> userMessage = new HashMap<>();
        userMessage.put("content", message);
        userMessage.put("messageType", "user");
        userMessage.put("createTime", LocalDateTime.now());
        
        Map<String, Object> aiMessage = new HashMap<>();
        aiMessage.put("content", aiReply);
        aiMessage.put("messageType", "ai");
        aiMessage.put("createTime", LocalDateTime.now());
        
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("userMessage", userMessage);
        resultMap.put("aiMessage", aiMessage);
        
        return Result.success(resultMap);
    }
} 