package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.community.CommunityPostCreateRequest;
import com.emotion.dto.request.community.CommunityPostUpdateRequest;
import com.emotion.dto.request.community.CommunityPostPageRequest;
import com.emotion.dto.response.community.CommunityPostResponse;
import com.emotion.service.CommunityPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 社区帖子控制器
 * 
 * @author huazhongmin
 * @date 2025-07-23
 */
@RestController
@RequestMapping("/communityPost")
public class CommunityPostController {

    @Autowired
    private CommunityPostService communityPostService;

    /**
     * 分页查询帖子
     */
    @GetMapping(value = "/page")
    public Result<PageResult<CommunityPostResponse>> getPage(@Validated CommunityPostPageRequest request) {
        PageResult<CommunityPostResponse> pageResult = communityPostService.getPage(request);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取帖子
     */
    @GetMapping(value = "/detail")
    public Result<CommunityPostResponse> getById(@RequestParam String id) {
        CommunityPostResponse response = communityPostService.getById(id);
        if (response == null) {
            return Result.notFound("帖子不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建帖子
     */
    @PostMapping(value = "/create")
    public Result<CommunityPostResponse> create(@RequestBody @Validated CommunityPostCreateRequest request) {
        CommunityPostResponse response = communityPostService.create(request);
        return Result.success(response);
    }

    /**
     * 更新帖子
     */
    @PutMapping(value = "/update")
    public Result<CommunityPostResponse> update(@RequestBody @Validated CommunityPostUpdateRequest request) {
        CommunityPostResponse response = communityPostService.update(request);
        return Result.success(response);
    }

    /**
     * 删除帖子
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = communityPostService.delete(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}