package com.njcn.rdms.module.system.controller.admin.feedback;

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.system.controller.admin.feedback.vo.FeedbackPageReqVO;
import com.njcn.rdms.module.system.controller.admin.feedback.vo.FeedbackRespVO;
import com.njcn.rdms.module.system.controller.admin.feedback.vo.FeedbackSaveReqVO;
import com.njcn.rdms.module.system.controller.admin.feedback.vo.FeedbackStatRespVO;
import com.njcn.rdms.module.system.controller.admin.feedback.vo.FeedbackStatusReqVO;
import com.njcn.rdms.module.system.controller.admin.feedback.vo.FeedbackUpdateReqVO;
import com.njcn.rdms.module.system.service.feedback.FeedbackService;
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 org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.njcn.rdms.framework.common.pojo.CommonResult.success;

// 临时功能：意见反馈过测试阶段即下线。全部接口均「有意」不挂后端鉴权（仅过 token，需登录），
// 按钮可见性（仅 admin 可见）由前端控制；并非漏挂。如需恢复鉴权，给对应接口加回
// @PreAuthorize("@ss.hasPermission('system:feedback:xxx')") 并配 system_menu 权限码 + 角色授权。
@Tag(name = "管理后台 - 用户意见反馈")
@RestController
@RequestMapping("/system/feedback")
@Validated
public class FeedbackController {

    @Resource
    private FeedbackService feedbackService;

    @PostMapping("/create")
    @Operation(summary = "提交意见反馈")
    public CommonResult<Long> createFeedback(@Valid @RequestBody FeedbackSaveReqVO reqVO) {
        return success(feedbackService.createFeedback(reqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改意见反馈")
    public CommonResult<Boolean> updateFeedback(@Valid @RequestBody FeedbackUpdateReqVO reqVO) {
        feedbackService.updateFeedback(reqVO);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得意见反馈分页（全量）")
    public CommonResult<PageResult<FeedbackRespVO>> getFeedbackPage(@Valid FeedbackPageReqVO pageReqVO) {
        return success(feedbackService.getFeedbackPage(pageReqVO));
    }

    @GetMapping("/stat")
    @Operation(summary = "意见反馈统计（全量：总数 + 按分类 / 按状态分组计数）")
    public CommonResult<FeedbackStatRespVO> getFeedbackStat() {
        return success(feedbackService.getFeedbackStat());
    }

    @GetMapping("/get")
    @Operation(summary = "获得意见反馈详情")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<FeedbackRespVO> getFeedback(@RequestParam("id") Long id) {
        return success(feedbackService.getFeedback(id));
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "修改意见反馈处理状态")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> updateFeedbackStatus(@PathVariable("id") Long id,
                                                      @Valid @RequestBody FeedbackStatusReqVO reqVO) {
        feedbackService.updateFeedbackStatus(id, reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除意见反馈")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteFeedback(@RequestParam("id") Long id) {
        feedbackService.deleteFeedback(id);
        return success(true);
    }

}
