#!/usr/bin/env bash
# aTool - hooks/prompt-guard
# UserPromptSubmit hook: reminds AI to clarify before implementing
# v2: Tightened skip conditions, structured template injection for Tier 2+

set -euo pipefail

# Detect which IDE is running this hook
HOOK_IDE="claude"
if [[ -n "${CURSOR_PLUGIN_ROOT:-}" ]]; then
    HOOK_IDE="cursor"
fi

# Escape string for JSON embedding
escape_for_json() {
    local s="$1"
    s="${s//\\/\\\\}"
    s="${s//\"/\\\"}"
    s="${s//$'\n'/\\n}"
    s="${s//$'\r'/\\r}"
    s="${s//$'\t'/\\t}"
    printf '%s' "$s"
}

# Read JSON from stdin (Claude Code provides {"prompt": "..."} on stdin)
INPUT=""
if [[ ! -t 0 ]]; then
    INPUT=$(cat)
fi

# Extract the prompt text using jq
PROMPT=""
if command -v jq &>/dev/null && [[ -n "$INPUT" ]]; then
    PROMPT=$(printf '%s' "$INPUT" | jq -r '.prompt // empty' 2>/dev/null || echo "")
fi

# Fallback: if jq not available or parsing failed, try grep
if [[ -z "$PROMPT" && -n "$INPUT" ]]; then
    PROMPT=$(printf '%s' "$INPUT" | grep -o '"prompt"[[:space:]]*:[[:space:]]*"[^"]*"' 2>/dev/null | head -1 | sed 's/.*"prompt"[[:space:]]*:[[:space:]]*"//;s/"$//' || echo "")
fi

# No prompt extracted — exit silently
if [[ -z "$PROMPT" ]]; then
    exit 0
fi

# Normalize: lowercase for pattern matching
PROMPT_LOWER=$(printf '%s' "$PROMPT" | tr '[:upper:]' '[:lower:]')
PROMPT_LEN=${#PROMPT}

# ── Skip conditions (< 5ms exit) ──────────────────────────────────────────

# Detect CJK text (more information-dense, use lower threshold)
# Use LC_ALL=C to ensure byte-level matching works regardless of locale
HAS_CJK=false
if printf '%s' "$PROMPT" | LC_ALL=C grep -q '[一-龥]' 2>/dev/null; then
    HAS_CJK=true
fi

# 1. Short messages — TIGHTENED: lower thresholds
#    Old: CJK < 8, Latin < 20
#    New: CJK < 6, Latin < 15 (pure short replies like "ok", "好")
if $HAS_CJK && [[ "$PROMPT_LEN" -lt 6 ]]; then
    exit 0
elif ! $HAS_CJK && [[ "$PROMPT_LEN" -lt 15 ]]; then
    exit 0
fi

# 2. Pure confirmations only — TIGHTENED: strict single-word matches
#    Old: included multi-word phrases like "go ahead", "pls", "please"
#    New: only exact single-word affirmative replies
# Perf note: bash `[[ $x =~ $pattern ]]` is in-process (no subprocess fork);
# was `printf | grep -qE` ≈ 5-10ms × 11 calls = ~80ms. In-process matching
# brings the hook from ~35ms total to <8ms — felt latency on every prompt.
PURE_CONFIRMATIONS='^(yes|ok|okay|好的|继续|对|嗯|是的|行|可以|sure|yep|yeah|done|go|next)$'
[[ "$PROMPT_LOWER" =~ $PURE_CONFIRMATIONS ]] && exit 0

# 3. Exit signals (user explicitly wants to skip clarification) — KEPT as-is
EXIT_SIGNALS='(别问了|不要问|别再问|直接做|直接实现|直接开始|不用确认|跳过提问|不要确认|just do it|stop asking|no questions|don.t ask|skip asking|go ahead and implement)'
[[ "$PROMPT_LOWER" =~ $EXIT_SIGNALS ]] && exit 0

# ── REMOVED: Old skip conditions that were too broad ───────────────────────
# OLD: "Questions ending with ?" → REMOVED (questions like "能帮我实现一个搜索功能吗？" should trigger)
# OLD: "Meta-commands (commit, run, npm, explain...)" → REMOVED
#      ("build a dashboard" starts with "build" but is a feature request)
#      ("run the migration to add new user fields" is an implementation request)

# ── Classify task complexity ──────────────────────────────────────────────

# Count words: for CJK text (no spaces), count CJK chars as words;
# for Latin text, count whitespace-separated tokens
WORD_COUNT=0
if $HAS_CJK; then
    CJK_COUNT=$(printf '%s' "$PROMPT" | LC_ALL=C grep -o '[一-龥]' 2>/dev/null | wc -l | tr -d '[:space:]')
    WORD_COUNT=$((CJK_COUNT))
else
    for word in $PROMPT; do
        WORD_COUNT=$((WORD_COUNT + 1))
    done
fi

# Detect implementation intent keywords
# Extended with common natural language patterns (CJK + English)
IMPLEMENT_KEYWORDS="(开发|创建|新增|实现|添加|加个|做一个|写一个|重构|优化|修改|调整|修复|集成|迁移|设计|拆分|改造|搭建|编写|扩展|build|create|add|implement|make|develop|design|feature|refactor|extract|integrate|support|handle|fix|migrate|modify|update|change|enhance|improve)"

HAS_IMPLEMENT=false
LENGTH_OK=false

if $HAS_CJK && [[ "$PROMPT_LEN" -ge 6 ]]; then
    LENGTH_OK=true
elif ! $HAS_CJK && [[ "$PROMPT_LEN" -ge 15 ]]; then
    LENGTH_OK=true
fi

[[ "$PROMPT_LOWER" =~ $IMPLEMENT_KEYWORDS ]] && HAS_IMPLEMENT=true

# ── Determine tier and output ─────────────────────────────────────────────

if $LENGTH_OK && $HAS_IMPLEMENT && [[ "$WORD_COUNT" -gt 3 ]]; then
    # Determine complexity tier
    TIER=0

    # Tier 3 indicators: architecture-level, multi-module, system-wide
    TIER3_KEYWORDS="(架构|系统|整体|全面|重新设计|从零|微服务|拆分|architecture|system.redesign|from scratch|microservice|monolith|break down|overhaul)"
    # Tier 2 indicators: new feature, multi-step, unclear scope
    TIER2_KEYWORDS="(功能|模块|页面|组件|接口|服务|支持|feature|module|page|component|endpoint|service|support|workflow|pipeline|dashboard|integration)"

    if [[ "$PROMPT_LOWER" =~ $TIER3_KEYWORDS ]]; then
        TIER=3
    elif [[ "$PROMPT_LOWER" =~ $TIER2_KEYWORDS ]]; then
        TIER=2
    else
        TIER=1
    fi

    # Fallback tier escalation: if message has pronouns + concrete request context, bump tier
    # This catches natural language requests without explicit keywords
    if [[ "$TIER" -eq 1 ]] && [[ "$PROMPT_LEN" -gt 30 ]]; then
        _PRONOUN_RE='(我|my|我们|our|帮|help|想|want|需要|need|能|can|可以|able)'
        _CODEREF_RE='(\.\w+|/\w+|\{\w+\}|function|class|method|endpoint)'
        if [[ "$PROMPT_LOWER" =~ $_PRONOUN_RE ]] && [[ "$PROMPT" =~ $_CODEREF_RE ]]; then
            TIER=2
        fi
    fi

    # Detect UI-related requests
    UI_KEYWORDS='(页面|组件|界面|表单|弹窗|导航|dashboard|UI|ux|layout|form|modal|navigation|button|card|dialog|menu|sidebar|header|footer|动画|交互|样式|style|animation|interaction)'
    HAS_UI_REQUEST=false
    [[ "$PROMPT_LOWER" =~ $UI_KEYWORDS ]] && HAS_UI_REQUEST=true

    # Detect brand/style reference
    BRAND_KEYWORDS='(stripe|apple|vercel|notion|linear|spotify|tesla|figma|airbnb|slack|github|discord|netflix|shopify|coinbase|revolut|uber|bmw|ferrari|framer|miro|supabase|sentry|posthog|cursor|raycast|warp|style|主题|theme|配色|color.scheme|设计系统|design.system|风格|极简|minimal|暗色|dark.mode|渐变|gradient|高端|premium)'
    HAS_BRAND_REQUEST=false
    [[ "$PROMPT_LOWER" =~ $BRAND_KEYWORDS ]] && HAS_BRAND_REQUEST=true

    # Detect data-warehouse related requests
    DATA_KEYWORDS='(数据仓库|数仓|ETL|数据建模|维度建模|事实表|维度表|数据质量|指标口径|S2T|T2S|DAG|Airflow|dbt|Hive|Spark.sql|数据分区|数据分层|ODS|DWD|DWS|ADS|DIM|CDC|数据血缘|数据治理|data.warehouse|dimensional.model|fact.table|dim.table|data.quality|data.pipeline|data.lineage|data.governance)'
    HAS_DATA_REQUEST=false
    [[ "$PROMPT_LOWER" =~ $DATA_KEYWORDS ]] && HAS_DATA_REQUEST=true

    # Build tier-appropriate reminder
    if [[ "$TIER" -ge 2 ]]; then
        # Tier 2+: Structured template — require analysis before implementation
        _REMINDER="<system-reminder>
**TIER ${TIER} TASK DETECTED** — This is a ${TIER}-level task. Before writing ANY code:

1. **Analyze** — Read relevant source files first (CLAUDE.md, package.json, existing patterns)
2. **Classify** — Confirm complexity: Tier ${TIER} = $([ "$TIER" -eq 2 ] && echo "3-5 clarification questions" || echo "delegate to /brainstorming")
3. **Clarify** — Ask context-aware questions about: $([ "$TIER" -eq 2 ] && echo "scope, edge cases, existing patterns, acceptance criteria" || echo "architecture, constraints, trade-offs, migration strategy")
4. **Plan** — Write implementation plan BEFORE touching code

Do NOT jump to implementation. Read first, ask second, plan third, code last.
See /clarify-before-build for full methodology.
</system-reminder>"
    else
        # Tier 1: Lightweight reminder
        _REMINDER='<system-reminder>
Before implementing, classify task complexity and clarify requirements if needed:
- Tier 0 (direct): single-value change, typo, format fix -> just do it
- Tier 1 (1-2 questions): small change with ambiguity -> ask key questions
- Tier 2 (3-5 questions): new feature, medium complexity -> structured clarification
- Tier 3: architecture-level, vague requirements -> delegate to /brainstorming
See /clarify-before-build for full methodology.
</system-reminder>'
    fi

    # Append UI design gate reminder for UI-related requests
    if $HAS_UI_REQUEST; then
        _REMINDER+=$'\n'
        _REMINDER+="5. **UI Design Gate** — This request involves UI. Before implementing:"
        _REMINDER+=$'\n'
        _REMINDER+="   - If project has no UI_STYLE.md, establish design guidelines first with /ui-ux-pro"
        _REMINDER+=$'\n'
        _REMINDER+="   - If project has no DESIGN.md, run /ui-ux-pro to generate a design system manifest"
        _REMINDER+=$'\n'
        _REMINDER+="   - Create UI design doc + HTML prototype in docs/500-design/ before coding"
        _REMINDER+=$'\n'
        _REMINDER+="   - Get user approval on prototype before implementation"
    fi

    # Append brand style gate reminder for brand/style references
    if $HAS_BRAND_REQUEST; then
        _REMINDER+=$'\n'
        _REMINDER+="6. **Brand Style Gate** — User referenced a brand/style. Check /ui-ux-pro brand catalog (data/brand-catalog.md) for matching templates. Generate DESIGN.md from brand template if project has none."
    fi

    # Append data skill routing reminder for data-related requests
    if $HAS_DATA_REQUEST; then
        _REMINDER+=$'\n'
        _REMINDER+="7. **Data Skill Routing** — Data warehouse keywords detected. Available data skills:"
        _REMINDER+=$'\n'
        _REMINDER+="   - /data-conventions — DW architecture, naming, SQL standards, table design"
        _REMINDER+=$'\n'
        _REMINDER+="   - /data-warehouse-modeling — Dimensional modeling, DDL, ER diagrams"
        _REMINDER+=$'\n'
        _REMINDER+="   - /sql-development — SQL script generation (ODS→ADS pipeline)"
        _REMINDER+=$'\n'
        _REMINDER+="   - /data-code-review — DDL/DML review and compliance checks"
        _REMINDER+=$'\n'
        _REMINDER+="   - /data-quality — Quality rule design and validation"
        _REMINDER+=$'\n'
        _REMINDER+="   - /sql-optimization — SQL performance tuning"
        _REMINDER+=$'\n'
        _REMINDER+="   - /requirement-analysis — BRD to data PRD and S2T Mapping"
        _REMINDER+=$'\n'
        _REMINDER+="   - /scheduling-design — Airflow DAG and scheduling"
        _REMINDER+=$'\n'
        _REMINDER+="   - /test-case-generator — S2T/T2S/Count/Constraint test cases"
        _REMINDER+=$'\n'
        _REMINDER+="   - /deployment — Data deployment and release"
    fi

    if [[ "$HOOK_IDE" == "cursor" ]]; then
        _ESCAPED=$(escape_for_json "$_REMINDER")
        printf '{\n  "additional_context": "%s"\n}\n' "$_ESCAPED"
    else
        printf '%s' "$_REMINDER"
    fi
fi

exit 0
