package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.TopicInteractionCreateRequest;
import com.emotion.dto.request.TopicInteractionPageRequest;
import com.emotion.dto.request.TopicInteractionUpdateRequest;
import com.emotion.dto.response.TopicInteractionResponse;
import com.emotion.service.TopicInteractionService;
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-09-08
 */
@RestController
@RequestMapping("/topicInteraction")
public class TopicInteractionController {

    @Autowired
    private TopicInteractionService topicInteractionService;

    /**
     * 分页查询话题互动
     */
    @GetMapping(value = "/page")
    public Result<PageResult<TopicInteractionResponse>> getPage(@Validated TopicInteractionPageRequest request) {
        PageResult<TopicInteractionResponse> pageResult = topicInteractionService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取话题互动
     */
    @GetMapping(value = "/detail")
    public Result<TopicInteractionResponse> getById(@RequestParam String id) {
        TopicInteractionResponse response = topicInteractionService.getTopicInteractionResponseById(id);
        if (response == null) {
            return Result.notFound("话题互动不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建话题互动
     */
    @PostMapping(value = "/create")
    public Result<TopicInteractionResponse> create(@Valid @RequestBody TopicInteractionCreateRequest request) {
        TopicInteractionResponse response = topicInteractionService.createTopicInteractionWithResponse(request);
        return Result.success(response);
    }

    /**
     * 更新话题互动
     */
    @PutMapping(value = "/update")
    public Result<TopicInteractionResponse> update(@Valid @RequestBody TopicInteractionUpdateRequest request) {
        TopicInteractionResponse response = topicInteractionService.updateTopicInteractionWithResponse(request);
        if (response == null) {
            return Result.notFound("话题互动不存在");
        }
        return Result.success(response);
    }

    /**
     * 删除话题互动
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = topicInteractionService.deleteTopicInteraction(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}