package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.EmotionAnalysisCreateRequest;
import com.emotion.dto.request.EmotionAnalysisPageRequest;
import com.emotion.dto.request.EmotionAnalysisUpdateRequest;
import com.emotion.dto.response.EmotionAnalysisResponse;
import com.emotion.service.EmotionAnalysisService;
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;

/**
 * 情绪分析控制器
 * 
 * @author huazhongmin
 * @date 2025-07-23
 */
@RestController
@RequestMapping("/emotionAnalysis")
public class EmotionAnalysisController {

    @Autowired
    private EmotionAnalysisService emotionAnalysisService;

    /**
     * 分页查询情绪分析记录
     */
    @GetMapping(value = "/page")
    public Result<PageResult<EmotionAnalysisResponse>> getPage(@Validated EmotionAnalysisPageRequest request) {
        return Result.success(emotionAnalysisService.getPageWithResponse(request));
    }

    /**
     * 根据ID获取情绪分析记录
     */
    @GetMapping(value = "/detail")
    public Result<EmotionAnalysisResponse> getById(@RequestParam String id) {
        EmotionAnalysisResponse response = emotionAnalysisService.getEmotionAnalysisResponseById(id);
        if (response == null) {
            return Result.notFound("情绪分析记录不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建情绪分析记录
     */
    @PostMapping(value = "/create")
    public Result<EmotionAnalysisResponse> create(@RequestBody @Valid EmotionAnalysisCreateRequest request) {
        return Result.success(emotionAnalysisService.createEmotionAnalysisWithResponse(request));
    }

    /**
     * 更新情绪分析记录
     */
    @PutMapping(value = "/update")
    public Result<EmotionAnalysisResponse> update(@RequestBody @Valid EmotionAnalysisUpdateRequest request) {
        EmotionAnalysisResponse response = emotionAnalysisService.updateEmotionAnalysisWithResponse(request);
        if (response == null) {
            return Result.error("更新失败");
        }
        return Result.success(response);
    }

    /**
     * 删除情绪分析记录
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = emotionAnalysisService.deleteEmotionAnalysis(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}