#!/bin/bash
# HELP: Delete a worktree if it has no uncommitted changes
# ARG: name - Name of the worktree to delete
# OPT: --force - Force deletion even if worktree has uncommitted changes

if [ $# -eq 0 ]; then
    echo "Usage: $0 <worktree-name> [--force]"
    echo "Example: $0 feature-auth"
    echo "Example: $0 feature-auth --force"
    echo ""
    echo "Available worktrees:"
    if git rev-parse --git-dir > /dev/null 2>&1; then
        PROJECT_NAME=$(basename $(git rev-parse --show-toplevel))
        git worktree list | grep "$PROJECT_NAME-" | while IFS= read -r line; do
            path=$(echo "$line" | awk '{print $1}')
            name=$(basename "$path" | sed "s/$PROJECT_NAME-//")
            echo "  $name"
        done
    else
        echo "  Not in a git repository"
    fi
    exit 1
fi

# Ensure we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
    echo "Error: Not in a git repository"
    exit 1
fi

WORKTREE_NAME=""
FORCE_DELETE=false

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --force)
            FORCE_DELETE=true
            shift
            ;;
        -h|--help)
            echo "Usage: $0 <worktree-name> [--force]"
            echo ""
            echo "Delete a worktree if it has no uncommitted changes"
            echo ""
            echo "Arguments:"
            echo "  name        Name of the worktree to delete"
            echo ""
            echo "Options:"
            echo "  --force     Force deletion even if worktree has uncommitted changes"
            echo "  -h, --help  Show this help message"
            exit 0
            ;;
        *)
            if [ -z "$WORKTREE_NAME" ]; then
                WORKTREE_NAME="$1"
            else
                echo "Error: Unexpected argument '$1'"
                exit 1
            fi
            shift
            ;;
    esac
done

if [ -z "$WORKTREE_NAME" ]; then
    echo "Error: Worktree name is required"
    echo "Usage: $0 <worktree-name> [--force]"
    exit 1
fi

PROJECT_DIR=$(git rev-parse --show-toplevel)
PROJECT_NAME=$(basename "$PROJECT_DIR")
WORKTREE_PATH="$(dirname "$PROJECT_DIR")/$PROJECT_NAME-$WORKTREE_NAME"

# Check if worktree exists
if [ ! -d "$WORKTREE_PATH" ]; then
    echo "Error: Worktree directory $WORKTREE_PATH does not exist"
    echo ""
    echo "Available worktrees:"
    git worktree list | grep "$PROJECT_NAME-" | while IFS= read -r line; do
        path=$(echo "$line" | awk '{print $1}')
        name=$(basename "$path" | sed "s/$PROJECT_NAME-//")
        echo "  $name"
    done
    exit 1
fi

# Check if worktree is clean (no uncommitted changes)
cd "$WORKTREE_PATH"

if [ "$FORCE_DELETE" = false ]; then
    if [ -n "$(git status --porcelain)" ]; then
        echo "Error: Worktree '$WORKTREE_NAME' has uncommitted changes:"
        git status --porcelain | while read -r status_line; do
            echo "  $status_line"
        done
        echo ""
        echo "Either commit/stash the changes or use --force to delete anyway:"
        echo "  codeguard worktree delete $WORKTREE_NAME --force"
        exit 1
    fi
fi

echo "Deleting worktree: $WORKTREE_NAME"
echo "Path: $WORKTREE_PATH"

# Remove the worktree
cd "$PROJECT_DIR"
git worktree remove "$WORKTREE_PATH"

if [ $? -eq 0 ]; then
    echo "✅ Worktree '$WORKTREE_NAME' deleted successfully"
else
    echo "❌ Failed to delete worktree '$WORKTREE_NAME'"
    exit 1
fi