#!/bin/bash

##############################################################################
# ⚠️  DEPRECATED - This bash script is deprecated
#
# Deprecation Date: 2025-11-20
# Removal Date: 2026-02-20 (90 days)
# Replacement: dist/cli/pre-edit-hook.js
#
# This script will be removed in 90 days. Please migrate to TypeScript.
#
# Migration Guide: See docs/BASH_DEPRECATION_NOTICE.md
# TypeScript Benefits:
#   - Type safety (zero runtime type errors)
#   - 90%+ test coverage
#   - Better performance
#   - Comprehensive documentation
#
# Automatic Migration:
#   Set USE_TYPESCRIPT=true to use TypeScript implementation automatically
#
##############################################################################


# Pre-Edit Backup Hook Wrapper (TypeScript Implementation)
# Creates backup before file modifications in agent workflows
#
# Usage: .claude/hooks/cfn-invoke-pre-edit-ts.sh FILE_PATH --agent-id AGENT_ID
#
# Arguments:
#   FILE_PATH   - Absolute path to file about to be edited
#   --agent-id  - Unique identifier for the agent performing the edit
#
# Returns:
#   Backup directory path on success
#   Exit code 1 on failure

set -euo pipefail

# === Parse Arguments ===

FILE_PATH=""
AGENT_ID=""

# First positional argument is file path
if [[ -n "${1:-}" ]] && [[ "$1" != --* ]]; then
    FILE_PATH="$1"
    shift
fi

# Parse remaining named arguments
while [[ "$#" -gt 0 ]]; do
    case $1 in
        --agent-id)
            if [[ -z "${2:-}" ]]; then
                echo "Error: --agent-id requires a value" >&2
                exit 1
            fi
            AGENT_ID="$2"
            shift 2
            ;;
        *)
            echo "Error: Unknown argument: $1" >&2
            echo "Usage: cfn-invoke-pre-edit-ts.sh FILE_PATH --agent-id AGENT_ID" >&2
            exit 1
            ;;
    esac
done

# === Validate Inputs ===

if [[ -z "$FILE_PATH" ]]; then
    echo "Error: No file path provided" >&2
    echo "Usage: cfn-invoke-pre-edit-ts.sh FILE_PATH --agent-id AGENT_ID" >&2
    exit 1
fi

if [[ -z "$AGENT_ID" ]]; then
    echo "Error: No agent ID provided" >&2
    echo "Usage: cfn-invoke-pre-edit-ts.sh FILE_PATH --agent-id AGENT_ID" >&2
    exit 1
fi

if [[ ! -f "$FILE_PATH" ]]; then
    echo "Error: File does not exist: $FILE_PATH" >&2
    exit 1
fi

# === Execute Pre-Edit Backup ===

PROJECT_ROOT="$(git rev-parse --show-toplevel)" || exit 1

# Try to use compiled TypeScript version, fall back to bash
if command -v node >/dev/null 2>&1 && [[ -f "$PROJECT_ROOT/dist/cli/pre-edit-hook.js" ]]; then
    # Use TypeScript implementation
    if ! BACKUP_DIR=$(node "$PROJECT_ROOT/dist/cli/pre-edit-hook.js" "$FILE_PATH" --agent-id "$AGENT_ID" 2>&1); then
        echo "Error: Backup failed: $BACKUP_DIR" >&2
        exit 1
    fi
else
    # Fall back to bash implementation
    BACKUP_SCRIPT="${PROJECT_ROOT}/.claude/hooks/cfn-invoke-pre-edit.sh"
    if [[ ! -f "$BACKUP_SCRIPT" ]]; then
        echo "Error: Backup script not found and TypeScript not available: $BACKUP_SCRIPT" >&2
        exit 1
    fi

    if ! BACKUP_DIR=$("$BACKUP_SCRIPT" "$FILE_PATH" --agent-id "$AGENT_ID" 2>&1); then
        echo "Error: Backup failed: $BACKUP_DIR" >&2
        exit 1
    fi
fi

# Return backup directory path
echo "$BACKUP_DIR"
exit 0
