import { $ } from "bun";

export async function isGitRepo(): Promise<boolean> {
  try {
    await $`git rev-parse --git-dir`.quiet();
    return true;
  } catch {
    return false;
  }
}

export async function getStagedDiff(): Promise<string> {
  try {
    const result = await $`git diff --cached`.text();
    return result.trim();
  } catch (error) {
    console.error("Failed to get git diff:", error);
    return "";
  }
}

export async function hasStagedChanges(): Promise<boolean> {
  try {
    const result = await $`git diff --cached --name-only`.text();
    return result.trim().length > 0;
  } catch {
    return false;
  }
}

export async function createCommit(message: string): Promise<void> {
  try {
    await $`git commit -m ${message}`.quiet();
  } catch (error) {
    throw new Error(`Failed to create commit: ${error}`);
  }
}

export async function getCurrentBranch(): Promise<string> {
  try {
    const result = await $`git branch --show-current`.text();
    return result.trim();
  } catch {
    return "unknown";
  }
}