//! Tauri-specific extensions for yaak-git.
//!
//! This module provides the Tauri commands for git functionality.

use crate::error::Result;
use std::path::{Path, PathBuf};
use yaak_git::{
    BranchDeleteResult, CloneResult, GitBranchInfo, GitCommit, GitFileDiff, GitRemote,
    GitStatusSummary, GitWorktreeStatus, PullResult, PushResult, git_add, git_add_credential,
    git_add_remote, git_branch_info, git_checkout_branch, git_clone, git_commit, git_create_branch,
    git_delete_branch, git_delete_remote_branch, git_fetch_all, git_file_diff_for_commit, git_init,
    git_log, git_log_for_file, git_merge_branch, git_pull, git_pull_force_reset, git_pull_merge,
    git_push, git_remotes, git_rename_branch, git_reset_changes, git_restore,
    git_restore_file_from_commit, git_rm_remote, git_status, git_unstage, git_worktree_status,
};

// NOTE: All of these commands are async to prevent blocking work from locking up the UI

pub async fn cmd_git_checkout(dir: &Path, branch: &str, force: bool) -> Result<String> {
    Ok(git_checkout_branch(dir, branch, force).await?)
}

pub async fn cmd_git_branch(dir: &Path, branch: &str, base: Option<&str>) -> Result<()> {
    Ok(git_create_branch(dir, branch, base).await?)
}

pub async fn cmd_git_delete_branch(
    dir: &Path,
    branch: &str,
    force: Option<bool>,
) -> Result<BranchDeleteResult> {
    Ok(git_delete_branch(dir, branch, force.unwrap_or(false)).await?)
}

pub async fn cmd_git_delete_remote_branch(dir: &Path, branch: &str) -> Result<()> {
    Ok(git_delete_remote_branch(dir, branch).await?)
}

pub async fn cmd_git_merge_branch(dir: &Path, branch: &str) -> Result<()> {
    Ok(git_merge_branch(dir, branch).await?)
}

pub async fn cmd_git_rename_branch(dir: &Path, old_name: &str, new_name: &str) -> Result<()> {
    Ok(git_rename_branch(dir, old_name, new_name).await?)
}

pub async fn cmd_git_status(dir: &Path) -> Result<GitStatusSummary> {
    Ok(git_status(dir)?)
}

pub async fn cmd_git_branch_info(dir: &Path) -> Result<GitBranchInfo> {
    Ok(git_branch_info(dir)?)
}

pub async fn cmd_git_worktree_status(dir: &Path) -> Result<GitWorktreeStatus> {
    Ok(git_worktree_status(dir)?)
}

pub async fn cmd_git_log(dir: &Path) -> Result<Vec<GitCommit>> {
    Ok(git_log(dir)?)
}

pub async fn cmd_git_log_for_file(dir: &Path, rela_path: PathBuf) -> Result<Vec<GitCommit>> {
    Ok(git_log_for_file(dir, &rela_path)?)
}

pub async fn cmd_git_file_diff_for_commit(
    dir: &Path,
    commit_oid: &str,
    rela_path: PathBuf,
) -> Result<GitFileDiff> {
    Ok(git_file_diff_for_commit(dir, commit_oid, &rela_path)?)
}

pub async fn cmd_git_initialize(dir: &Path) -> Result<()> {
    Ok(git_init(dir)?)
}

pub async fn cmd_git_clone(url: &str, dir: &Path) -> Result<CloneResult> {
    Ok(git_clone(url, dir).await?)
}

pub async fn cmd_git_commit(dir: &Path, message: &str) -> Result<()> {
    Ok(git_commit(dir, message).await?)
}

pub async fn cmd_git_fetch_all(dir: &Path) -> Result<()> {
    Ok(git_fetch_all(dir).await?)
}

pub async fn cmd_git_push(dir: &Path) -> Result<PushResult> {
    Ok(git_push(dir).await?)
}

pub async fn cmd_git_pull(dir: &Path) -> Result<PullResult> {
    Ok(git_pull(dir).await?)
}

pub async fn cmd_git_pull_force_reset(
    dir: &Path,
    remote: &str,
    branch: &str,
) -> Result<PullResult> {
    Ok(git_pull_force_reset(dir, remote, branch).await?)
}

pub async fn cmd_git_pull_merge(dir: &Path, remote: &str, branch: &str) -> Result<PullResult> {
    Ok(git_pull_merge(dir, remote, branch).await?)
}

pub async fn cmd_git_add(dir: &Path, rela_paths: Vec<PathBuf>) -> Result<()> {
    for path in rela_paths {
        git_add(dir, &path)?;
    }
    Ok(())
}

pub async fn cmd_git_unstage(dir: &Path, rela_paths: Vec<PathBuf>) -> Result<()> {
    for path in rela_paths {
        git_unstage(dir, &path)?;
    }
    Ok(())
}

pub async fn cmd_git_reset_changes(dir: &Path) -> Result<()> {
    Ok(git_reset_changes(dir).await?)
}

pub async fn cmd_git_restore_files(dir: &Path, rela_paths: Vec<PathBuf>) -> Result<()> {
    for path in rela_paths {
        git_restore(dir, &path)?;
    }
    Ok(())
}

pub async fn cmd_git_restore_file_from_commit(
    dir: &Path,
    commit_oid: &str,
    rela_path: PathBuf,
) -> Result<()> {
    Ok(git_restore_file_from_commit(dir, commit_oid, &rela_path)?)
}

pub async fn cmd_git_add_credential(
    remote_url: &str,
    username: &str,
    password: &str,
) -> Result<()> {
    Ok(git_add_credential(remote_url, username, password).await?)
}

pub async fn cmd_git_remotes(dir: &Path) -> Result<Vec<GitRemote>> {
    Ok(git_remotes(dir)?)
}

pub async fn cmd_git_add_remote(dir: &Path, name: &str, url: &str) -> Result<GitRemote> {
    Ok(git_add_remote(dir, name, url)?)
}

pub async fn cmd_git_rm_remote(dir: &Path, name: &str) -> Result<()> {
    Ok(git_rm_remote(dir, name)?)
}
