import { request } from "./client";

// Connection settings
export const gitlabGetSettings = (token) =>
  request("GET", "/gitlab/settings", token);

export const gitlabUpdateSettings = (token, data) =>
  request("PUT", "/gitlab/settings", token, data);

export const gitlabTestConnection = (token) =>
  request("POST", "/gitlab/test-connection", token);

// Projects
export const gitlabSearchProjects = (token, search, owned) =>
  request("GET", `/gitlab/projects?search=${encodeURIComponent(search || "")}&owned=${owned || false}`, token);

export const gitlabCreateProject = (token, data) =>
  request("POST", "/gitlab/projects", token, data);

// Linked repos
export const gitlabListRepos = (token) =>
  request("GET", "/gitlab/repos", token);

export const gitlabLinkRepo = (token, gitlabProjectId) =>
  request("POST", "/gitlab/repos", token, { gitlab_project_id: gitlabProjectId });

export const gitlabUnlinkRepo = (token, repoId) =>
  request("DELETE", `/gitlab/repos/${repoId}`, token);

export const gitlabAnalyzeRepo = (token, repoId) =>
  request("POST", `/gitlab/repos/${repoId}/analyze`, token);

export const gitlabGetRepoMap = (token, repoId) =>
  request("GET", `/gitlab/repos/${repoId}/map`, token);

// Tree & files
export const gitlabGetTree = (token, repoId, path, ref) =>
  request("GET", `/gitlab/repos/${repoId}/tree?path=${encodeURIComponent(path || "")}&ref=${encodeURIComponent(ref || "")}`, token);

export const gitlabGetFile = (token, repoId, path, ref) =>
  request("GET", `/gitlab/repos/${repoId}/file?path=${encodeURIComponent(path)}&ref=${encodeURIComponent(ref || "")}`, token);

// Branches & commits
export const gitlabGetBranches = (token, repoId) =>
  request("GET", `/gitlab/repos/${repoId}/branches`, token);

export const gitlabCommitFiles = (token, repoId, data) =>
  request("POST", `/gitlab/repos/${repoId}/commit`, token, data);

export const gitlabCommitSingle = (token, repoId, data) =>
  request("POST", `/gitlab/repos/${repoId}/commit-single`, token, data);

export const gitlabCreateBranch = (token, repoId, data) =>
  request("POST", `/gitlab/repos/${repoId}/branches`, token, data);

export const gitlabCreateMergeRequest = (token, repoId, data) =>
  request("POST", `/gitlab/repos/${repoId}/merge-request`, token, data);

// Actions
export const gitlabListActions = (token, status) =>
  request("GET", `/gitlab/actions?status=${status || "pending"}`, token);

export const gitlabCreateAction = (token, data) =>
  request("POST", "/gitlab/actions", token, data);

export const gitlabApproveAction = (token, actionId) =>
  request("POST", `/gitlab/actions/${actionId}/approve`, token);

export const gitlabRejectAction = (token, actionId) =>
  request("POST", `/gitlab/actions/${actionId}/reject`, token);
