#!/usr/bin/env bash
# classify-intent.sh  -  deterministic question-vs-task classifier for Phase 0.
#
# Free-text input to the pipeline is usually an actionable task ("fix the login
# bug"), but users also type conceptual questions ("how does the auth flow
# work?"). Spinning up a branch + worktree for a question is the single most-
# cited daily annoyance with coding agents. This classifier lets Phase 0 detect
# a question up front and answer it in place instead of starting the dev chain.
#
# Usage:
#   classify-intent.sh "how does the login flow work?"
#   echo "fix the crash on launch" | classify-intent.sh -
#
# Prints exactly one of: task | question | ambiguous   (always exits 0)
#
# Heuristic (deterministic, language-aware EN + TR):
#   1. Polite request wrapping an action verb   -> task  (unless it is a
#                                                   "how/which/explain" question)
#   2. A leading imperative verb                 -> task  (beats a trailing "?")
#   3. Interrogative lead / trailing "?" / TR particle -> question
#   4. An imperative verb anywhere               -> task
#   5. else                                      -> ambiguous (treated as task by caller)
#
# Enforced by smoke-intent-guard.sh.

set -uo pipefail

read_input() {
  if [ "${1:-}" = "-" ] || [ "$#" -eq 0 ]; then
    cat
  else
    printf '%s' "$*"
  fi
}

raw="$(read_input "$@")"
# Lowercase, collapse whitespace, strip leading list markers.
text="$(printf '%s' "$raw" | tr '[:upper:]' '[:lower:]' | tr '\n' ' ' | sed -E 's/^[[:space:]]*[-*0-9.)]*[[:space:]]*//; s/[[:space:]]+/ /g')"

if [ -z "${text// /}" ]; then
  echo "ambiguous"
  exit 0
fi

# Imperative action verbs (EN + TR). Word-boundary match so "added" / "fixes"
# in a sentence still count, but "addition" as a noun does not over-trigger.
IMP_VERBS='add|adds|fix|fixes|implement|create|creates|refactor|remove|removes|delete|deletes|update|updates|build|rename|renames|migrate|write|make|wire|bump|upgrade|integrate|replace|move|split|merge|support|enable|disable|handle|render|extract|inject|expose|ekle|duzelt|düzelt|uygula|olustur|oluştur|kaldir|kaldır|sil|guncelle|güncelle|yaz|yap|tasi|taşı|degistir|değiştir|bagla|bağla|ekleme yap|entegre et'
IMPERATIVE="\\b(${IMP_VERBS})\\b"
# Same verbs anchored at the very start: a clear leading command ("fix the
# bug?", "add retry, ok?") is a task even when it ends with a "?" tag.
LEADIMP="^(${IMP_VERBS})\\b"

# Interrogative leads (EN + TR).
QLEAD='^(how|what|whats|what\x27s|why|when|where|which|who|can|could|should|would|is|are|does|do|did|explain|compare|nasil|nasıl|ne|neden|nicin|niçin|niye|hangi|kim|kimin|acikla|açıkla|karsilastir|karşılaştır|nedir|midir|mudur)\b'
# TR yes/no question particles appearing as standalone words (mi/mı/mu/mü ...).
QPARTICLE='\b(mi|mı|mu|mü|mİ|mu?dur|midir|misin|miyiz)\b'
# TR is SOV, so interrogatives ("nasil refactor ederim", "neden yavas") sit
# mid-sentence, not at the start like English - QLEAD's ^ anchor misses them.
# These words as whole tokens anywhere are a strong question signal in TR.
QTRMID='\b(nasil|nasıl|neden|nicin|niçin|niye|hangi)\b'

# Polite request to DO something: "can you add X", "could you split Y", "please
# rename Z". These look interrogative but are tasks. Only counts as a task when
# an actual action verb is also present (so "can you explain X" stays a question).
QPOLITE='^(please |pls |lutfen |lütfen |(can|could|would|will) (you|we|u) )'

# A polite lead can still wrap a conceptual question rather than an action
# request: "can you explain how to add X", "could you tell me which file".
# When these markers are present the polite-imperative shortcut must not fire,
# so the input falls through to the question rule below.
QCONCEPTUAL='\b(explain|how to|how do|how does|which|whether|tell me|you think|you say|what (is|are|does|happens))\b'

# 1. Polite imperative request -> task, UNLESS it wraps a conceptual question.
if printf '%s' "$text" | grep -qE "$QPOLITE" \
  && printf '%s' "$text" | grep -qE "$IMPERATIVE" \
  && ! printf '%s' "$text" | grep -qE "$QCONCEPTUAL"; then
  echo "task"
  exit 0
fi

# 2. Clear leading imperative -> task. A command at the very start beats a
#    trailing "?" tag, so a real task ("fix the bug?") is not skipped as a
#    question. Conceptual leads ("explain", "how", "which") are not in this set.
if printf '%s' "$text" | grep -qE "$LEADIMP"; then
  echo "task"
  exit 0
fi

# 3. Strong question signal (interrogative lead, trailing "?", or a TR question
#    particle) wins over a bare imperative verb that is NOT at the start:
#    "does it support X", "should we enable Y" are questions even though they
#    contain action words. When in doubt we lean to "question" - the safe
#    direction, since the cost of a wrong "task" is spinning up a worktree for
#    something the user only asked.
if printf '%s' "$text" | grep -qE "$QLEAD" \
  || printf '%s' "$text" | grep -qE '\?[[:space:]]*$' \
  || printf '%s' "$text" | grep -qE "$QPARTICLE" \
  || printf '%s' "$text" | grep -qE "$QTRMID"; then
  echo "question"
  exit 0
fi

# 4. Plain imperative anywhere -> task.
if printf '%s' "$text" | grep -qE "$IMPERATIVE"; then
  echo "task"
  exit 0
fi

echo "ambiguous"
exit 0
