package com.microservices.pms.project.service;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.httpClient.util.GitLinkRequestHelper;
import com.microservices.pms.utils.PmsGitLinkRequestUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URISyntaxException;

@Service
public class PmsForgeApiForwardService {

    @Autowired
    private GitLinkRequestHelper gitLinkRequestHelper;

    public JSONObject selectForgeRepositoryList(String enterpriseIdentifier, JSONObject projectSearchInputVo) {

        try {
            return gitLinkRequestHelper.doGet(PmsGitLinkRequestUrl.GET_ORGANIZATION_PUBLIC_REPO_LIST(enterpriseIdentifier, projectSearchInputVo));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public JSONObject selectForgeIssueList(String owner, String repo, JSONObject issueSearch) {
        try {
            return gitLinkRequestHelper.doGet(PmsGitLinkRequestUrl.GET_FORGE_ISSUE_LIST(owner, repo, issueSearch));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public JSONObject selectForgeIssue(String repo, String owner, String issueId) {

        return gitLinkRequestHelper.doGet(PmsGitLinkRequestUrl.GET_ISSUE_DETAILS(repo, owner, issueId));
    }

    public JSONObject updateForgeIssue(String repo, String owner, String issueId, JSONObject issue) {
        return gitLinkRequestHelper.doPatch(PmsGitLinkRequestUrl.UPDATE_ISSUE(repo, owner, issueId), issue);
    }

    public JSONObject selectForgeProject(String repo, String owner) {
        return gitLinkRequestHelper.doGet(PmsGitLinkRequestUrl.GET_PROJECT_DETAILS(repo, owner));
    }

    public JSONObject getIssueRelatedEnum(String repo, String owner, String issueUrl, Boolean only_name) {
        return gitLinkRequestHelper.doGet(PmsGitLinkRequestUrl.GET_ISSUE_RELATED_ENUM(repo, owner, issueUrl, only_name));
    }

    public JSONObject deleteForgeIssue(String repo, String owner, String issueId) {
        return gitLinkRequestHelper.doDelete(PmsGitLinkRequestUrl.DELETE_ISSUE(repo, owner, issueId));
    }

    public JSONObject createIssueComment(String repo, String owner, Long issueId, JSONObject issue) {
        return gitLinkRequestHelper.doPost(
                PmsGitLinkRequestUrl.ADD_REPO_ISSUE_JOURNAL(issueId, owner, repo), issue);
    }

    public JSONObject getIssueCommentList(String repo, String owner, Long issueId, String category, Integer page, Integer limit) {
        return gitLinkRequestHelper.doGet(PmsGitLinkRequestUrl.GET_ISSUE_COMMENT_LIST(issueId, owner, repo, category, page, limit));
    }

    public JSONObject updateIssueComment(String repo, String owner, Long issueId, Long journalId, JSONObject issue) {
        return gitLinkRequestHelper.doPatch(PmsGitLinkRequestUrl.UPDATE_ISSUE_COMMENT(issueId, owner, repo, journalId), issue);
    }

    public JSONObject deleteIssueComment(String repo, String owner, Long issueId, Long journalId) {
        return gitLinkRequestHelper.doDelete(PmsGitLinkRequestUrl.DELETE_ISSUE_COMMENT(issueId, owner, repo, journalId));
    }
}
