package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.growth.GrowthTopicCreateRequest;
import com.emotion.dto.request.growth.GrowthTopicPageRequest;
import com.emotion.dto.request.growth.GrowthTopicUpdateRequest;
import com.emotion.dto.response.growth.GrowthTopicResponse;
import com.emotion.service.GrowthTopicService;
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("/growthTopic")
public class GrowthTopicController {

    @Autowired
    private GrowthTopicService growthTopicService;

    /**
     * 分页查询成长话题
     */
    @GetMapping(value = "/page")
    public Result<PageResult<GrowthTopicResponse>> getPage(@Validated GrowthTopicPageRequest request) {
        PageResult<GrowthTopicResponse> pageResult = growthTopicService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取成长话题
     */
    @GetMapping(value = "/detail")
    public Result<GrowthTopicResponse> getById(@RequestParam String id) {
        GrowthTopicResponse response = growthTopicService.getGrowthTopicResponseById(id);
        if (response == null) {
            return Result.notFound("成长话题不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建成长话题
     */
    @PostMapping(value = "/create")
    public Result<GrowthTopicResponse> create(@Valid @RequestBody GrowthTopicCreateRequest request) {
        GrowthTopicResponse response = growthTopicService.createGrowthTopicWithResponse(request);
        return Result.success(response);
    }

    /**
     * 更新成长话题
     */
    @PutMapping(value = "/update")
    public Result<GrowthTopicResponse> update(@Valid @RequestBody GrowthTopicUpdateRequest request) {
        GrowthTopicResponse response = growthTopicService.updateGrowthTopicWithResponse(request);
        if (response == null) {
            return Result.notFound("成长话题不存在");
        }
        return Result.success(response);
    }

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