#!/bin/bash
# Hook: UserPromptSubmit - Suggest Airflow skill when Airflow keywords detected
# This hook runs before Claude processes the user's prompt and can inject context

# Read user prompt from stdin (hook executor sends JSON with a "prompt" field)
INPUT_JSON=$(cat)
USER_PROMPT=$(echo "$INPUT_JSON" | jq -r '.prompt // empty' 2>/dev/null)
if [ -z "$USER_PROMPT" ]; then
    # Fallback: treat entire input as the prompt (non-JSON input)
    USER_PROMPT="$INPUT_JSON"
fi

# Define Airflow-related keywords (case insensitive)
AIRFLOW_KEYWORDS=(
    "airflow"
    "dag"
    "dags"
    "pipeline"
    "pipelines"
    "workflow"
    "workflows"
    "task instance"
    "task run"
    "connection"
    "variable"
    "pool"
    "trigger dag"
    "test dag"
    "debug dag"
    "dag fail"
    "list dags"
    "show dags"
    "get dag"
    "dag status"
    "dag run"
    "astro dev"
    "af"
)

# Check if prompt contains any Airflow keywords
MATCHED=false
PROMPT_LOWER=$(echo "$USER_PROMPT" | tr '[:upper:]' '[:lower:]')

for keyword in "${AIRFLOW_KEYWORDS[@]}"; do
    if echo "$PROMPT_LOWER" | grep -qw "$keyword"; then
        MATCHED=true
        break
    fi
done

# If Airflow keywords detected, inject skill suggestion
if [ "$MATCHED" = true ]; then
    # Check if user already explicitly mentioned using the skill
    if echo "$PROMPT_LOWER" | grep -q "use.*skill\|/data:airflow"; then
        # User already wants to use the skill, no need to inject
        exit 0
    fi

    # Inject context as system-reminder to suggest using the Airflow skill
    cat <<'EOF'
<system-reminder>
Airflow operation detected. Use the `/data:airflow` skill for Airflow operations. This skill provides structured workflow guidance, best practices for MCP tool usage, routing to specialized skills (testing, debugging, authoring), and prevention of bash/CLI antipatterns. Load the skill first with `/data:airflow`, then proceed with the user's request.
</system-reminder>
EOF
    exit 0
fi

# No Airflow keywords found, pass through
exit 0
