#!/usr/bin/env bash
# stop_session.sh — multi-agent-mux-stop 의 부속 스크립트
# Usage:
#   bash stop_session.sh --session <name> [--agent claude|agy] \
#       [--mode soft|hard] [--purge-conversation] [--yes]
#
# mode:
#   soft  — YAML 을 status=archived 로 마크, tmux 세션은 그대로 둠 (P1-A:
#           terminated 는 tmux 가 실제로 죽은 상태에만 사용)
#   hard  — tmux kill-session + YAML status=terminated
# --purge-conversation: --mode hard 일 때만. 삭제 대상 세션의 *워크스페이스에
#                       격리된* conversation artifact 만 삭제 (P0-C). 전역
#                       agent_identities 를 참조하지 않음. resume 불가.
#
# Stop extension (Option A — stop 확장, 새 6번째 스킬 없이 stop 의미론 흡수):
#   --capture-id   — kill 직전에 이 워크스페이스의 conversation id 를 row 에 확정
#                    기록 (claude_session_id_own / agy_conversation_id_own) →
#                    다음 resume 이 tier-1(race-free) 로 복원. find_workspace_uuid
#                    재사용 (per-row -> workspace-scoped disk scan -> cache).
#   --reason R     — 상태 전이 사유 (stop_reason). 기본값 manual_stop.
#   --graceful     — kill-session 즉시 종료 대신 send-keys 로 정상 종료 유도 →
#                    3초 대기 → 미종료 시 kill-session(SIGTERM) → 5초 → SIGKILL.
#   위 세 옵션 중 하나라도 주면 STOP 모드: status 가 terminated 가 아니라 stopped
#   로 전이 (running -> stopped). 멱등: 이미 stopped 면 no-op + exit 0.
#   옵션 미지정 시 기존 hard/soft 동작 그대로 (backward compatible).
#
# Exit codes:
#   0 = success (or already-stopped no-op) | 1 = YAML not found / not registered
#   2 = invalid args | 3 = interactive confirmation required (--yes 누락)
#   4 = purge aborted (tmux session survived the kill chain)
set -euo pipefail

# shellcheck disable=SC1091
source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)/lib.sh"

usage() {
  cat <<EOF
Usage: $0 --session <name> [--agent claude|agy] [--purge-conversation] [--yes] [--reason <reason>]

Stop arguments:
  --reason <reason> — stop_reason field (default: manual_stop)
  (idempotent: stopping an already-stopped session is a no-op with exit 0)
EOF
}

SESSION_NAME=""
AGENT=""
PURGE=0
YES=0
CAPTURE_ID=1
GRACEFUL=1
REASON="manual_stop"
STOP_MODE=1

while [ $# -gt 0 ]; do
  case "$1" in
    --session) SESSION_NAME="$2"; shift 2 ;;
    --agent)   AGENT="$2";        shift 2 ;;
    --purge-conversation) PURGE=1; shift ;;
    --yes)     YES=1; shift ;;
    --reason)  REASON="$2";       shift 2 ;;
    --mode|--capture-id|--graceful)
      echo "ERROR: $1 option is deprecated. Stop now always stops gracefully and captures IDs." >&2
      exit 2
      ;;
    -h|--help) usage; exit 0 ;;
    *) echo "ERROR: unknown arg: $1" >&2; usage; exit 2 ;;
  esac
done
[ -n "$SESSION_NAME" ] || { echo "ERROR: --session required" >&2; usage; exit 2; }
[ -f "$AGENT_SESSIONS_YAML" ] || { echo "ERROR: $AGENT_SESSIONS_YAML not found" >&2; exit 1; }

TMUX_SERVER_NAME="$(resolve_tmux_server "$SESSION_NAME")"
export TMUX_SERVER_NAME

# --agent 미지정 시 이름 suffix 로 fallback (P1-F)
if [ -z "$AGENT" ]; then
  case "$SESSION_NAME" in
    *-creator-claude) AGENT=claude ;;
    *-creator-agy)    AGENT=agy ;;
    *-creator-hermes) AGENT=hermes ;;
    *-creator-cline)  AGENT=cline ;;
    *) echo "ERROR: cannot infer agent from '$SESSION_NAME'; pass --agent" >&2; exit 2 ;;
  esac
fi

# 세션이 YAML 에 있는지 + 해당 row 의 워크스페이스 cwd 및 delegate_job_id 추출.
# JSON 으로 emit — cwd 에 '|' 가 들어가도 안전 (review item 7; 기존 cwd|jid 파서 대체).
MAPPED_DATA=$(MAM_STATE_JSON="$(load_state_json)" SESSION_NAME="$SESSION_NAME" python3 -c "
import sys, os, json
name = os.environ['SESSION_NAME']
d = json.loads(os.environ.get('MAM_STATE_JSON', '{}'))
for s in d.get('tmux_sessions', []):
    if s.get('name') == name:
        cwd = (s.get('pane') or {}).get('cwd', '')
        jid = s.get('delegate_job_id', '') or ''
        print(json.dumps({'cwd': cwd, 'job_id': jid}))
        sys.exit(0)
sys.exit(7)
") || {
  echo "ERROR: session '$SESSION_NAME' not in $AGENT_SESSIONS_YAML" >&2
  exit 1
}

TARGET_CWD=$(printf '%s' "$MAPPED_DATA" | python3 -c 'import sys,json; print(json.load(sys.stdin).get("cwd",""))')
DELEGATE_JOB_ID=$(printf '%s' "$MAPPED_DATA" | python3 -c 'import sys,json; print(json.load(sys.stdin).get("job_id",""))')

# 멱등성: STOP 모드에서 이미 stopped 인 세션이고 purge 가 아닐 때만 no-op + exit 0
if [ "$STOP_MODE" = "1" ] && [ "$PURGE" != "1" ]; then
  if STOPPED_INFO=$(is_already_stopped "$SESSION_NAME"); then
    echo "already stopped (status=stopped, $STOPPED_INFO) — no-op"
    exit 0
  fi
fi

# purge 확인
if [ "$PURGE" = "1" ] && [ "$YES" != "1" ]; then
  echo "DANGER: --purge-conversation will DELETE this workspace's on-disk conversation."
  echo "  workspace: ${TARGET_CWD:-<unknown>}"
  echo "  This means: no future multi-agent-mux-resume for this session."
  echo "  Re-run with --yes to confirm."
  exit 3
fi

# purge 대상 UUID 를 워크스페이스 격리해서 해결 (P0-C — 전역 참조 금지)
PURGE_UUID=""
if [ "$PURGE" = "1" ] && [ -n "$TARGET_CWD" ]; then
  PURGE_UUID=$(find_workspace_uuid "$TARGET_CWD" "$AGENT" "$SESSION_NAME" || true)
fi

NOW_ISO=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
NOW_EPOCH=$(date +%s)

# tmux 상태 + 마지막 TUI 스냅샷 (살아있을 때만; capture-pane 내용은 env 로만 전달)
TMUX_ALIVE=0
LAST_STATUS=""
if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
  TMUX_ALIVE=1
  LAST_STATUS=$(tmux capture-pane -t "$SESSION_NAME" -p -S -10 2>/dev/null | tr '\n' ' ' | head -c 500 || true)
fi

# --capture-id: kill 직전에 conversation id 를 해결 (process/jsonl 이 아직 살아있을 때).
# find_workspace_uuid 가 tier-1(row) -> tier-2(workspace-scoped disk scan) -> tier-3(cache)
# 를 알아서 시도하므로 tmux 생사와 무관하게 동작.
CAPTURED_UUID=""
if [ "$CAPTURE_ID" = "1" ] && [ -n "$TARGET_CWD" ]; then
  CAPTURED_UUID=$(capture_conversation_id "$AGENT" "$TARGET_CWD" "$SESSION_NAME" || true)
  if [ -n "$CAPTURED_UUID" ]; then
    echo "captured conversation id: $CAPTURED_UUID"
  else
    echo "WARN: --capture-id requested but no conversation id resolved (nothing on disk yet)"
  fi
fi

delegate_publish_event "$DELEGATE_JOB_ID" progress "terminating"

# --graceful: send-keys 로 정상 종료 유도 → 폴백 체인 (SIGTERM → SIGKILL).
graceful_stop() {
  local pane_pid exitkey
  pane_pid=$(tmux list-panes -t "$SESSION_NAME" -F '#{pane_pid}' 2>/dev/null | head -1 || true)
  case "$AGENT" in
    claude) exitkey="/exit" ;;
    agy)    exitkey="Exit" ;;
    hermes) exitkey="/exit" ;;
    cline)  exitkey="/exit" ;;
    *)      exitkey="/exit" ;;
  esac
  echo "graceful: send-keys '$exitkey' to $SESSION_NAME"
  send_keys_safe "$SESSION_NAME" "$exitkey" "stop$$" || echo "graceful: safe delivery failed (rc=$?) — falling back to kill chain"
  _wait_session_gone "$SESSION_NAME" 5 || true
  if ! tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
    echo "graceful: exited cleanly"
    return 0
  fi
  echo "graceful: still alive → kill-session (SIGTERM)"
  tmux kill-session -t "$SESSION_NAME" 2>/dev/null || true
  _wait_session_gone "$SESSION_NAME" 8 || true
  if ! tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
    echo "graceful: terminated after kill-session"
    return 0
  fi
  echo "graceful: STILL alive → SIGKILL fallback (pane pid $pane_pid)"
  [ -n "$pane_pid" ] && kill -9 "$pane_pid" 2>/dev/null || true
}

# tmux 종료: graceful 이면 폴백 체인, 아니면 기존 hard kill.
if [ "$GRACEFUL" = "1" ] && [ "$TMUX_ALIVE" = "1" ]; then
  graceful_stop
elif [ "$TMUX_ALIVE" = "1" ]; then
  tmux kill-session -t "$SESSION_NAME"
  echo "killed tmux: $SESSION_NAME"
else
  echo "tmux already dead, just updating YAML"
fi

# Purge pre-gate: 레코드 제거는 tmux 사망이 확인된 경우에만 허용한다.
# (kill 체인은 best-effort — 세션이 살아남으면 monitor drift-B 가
#  레코드 없는 세션을 running 으로 자동 재등록해 purge 가 조용히 뒤집힌다)
if [ "$PURGE" = "1" ] && [ "$TMUX_ALIVE" = "1" ]; then
  _wait_session_gone "$SESSION_NAME" 5 || true   # SIGKILL 폴백의 비동기 회수 윈도우 흡수
  if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
    echo "ERROR: session '$SESSION_NAME' is still alive after the kill chain." >&2
    echo "  Refusing registry removal — records preserved (no state was modified)." >&2
    echo "  Diagnose the stuck TUI (tmux attach -t '$SESSION_NAME'), then re-run" >&2
    echo "  stop_session.sh --purge-conversation --yes (retry is safe/idempotent)." >&2
    delegate_publish_event "$DELEGATE_JOB_ID" error "purge aborted: tmux session still alive"
    exit 4
  fi
fi

atomic_dump_yaml "$AGENT_SESSIONS_YAML" \
  SESSION_NAME="$SESSION_NAME" AGENT="$AGENT" PURGE="$PURGE" \
  NOW_ISO="$NOW_ISO" NOW_EPOCH="$NOW_EPOCH" LAST_STATUS="$LAST_STATUS" \
  PURGE_UUID="$PURGE_UUID" TARGET_CWD="$TARGET_CWD" \
  REASON="$REASON" CAPTURED_UUID="$CAPTURED_UUID" <<'PYEOF'
import shutil
name = os.environ['SESSION_NAME']
agent = os.environ['AGENT']
purge = os.environ['PURGE'] == '1'
now = os.environ['NOW_ISO']
home = os.environ['HOME_DIR']
last_status = os.environ.get('LAST_STATUS', '')
purge_uuid = os.environ.get('PURGE_UUID', '').strip()
ws = os.environ.get('TARGET_CWD', '')
reason = os.environ.get('REASON', '') or 'manual_stop'
captured = os.environ.get('CAPTURED_UUID', '').strip()

target = None
for s in d.get('tmux_sessions', []):
    if s.get('name') == name:
        target = s
        break
if target is None:
    print(f"ERROR: disappeared during script: {name}", flush=True)
    raise SystemExit(1)

if not purge:
    target['status'] = 'stopped'
    target['stopped_at'] = now
    target['stopped_at_epoch'] = int(os.environ['NOW_EPOCH'])
    target['stop_reason'] = reason
    target['termination_mode'] = 'graceful'

if last_status:
    target['last_visible_status_at_termination'] = last_status

# --capture-id: 항상 captured UUID 기록 (purge가 아닐 때만)
if captured and not purge:
    if agent == 'claude':
        target['claude_session_id_own'] = captured
    elif agent == 'agy':
        target['agy_conversation_id_own'] = captured
    elif agent == 'hermes':
        target['hermes_conversation_id_own'] = captured
    elif agent == 'cline':
        target['cline_conversation_id_own'] = captured
    target['resumable'] = True

# --purge-conversation: 워크스페이스 격리된 UUID 의 디스크 artifact 만 삭제 (P0-C)
# T6: stop-purge 시 격리 디렉터리 청소 및 경로 가드
iso = target.get('isolation')
if purge and iso:
    iso_root = iso.get('root')
    iso_uuid = iso.get('uuid')
    if iso_root and iso_uuid:
        ws_abs = os.path.abspath(ws) if ws else ""
        expected_homes_dir = os.path.join(ws_abs, '.mam', 'agent_homes')
        expected_iso_root = os.path.join(expected_homes_dir, iso_uuid)
        if (os.path.abspath(iso_root) == os.path.abspath(expected_iso_root) and
            os.path.abspath(iso_root).startswith(os.path.abspath(expected_homes_dir) + os.sep)):
            if os.path.isdir(iso_root):
                shutil.rmtree(iso_root)
                print(f"purged isolated home: {iso_root}", flush=True)
        else:
            print(f"WARN: isolated home path check failed: {iso_root}", flush=True)

if purge and purge_uuid:
    if agent == 'claude':
        key = ws.replace('/', '-').replace('_', '-')
        claude_project_dir = os.environ.get('CLAUDE_PROJECT_DIR', f"{home}/.claude/projects")
        jsonl = f"{claude_project_dir}/{key}/{purge_uuid}.jsonl"
        if os.path.exists(jsonl):
            os.remove(jsonl)
            print(f"purged: {jsonl}", flush=True)
        target['claude_session_id_own'] = None
    elif agent == 'agy':
        db = f"{home}/.gemini/antigravity-cli/conversations/{purge_uuid}.db"
        if os.path.exists(db):
            os.remove(db)
            print(f"purged: {db}", flush=True)
        brain = f"{home}/.gemini/antigravity-cli/brain/{purge_uuid}"
        if os.path.isdir(brain):
            shutil.rmtree(brain)
            print(f"purged: {brain}", flush=True)
        target['agy_conversation_id_own'] = None
    elif agent == 'hermes':
        json_file = f"{home}/.hermes/sessions/session_{purge_uuid}.json"
        if os.path.exists(json_file):
            os.remove(json_file)
            print(f"purged: {json_file}", flush=True)
        hdb = f"{home}/.hermes/state.db"
        if os.path.exists(hdb):
            try:
                import sqlite3
                hconn = sqlite3.connect(hdb)
                hconn.execute("DELETE FROM sessions WHERE id=?", (purge_uuid,))
                hconn.execute("DELETE FROM messages WHERE session_id=?", (purge_uuid,))
                hconn.commit()
                hconn.close()
                print(f"purged db records for session: {purge_uuid}", flush=True)
            except Exception as e:
                print(f"WARN: purge hermes db records failed: {e}", flush=True)
        target['hermes_conversation_id_own'] = None
    elif agent == 'cline':
        sessions_dir = f"{home}/.cline/data/sessions/{purge_uuid}"
        if os.path.isdir(sessions_dir):
            shutil.rmtree(sessions_dir)
            print(f"purged: {sessions_dir}", flush=True)
        target['cline_conversation_id_own'] = None
    # agent_identities 는 cache — 이 워크스페이스 것일 때만 비운다
    ai = (d.get('agent_identities') or {}).get(agent) or {}
    if ai.get('project_cwd') == ws:
        if agent == 'claude' and ai.get('session_id') == purge_uuid:
            ai['session_id'] = None
            ai['session_jsonl'] = None
            ai.pop('session_size_bytes', None)
            ai.pop('session_lines', None)
        elif agent == 'agy' and ai.get('conversation_id') == purge_uuid:
            ai['conversation_id'] = None
            ai['conversation_db'] = None
            ai['conversation_brain_dir'] = None
        elif agent == 'hermes' and ai.get('session_id') == purge_uuid:
            ai['session_id'] = None
        elif agent == 'cline' and ai.get('session_id') == purge_uuid:
            ai['session_id'] = None
elif purge and not purge_uuid:
    print("WARN: --purge-conversation requested but no workspace-scoped UUID resolved; nothing purged", flush=True)

if purge:
    d['tmux_sessions'] = [s for s in d.get('tmux_sessions', []) if s.get('name') != name]
    if purge_uuid:
        print(f"removed: {name} (registry entry fully purged from YAML+DB)", flush=True)
    else:
        print(f"removed: {name} (registry entry purged from YAML+DB; WARN: conversation artifacts unresolved — disk copies may remain)", flush=True)
else:
    print(f"updated: {name} status={target['status']}", flush=True)
PYEOF

delegate_publish_event "$DELEGATE_JOB_ID" completed "session terminated"

echo
echo "=== stop complete ==="
echo "  session:  $SESSION_NAME"
echo "  agent:    $AGENT"
echo "  reason:   $REASON"
echo "  captured: ${CAPTURED_UUID:-<none>}"
echo "  purge:    $PURGE${PURGE_UUID:+ (uuid $PURGE_UUID)}"
echo "  time:     $NOW_ISO"
echo
echo "Recovery: multi-agent-mux-create + multi-agent-mux-resume 로 동일 컨텍스트 복원 가능"
echo "  (단 --purge-conversation 사용 시 복원 불가)"
