const invoke = window.__TAURI__?.core?.invoke;
const dialog = window.__TAURI__?.dialog;

function ensureInvoke() {
  if (!invoke) {
    throw new Error("Tauri invoke API is not available.");
  }
}

function ensureDialog() {
  if (!dialog?.open) {
    throw new Error("Tauri dialog API is not available.");
  }
}

export async function browseDirectory(defaultPath = "") {
  ensureDialog();
  return dialog.open({
    directory: true,
    multiple: false,
    defaultPath: defaultPath || undefined,
  });
}

export async function browseApplication(defaultPath = "") {
  ensureDialog();
  return dialog.open({
    directory: false,
    multiple: false,
    defaultPath: defaultPath || undefined,
    filters: [
      { name: "Applications", extensions: ["exe", "cmd", "bat", "app"] },
    ],
  });
}

export async function runGitClone(repoUrl, destinationPath, gitPath) {
  ensureInvoke();
  return invoke("git_clone", {
    repoUrl,
    destinationPath,
    gitPath: gitPath || null,
  });
}

export async function runGitPull(repoPath, gitPath) {
  ensureInvoke();
  return invoke("git_pull", { repoPath, gitPath: gitPath || null });
}

export async function runGitPush(repoPath, gitPath) {
  ensureInvoke();
  return invoke("git_push", { repoPath, gitPath: gitPath || null });
}

export async function runGitPublishBranch(repoPath, remote, gitPath) {
  ensureInvoke();
  return invoke("git_publish_branch", { repoPath, remote: remote || null, gitPath: gitPath || null });
}

export async function runGitFetch(repoPath, gitPath) {
  ensureInvoke();
  return invoke("git_fetch", { repoPath, gitPath: gitPath || null });
}

export async function runGitSync(repoPath, gitPath) {
  ensureInvoke();
  return invoke("git_sync", { repoPath, gitPath: gitPath || null });
}

export async function runGitStatus(repoPath, gitPath) {
  ensureInvoke();
  return invoke("git_status", { repoPath, gitPath: gitPath || null });
}

export async function runGitBranch(repoPath, gitPath) {
  ensureInvoke();
  return invoke("git_branch", { repoPath, gitPath: gitPath || null });
}

export async function getWorkingTreeStatus(repoPath, gitPath) {
  ensureInvoke();
  return invoke("working_tree_status", { repoPath, gitPath: gitPath || null });
}

export async function getRepositorySyncStatus(repoPath, gitPath) {
  ensureInvoke();
  return invoke("repository_sync_status", { repoPath, gitPath: gitPath || null });
}

export async function getFileDiff(repoPath, path, status, gitPath) {
  ensureInvoke();
  return invoke("get_file_diff", { repoPath, path, status, gitPath: gitPath || null });
}

export async function stageFiles(repoPath, paths, gitPath) {
  ensureInvoke();
  return invoke("stage_files", { repoPath, paths, gitPath: gitPath || null });
}

export async function unstageFiles(repoPath, paths, gitPath) {
  ensureInvoke();
  return invoke("unstage_files", { repoPath, paths, gitPath: gitPath || null });
}

export async function commitChanges(repoPath, paths, summary, description, gitPath) {
  ensureInvoke();
  return invoke("commit_changes", {
    repoPath,
    paths,
    summary,
    description: description || null,
    gitPath: gitPath || null,
  });
}

export async function checkoutBranch(repoPath, branch, gitPath) {
  ensureInvoke();
  return invoke("checkout_branch", { repoPath, branch, gitPath: gitPath || null });
}

export async function createBranch(repoPath, branch, gitPath) {
  ensureInvoke();
  return invoke("create_branch", { repoPath, branch, gitPath: gitPath || null });
}

export async function deleteBranch(repoPath, branch, force, gitPath) {
  ensureInvoke();
  return invoke("delete_branch", { repoPath, branch, force: !!force, gitPath: gitPath || null });
}

export async function renameBranch(repoPath, oldBranch, newBranch, gitPath) {
  ensureInvoke();
  return invoke("rename_branch", {
    repoPath,
    oldBranch,
    newBranch,
    gitPath: gitPath || null,
  });
}

export async function getCommitHistory(repoPath, limit, gitPath) {
  ensureInvoke();
  return invoke("commit_history", { repoPath, limit, gitPath: gitPath || null });
}

export async function getCommitDetail(repoPath, hash, gitPath) {
  ensureInvoke();
  return invoke("commit_detail", { repoPath, hash, gitPath: gitPath || null });
}

export async function openInFileExplorer(repoPath) {
  ensureInvoke();
  return invoke("open_in_file_explorer", { repoPath });
}

export async function openInExternalEditor(repoPath, editorPath) {
  ensureInvoke();
  return invoke("open_in_external_editor", { repoPath, editorPath });
}

export async function scanInstalledIdes() {
  ensureInvoke();
  return invoke("scan_installed_ides");
}

export async function scanLocalRepos(roots = [], allowedRemoteUrls = [], gitPath = "", maxDepth = 4, maxResults = 200) {
  ensureInvoke();
  return invoke("scan_local_repos", {
    roots,
    allowedRemoteUrls,
    gitPath: gitPath || null,
    maxDepth,
    maxResults,
  });
}

export async function listLocalRepoBranches(repoPath, gitPath) {
  ensureInvoke();
  return invoke("local_repo_branches", { repoPath, gitPath: gitPath || null });
}

export async function listLocalRepoTree(repoPath, reference, path, gitPath) {
  ensureInvoke();
  return invoke("local_repo_tree", {
    repoPath,
    reference,
    path: path || "",
    gitPath: gitPath || null,
  });
}

export async function readLocalRepoFile(repoPath, reference, path, gitPath) {
  ensureInvoke();
  return invoke("local_repo_file", {
    repoPath,
    reference,
    path,
    gitPath: gitPath || null,
  });
}

export async function testGiteaConnection(payload) {
  ensureInvoke();
  return invoke("test_gitea_connection", payload);
}
