package com.emotion.controller;

import com.emotion.common.Result;
import com.emotion.dto.request.AiChatRequest;
import com.emotion.dto.request.AiSummaryRequest;
import com.emotion.dto.request.GuestChatRequest;
import com.emotion.dto.request.ConversationCreateRequest;
import com.emotion.dto.request.ChatStatsRequest;
import com.emotion.dto.response.AiChatResponse;
import com.emotion.dto.response.AiSummaryResponse;
import com.emotion.dto.response.AiStatusResponse;
import com.emotion.dto.response.ChatStatsResponse;
import com.emotion.dto.response.GuestChatResponse;
import com.emotion.dto.response.GuestUserInfoResponse;
import com.emotion.dto.response.ConversationResponse;
import com.emotion.service.AiChatService;
import com.emotion.util.UserContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * AI聊天控制器
 * 
 * @author huazhongmin
 * @date 2025-07-23
 */
@Slf4j
@RestController
@RequestMapping("/ai")
public class AiChatController {
    
    @Autowired
    private AiChatService aiChatService;

    /**
     * 发送聊天消息
     */
    @PostMapping("/chat")
    public Result<AiChatResponse> sendChatMessage(@Valid @RequestBody AiChatRequest request) {
        log.info("收到AI聊天请求: conversationId={}, userId={}, message={}",
                request.getConversationId(), request.getUserId(), request.getMessage());

        // 调用AI服务
        AiChatResponse response = aiChatService.sendChatMessage(request);
        return Result.success(response);
    }
    
    /**
     * 生成对话总结
     */
    @PostMapping("/summary")
    public Result<AiSummaryResponse> generateSummary(@Valid @RequestBody AiSummaryRequest request) {
        log.info("收到对话总结请求: conversationId={}, userId={}", request.getConversationId(), request.getUserId());

        // 调用AI总结服务
        AiSummaryResponse response = aiChatService.generateConversationSummary(request);
        return Result.success(response);
    }
    
    /**
     * 获取AI服务状态
     */
    @GetMapping("/status")
    public Result<AiStatusResponse> getServiceStatus() {
        log.info("获取AI服务状态");

        AiStatusResponse response = aiChatService.getServiceStatus();
        return Result.success(response);
    }
    
    /**
     * 获取聊天记录统计
     */
    @GetMapping("/stats")
    public Result<ChatStatsResponse> getChatStats(@Valid ChatStatsRequest request) {
        log.info("获取聊天统计: userId={}, conversationId={}", request.getUserId(), request.getConversationId());

        ChatStatsResponse response = aiChatService.getChatStats(request);
        return Result.success(response);
    }

    /**
     * 访客聊天（不需要登录）
     */
    @PostMapping("/guestChat")
    public Result<GuestChatResponse> guestChat(@Valid @RequestBody GuestChatRequest request,
            HttpServletRequest httpRequest) {
        String clientIp = UserContextUtils.getClientIpAddress(httpRequest);
        log.info("访客聊天请求: {}, IP: {}", request.getMessage(), clientIp);

        GuestChatResponse response = aiChatService.guestChat(request, clientIp);
        return Result.success("发送成功", response);
    }

    /**
     * 获取访客用户信息
     */
    @GetMapping("/guestUserInfo")
    public Result<GuestUserInfoResponse> getGuestUserInfo(HttpServletRequest request) {
        String clientIp = UserContextUtils.getClientIpAddress(request);
        log.info("获取访客用户信息: IP={}", clientIp);

        GuestUserInfoResponse response = aiChatService.getGuestUserInfo(clientIp);
        return Result.success(response);
    }

    /**
     * 创建对话
     */
    @PostMapping("/createConversation")
    public Result<ConversationResponse> createConversation(@Valid @RequestBody ConversationCreateRequest request,
            HttpServletRequest httpRequest) {
        log.info("创建对话请求: userId={}, title={}", request.getUserId(), request.getTitle());

        String clientIp = UserContextUtils.getClientIpAddress(httpRequest);
        ConversationResponse response = aiChatService.createConversation(request, clientIp);
        return Result.success("创建成功", response);
    }
}