# shellcheck shell=bash

cmd_copy_env() {
  local target_branch="${1-}"
  if [[ -z "$target_branch" ]]; then
    err "Target worktree name required"
    echo "Usage: gw copy-env <worktreename>"
    return 1
  fi

  git_require_repo || return 1

  local target_path
  target_path="$(git_branch_to_worktree_path "$target_branch")"
  if [[ -z "$target_path" ]]; then
    err "Worktree for branch '$target_branch' not found"
    return 1
  fi

  local root
  root="$(git_repo_root)"
  local shared_root
  shared_root="$(git_shared_root 2>/dev/null)"
  [[ -z "$shared_root" ]] && shared_root="$root"

  info "Copying env files from '$root' -> '$target_path'"
  gw_env_copy_files "$root" "$target_path" "$shared_root"
  info "Env files synced to '$target_branch'"
}
