#!/bin/bash

# shellcheck shell=bash

# Delete worktree and branch
bridge_delete_worktree() {
  if [[ -z "${1:-}" ]]; then
    json_error "Branch name required"
    return 1
  fi
  branch_name="$1"
  
  if git_require_repo_root >/dev/null 2>&1; then
    if git_worktree_exists_for_branch "$branch_name"; then
      git_remove_worktree "$branch_name"
      if git_branch_exists "$branch_name"; then
        git_delete_branch "$branch_name"
      fi
      echo "true"
    else
      json_error "No worktree found for branch: $branch_name"
    fi
  else
    json_error "Not a git repository"
  fi
}