//! Utility functions for Git command operations.
//!
//! Contains helper functions for URL conversion and other shared utilities.

/// Convert HTTPS GitHub URL to SSH format.
/// 
/// Example: `https://github.com/org/repo.git` -> `git@github.com:org/repo.git`
pub fn to_ssh_url(url: &str) -> Option<String> {
    if url.starts_with("git@") || url.starts_with("ssh://") {
        return Some(url.to_string());
    }
    // https://github.com/org/repo.git -> git@github.com:org/repo.git
    let trimmed = url.trim_end_matches(".git");
    let prefix = "https://";
    if trimmed.starts_with(prefix) {
        let rest = &trimmed[prefix.len()..];
        let parts: Vec<&str> = rest.split('/').collect();
        if parts.len() >= 2 {
            let host = parts[0];
            let path = parts[1..].join("/");
            return Some(format!("git@{}:{}.git", host, path));
        }
    }
    None
}

/// Convert remote URL to GitHub PR URL.
/// 
/// Supports both SSH and HTTPS GitHub URLs.
/// Example: `https://github.com/org/repo.git` + `"12"` -> `https://github.com/org/repo/pull/12`
pub fn to_github_pr_url(remote_url: &str, pr: &str) -> Option<String> {
    if remote_url.starts_with("git@github.com:") {
        let repo = remote_url.trim_start_matches("git@github.com:").trim_end_matches(".git");
        return Some(format!("https://github.com/{}/pull/{}", repo, pr));
    }
    if remote_url.starts_with("https://github.com/") {
        let repo = remote_url.trim_start_matches("https://github.com/").trim_end_matches(".git");
        return Some(format!("https://github.com/{}/pull/{}", repo, pr));
    }
    None
}

