package com.microservices.pms.project.controller;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.pms.project.service.PmsForgeApiForwardService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.microservices.common.core.web.domain.AjaxResult.success;

@RestController
@RequestMapping("/{enterpriseIdentifier}/forge")
@Api(tags = "项目管理-直接转发forge相关接口")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String")
})
public class PmsForgeApiForwardController {

    @Autowired
    private PmsForgeApiForwardService pmsForgeApiForwardService;

    @GetMapping("/projects")
    @ApiOperation("代码库列表")
    @ApiParam(name = "projectSearchInputVo",
            value = "【字段说明】:更多入参参考forge接口文档\n" +
                    "page:页码，默认为1;\n" +
                    "limit:每页记录数，默认为15;\n" +
                    "sort_by:排序字段，如updated_on;\n" +
                    "sort_direction:排序方向，asc升序或desc降序;\n" +
                    "is_public:是否公开项目，true或false;\n" +
                    "search:代码库名称关键字")
    public AjaxResult listProjects(@PathVariable String enterpriseIdentifier,
                                 @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                 @RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit,
                                 @RequestParam(value = "sort_by", required = false) String sortBy,
                                 @RequestParam(value = "sort_direction", required = false) String sortDirection,
                                 @RequestParam(value = "is_public", required = false) Boolean isPublic,
                                 @RequestParam(value = "keyword", required = false) String search) {
        JSONObject projectSearchInputVo = new JSONObject();
        projectSearchInputVo.put("page", page);
        projectSearchInputVo.put("limit", limit);
        projectSearchInputVo.put("sort_by", sortBy);
        projectSearchInputVo.put("sort_direction", sortDirection);
        projectSearchInputVo.put("is_public", isPublic);
        projectSearchInputVo.put("search", search);
        JSONObject result = pmsForgeApiForwardService.selectForgeRepositoryList(enterpriseIdentifier, projectSearchInputVo);
        return success(result);
    }

    @GetMapping("{owner}/{repo}/project")
    @ApiOperation("代码库详情")
    public AjaxResult getProject(@ApiParam(name = "repo", value = "仓库标识") @PathVariable String repo,
                               @ApiParam(name = "owner", value = "仓库拥有者") @PathVariable String owner) {
        JSONObject result = pmsForgeApiForwardService.selectForgeProject(repo, owner);
        return success(result);
    }

    @GetMapping("{owner}/{repo}/issues")
    @ApiOperation("疑修列表")
    @ApiParam(name = "issueSearch",
            value = "【字段说明】:更多入参参考forge接口文档\n" +
                    "page:页码，默认为1;\n" +
                    "limit:每页记录数，默认为15;\n" +
                    "sort_by:排序字段，如updated_on;\n" +
                    "sort_direction:排序方向，asc升序或desc降序;\n" +
                    "participant_category:参与类型，all 全部 aboutme 关于我的 authoredme 我创建的 assignedme 我负责的 atme @我的;\n" +
                    "category:疑修类型，all 全部 opened 开启中 closed 已关闭;"
    )
    public AjaxResult listIssues(@ApiParam(name = "repo", value = "仓库标识") @PathVariable String repo,
                                 @ApiParam(name = "owner", value = "仓库拥有者") @PathVariable String owner,
                                 @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                 @RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit,
                                 @RequestParam(value = "sort_by", required = false) String sortBy,
                                 @RequestParam(value = "sort_direction", required = false) String sortDirection,
                                 @RequestParam(value = "participant_category", required = false) String participantCategory,
                                 @RequestParam(value = "category", required = false) String category,
                                 @RequestParam(value = "author_id", required = false) String author_id,
                                 @RequestParam(value = "issue_tag_ids", required = false) String issue_tag_ids,
                                 @RequestParam(value = "assigner_id", required = false) String assigner_id,
                                 @RequestParam(value = "status_id", required = false) String status_id,
                                 @RequestParam(value = "priority_id", required = false) String priority_id,
                                 @RequestParam(value = "keyword", required = false) String keyword) {
        JSONObject issueSearch = new JSONObject();
        issueSearch.put("page", page);
        issueSearch.put("limit", limit);
        issueSearch.put("sort_by", sortBy);
        issueSearch.put("sort_direction", sortDirection);
        issueSearch.put("participant_category", participantCategory);
        issueSearch.put("category", category);
        issueSearch.put("author_id", author_id);
        issueSearch.put("issue_tag_ids", issue_tag_ids);
        issueSearch.put("assigner_id", assigner_id);
        issueSearch.put("status_id", status_id);
        issueSearch.put("priority_id", priority_id);
        issueSearch.put("keyword", keyword);
        JSONObject result = pmsForgeApiForwardService.selectForgeIssueList(owner, repo, issueSearch);
        return success(result);
    }

    @GetMapping("{owner}/{repo}/issues/{issue_id}")
    @ApiOperation("疑修详情")
    public AjaxResult getIssue(@ApiParam(name = "repo", value = "仓库标识") @PathVariable String repo,
                               @ApiParam(name = "owner", value = "仓库拥有者") @PathVariable String owner,
                               @ApiParam(name = "issue_id", value = "疑修id") @PathVariable String issue_id) {
        JSONObject result = pmsForgeApiForwardService.selectForgeIssue(repo, owner, issue_id);
        return success(result);
    }

    @PatchMapping("{owner}/{repo}/issues/{issue_id}")
    @ApiOperation("更新疑修")
    public AjaxResult updateIssue(@ApiParam(name = "repo", value = "仓库标识") @PathVariable String repo,
                                  @ApiParam(name = "owner", value = "仓库拥有者") @PathVariable String owner,
                                  @ApiParam(name = "issue_id", value = "疑修id") @PathVariable String issue_id,
                                  @Validated @RequestBody JSONObject issue) {
        JSONObject result = pmsForgeApiForwardService.updateForgeIssue(repo, owner, issue_id, issue);
        return success(result);
    }

    @GetMapping("{owner}/{repo}/{issue_url}")
    @ApiOperation("疑修相关枚举")
    public AjaxResult getIssueEnum(@ApiParam(name = "repo", value = "仓库标识") @PathVariable String repo,
                                 @ApiParam(name = "owner", value = "仓库拥有者") @PathVariable String owner,
                                   @ApiParam(name = "issue_url", value = "疑修url") @PathVariable String issue_url,
                                   @ApiParam(name = "only_name", value = "是否只返回名称") Boolean only_name) {
        JSONObject result = pmsForgeApiForwardService.getIssueRelatedEnum(repo, owner, issue_url, only_name);
        return success(result);
    }

    @DeleteMapping("{owner}/{repo}/issues/{issue_id}")
    @ApiOperation("删除疑修")
    public AjaxResult deleteIssue(@ApiParam(name = "repo", value = "仓库标识") @PathVariable String repo,
                               @ApiParam(name = "owner", value = "仓库拥有者") @PathVariable String owner,
                               @ApiParam(name = "issue_id", value = "疑修id") @PathVariable String issue_id) {
        JSONObject result = pmsForgeApiForwardService.deleteForgeIssue(repo, owner, issue_id);
        return success(result);
    }

    @PostMapping("{owner}/{repo}/issues/{issue_id}/journals")
    @ApiOperation("创建疑修评论")
    public AjaxResult createIssueComment(@ApiParam(name = "repo", value = "仓库标识") @PathVariable String repo,
                                         @ApiParam(name = "owner", value = "仓库拥有者") @PathVariable String owner,
                                         @ApiParam(name = "issue_id", value = "疑修id") @PathVariable Long issue_id,
                                         @Validated @RequestBody JSONObject issue) {
        JSONObject result = pmsForgeApiForwardService.createIssueComment(repo, owner, issue_id, issue);
        return success(result);
    }

    @GetMapping("{owner}/{repo}/issues/{issue_id}/journals")
    @ApiOperation("获取疑修评论列表")
    public AjaxResult getIssueCommentList(@ApiParam(name = "repo", value = "仓库标识") @PathVariable String repo,
                                          @ApiParam(name = "owner", value = "仓库拥有者") @PathVariable String owner,
                                          @ApiParam(name = "issue_id", value = "疑修id") @PathVariable Long issue_id,
                                          @ApiParam(name = "category", value = "评论类型") String category,
                                          @ApiParam(name = "page", value = "页码") Integer page,
                                          @ApiParam(name = "limit", value = "每页记录数") Integer limit
    ) {
        JSONObject result = pmsForgeApiForwardService.getIssueCommentList(repo, owner, issue_id, category, page, limit);
        return success(result);
    }

    @PatchMapping("{owner}/{repo}/issues/{issue_id}/journals/{journal_id}")
    @ApiOperation("修改疑修评论详情")
    public AjaxResult updateIssueComment(@ApiParam(name = "repo", value = "仓库标识") @PathVariable String repo,
                                         @ApiParam(name = "owner", value = "仓库拥有者") @PathVariable String owner,
                                         @ApiParam(name = "issue_id", value = "疑修id") @PathVariable Long issue_id,
                                         @ApiParam(name = "journal_id", value = "疑修评论id") @PathVariable Long journal_id,
                                         @RequestBody JSONObject issue) {
        JSONObject result = pmsForgeApiForwardService.updateIssueComment(repo, owner, issue_id, journal_id, issue);
        return success(result);
    }

    @DeleteMapping("{owner}/{repo}/issues/{issue_id}/journals/{journal_id}")
    @ApiOperation("删除疑修评论")
    public AjaxResult deleteIssueComment(@ApiParam(name = "repo", value = "仓库标识") @PathVariable String repo,
                                         @ApiParam(name = "owner", value = "仓库拥有者") @PathVariable String owner,
                                         @ApiParam(name = "issue_id", value = "疑修id") @PathVariable Long issue_id,
                                         @ApiParam(name = "journal_id", value = "疑修评论id") @PathVariable Long journal_id) {
        JSONObject result = pmsForgeApiForwardService.deleteIssueComment(repo, owner, issue_id, journal_id);
        return success(result);
    }

}
