package com.xindazhou.business.controller.admin.community.topic;

import com.xindazhou.business.controller.admin.community.topic.vo.TopicPageReqVO;
import com.xindazhou.business.controller.admin.community.topic.vo.TopicRespVO;
import com.xindazhou.business.controller.admin.community.topic.vo.TopicSaveReqVO;
import com.xindazhou.business.service.community.topic.TopicService;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 话题 Controller
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 话题管理")
@RestController("communityTopicController")
@RequestMapping("/admin-api/community/topic")
@Validated
@Slf4j
public class TopicController {

    @Resource
    private TopicService topicService;

    @PostMapping("/create")
    @Operation(summary = "创建话题")
    @PreAuthorize("@ss.hasPermission('community:topic:create')")
    public CommonResult<Long> createTopic(@Valid @RequestBody TopicSaveReqVO createReqVO) {
        Long topicId = topicService.createTopic(createReqVO);
        return success(topicId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新话题")
    @PreAuthorize("@ss.hasPermission('community:topic:update')")
    public CommonResult<Boolean> updateTopic(@Valid @RequestBody TopicSaveReqVO updateReqVO) {
        topicService.updateTopic(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除话题")
    @Parameter(name = "id", description = "话题ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('community:topic:delete')")
    public CommonResult<Boolean> deleteTopic(@RequestParam("id") Long id) {
        topicService.deleteTopic(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得话题")
    @Parameter(name = "id", description = "话题ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('community:topic:query')")
    public CommonResult<TopicRespVO> getTopic(@RequestParam("id") Long id) {
        TopicRespVO topic = topicService.getTopic(id);
        return success(topic);
    }

    @GetMapping("/page")
    @Operation(summary = "获得话题分页列表")
    @PreAuthorize("@ss.hasPermission('community:topic:query')")
    public CommonResult<PageResult<TopicRespVO>> getTopicPage(@Valid TopicPageReqVO pageReqVO) {
        PageResult<TopicRespVO> pageResult = topicService.getTopicPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/list-enabled")
    @Operation(summary = "获得启用的话题列表")
    public CommonResult<List<TopicRespVO>> getEnabledTopicList() {
        List<TopicRespVO> topics = topicService.getEnabledTopicList();
        return success(topics);
    }

}

