#!/bin/bash

# shellcheck shell=bash

# Create worktree from remote branch
bridge_create_worktree_from_remote() {
  if [[ -z "${1:-}" ]]; then
    json_error "Branch name required"
    return 1
  fi
  branch_name="$1"
  remote_name="${2:-origin}"

  if git_require_repo_root >/dev/null 2>&1; then
    if git_worktree_exists_for_branch "$branch_name"; then
      json_error "Worktree already exists for branch: $branch_name"
      return 1
    fi

    if worktree_path="$(git_create_worktree_from_remote_branch "$branch_name" "$remote_name")"; then
      echo "\"$worktree_path\""
    else
      json_error "Failed to create worktree from remote branch: $remote_name/$branch_name"
    fi
  else
    json_error "Not a git repository"
  fi
}