package org.agt.module.license.controller.admin.comment;

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.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.agt.framework.apilog.core.annotation.ApiAccessLog;
import org.agt.framework.common.pojo.CommonResult;
import org.agt.framework.common.util.object.BeanUtils;
import org.agt.framework.excel.core.util.ExcelUtils;
import org.agt.module.license.controller.admin.comment.vo.CommentListReqVO;
import org.agt.module.license.controller.admin.comment.vo.CommentRespVO;
import org.agt.module.license.controller.admin.comment.vo.CommentSaveReqVO;
import org.agt.module.license.controller.admin.comment.vo.CommentTreeRespVO;
import org.agt.module.license.dal.dataobject.comment.CommentDO;
import org.agt.module.license.service.comment.CommentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static org.agt.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static org.agt.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 评论")
@RestController
@RequestMapping("/license/comment")
@Validated
public class CommentController {

    @Resource
    private CommentService commentService;

    @PostMapping("/create")
    @Operation(summary = "创建评论")
    @PreAuthorize("@ss.hasPermission('license:project:comment')")
    public CommonResult<Long> createComment(@Valid @RequestBody CommentSaveReqVO createReqVO) {
        return success(commentService.createComment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新评论")
//    @PreAuthorize("@ss.hasPermission('license:comment:update')")
    public CommonResult<Boolean> updateComment(@Valid @RequestBody CommentSaveReqVO updateReqVO) {
        commentService.updateComment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评论")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('license:comment:delete')")
    public CommonResult<Boolean> deleteComment(@RequestParam("id") Long id) {
        commentService.deleteComment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('license:comment:query')")
    public CommonResult<CommentRespVO> getComment(@RequestParam("id") Long id) {
        CommentDO comment = commentService.getComment(id);
        return success(BeanUtils.toBean(comment, CommentRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得评论列表")
    @PreAuthorize("@ss.hasPermission('license:comment:query')")
    public CommonResult<List<CommentRespVO>> getCommentList(@Valid CommentListReqVO listReqVO) {
        List<CommentDO> list = commentService.getCommentList(listReqVO);
        return success(BeanUtils.toBean(list, CommentRespVO.class));
    }

    @GetMapping("/tree")
    @Operation(summary = "获得评论树形列表")
    public CommonResult<List<CommentTreeRespVO>> getCommentTree(@RequestParam("projectId") Long projectId, @RequestParam(value = "sort", required = false) Boolean sort) {
        List<CommentTreeRespVO> list = commentService.getCommentTree(projectId, sort);
        return success(list);
    }

    @GetMapping("/latest")
    @Operation(summary = "获得最新的评论列表")
    public CommonResult<List<CommentTreeRespVO>> getLatestCommentList() {
        List<CommentTreeRespVO> list = commentService.getLatestCommentList();
        return success(list);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出评论 Excel")
    @PreAuthorize("@ss.hasPermission('license:comment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCommentExcel(@Valid CommentListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<CommentDO> list = commentService.getCommentList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "评论.xls", "数据", CommentRespVO.class,
                        BeanUtils.toBean(list, CommentRespVO.class));
    }

}