package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.EmotionRecordCreateRequest;
import com.emotion.dto.request.EmotionRecordPageRequest;
import com.emotion.dto.request.EmotionRecordUpdateRequest;
import com.emotion.dto.response.EmotionRecordResponse;
import com.emotion.service.EmotionRecordService;
import com.emotion.util.UserContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 情绪记录控制器
 *
 * @author huazhongmin
 * @date 2025-07-22
 */
@RestController
@RequestMapping("/emotionRecord")
public class EmotionRecordController {

    private static final Logger log = LoggerFactory.getLogger(EmotionRecordController.class);

    @Autowired
    private EmotionRecordService emotionRecordService;

    /**
     * 创建情绪记录
     */
    @PostMapping(value = "/create")
    public Result<EmotionRecordResponse> createRecord(@RequestBody @Valid EmotionRecordCreateRequest request) {
        log.info("创建情绪记录: userId={}", request.getUserId());
        
        // 从上下文中获取当前用户ID
        String userId = UserContextUtils.requireCurrentUserId();
        request.setUserId(userId);
        
        EmotionRecordResponse response = emotionRecordService.createEmotionRecordWithResponse(request);
        return Result.success("创建成功", response);
    }

    /**
     * 分页查询情绪记录
     */
    @GetMapping(value = "/page")
    public Result<PageResult<EmotionRecordResponse>> getPage(@Validated EmotionRecordPageRequest request) {
        // 从上下文中获取当前用户ID
        String userId = UserContextUtils.requireCurrentUserId();

        log.info("分页查询情绪记录: userId={}, current={}, size={}", userId, request.getCurrent(), request.getSize());

        PageResult<EmotionRecordResponse> page = emotionRecordService.getPageByUserIdWithResponse(userId, request);

        log.info("分页查询情绪记录成功: userId={}, total={}, records={}",
                        userId, page.getTotal(), page.getRecords().size());

        return Result.success(page);
    }

    /**
     * 获取情绪记录详情
     */
    @GetMapping(value = "/detail")
    public Result<EmotionRecordResponse> getRecord(@RequestParam String id) {
        log.info("获取情绪记录详情: {}", id);
        
        EmotionRecordResponse response = emotionRecordService.getEmotionRecordResponseById(id);
        if (response == null) {
            return Result.notFound("情绪记录不存在");
        }
        return Result.success(response);
    }

    /**
     * 更新情绪记录
     */
    @PutMapping(value = "/update")
    public Result<EmotionRecordResponse> updateRecord(@RequestBody @Valid EmotionRecordUpdateRequest request) {
        log.info("更新情绪记录: {}", request.getId());
        
        EmotionRecordResponse response = emotionRecordService.updateEmotionRecordWithResponse(request);
        if (response == null) {
            return Result.notFound("情绪记录不存在");
        }
        return Result.success("更新成功", response);
    }

    /**
     * 删除情绪记录
     */
    @DeleteMapping(value = "/delete")
    public Result<String> deleteRecord(@RequestParam String id) {
        log.info("删除情绪记录: {}", id);
        
        boolean deleted = emotionRecordService.deleteEmotionRecord(id);
        if (!deleted) {
            return Result.notFound("情绪记录不存在");
        }
        return Result.success("删除成功");
    }

    /**
     * 获取情绪统计
     */
    @GetMapping(value = "/stats")
    public Result<Map<String, Object>> getEmotionStats(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        
        // 从上下文中获取当前用户ID
        String userId = UserContextUtils.requireCurrentUserId();
        
        log.info("获取情绪统计: userId={}, startDate={}, endDate={}", userId, startDate, endDate);
        
        Map<String, Object> stats = emotionRecordService.getEmotionStats(userId, startDate, endDate);
        
        return Result.success(stats);
    }
}