package com.emotion.controller;

import com.emotion.common.Result;
import com.emotion.dto.request.EmotionSummaryGenerateRequest;
import com.emotion.dto.request.EmotionSummaryStatusRequest;
import com.emotion.dto.response.EmotionSummaryGenerateResponse;
import com.emotion.dto.response.EmotionSummaryStatusResponse;
import com.emotion.service.AiChatService;
import com.emotion.util.UserContextUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 情绪总结控制器
 *
 * @author huazhongmin
 * @date 2025-07-25
 */
@Slf4j
@RestController
@RequestMapping("/emotionSummary")
@Tag(name = "情绪总结管理", description = "用户情绪记录总结和分析功能")
public class EmotionSummaryController {

    @Autowired
    private AiChatService aiChatService;

    /**
     * 生成用户当天的情绪记录总结
     */
    @Operation(summary = "生成用户当天的情绪记录总结", description = "基于用户当天的聊天记录生成情绪分析和记录")
    @PostMapping(value = "/generate")
    public Result<EmotionSummaryGenerateResponse> generateEmotionSummary(
            @RequestBody @Valid EmotionSummaryGenerateRequest request) {
        String userId = UserContextUtils.requireCurrentUserId();
        log.info("收到生成情绪记录总结请求: userId={}", userId);

        // 调用AI服务生成情绪总结
        EmotionSummaryGenerateResponse response = aiChatService.generateEmotionSummaryWithResponse(userId);

        if (response.getSuccess()) {
            log.info("情绪记录总结生成成功: userId={}", userId);
            return Result.success("情绪记录总结生成成功", response);
        } else {
            log.warn("情绪记录总结生成失败: userId={}, message={}", userId, response.getMessage());
            return Result.error(response.getMessage());
        }
    }

    /**
     * 获取用户情绪记录总结状态
     */
    @Operation(summary = "获取用户情绪记录总结状态", description = "检查用户今天是否已经生成过情绪记录")
    @GetMapping(value = "/status")
    public Result<EmotionSummaryStatusResponse> getEmotionSummaryStatus(
            @Validated EmotionSummaryStatusRequest request) {
        // 从上下文中获取当前用户ID
        String userId = UserContextUtils.requireCurrentUserId();
        log.info("查询用户情绪记录总结状态: userId={}", userId);

        // 调用AI服务获取状态信息
        EmotionSummaryStatusResponse response = aiChatService.getEmotionSummaryStatusWithResponse(userId);

        return Result.success(response);
    }
}