#!/usr/bin/env bash
set -euo pipefail

PROJECT_DIR="/home/long/project/蚊子"
STATE_DIR="$PROJECT_DIR/logs/e2e-automation"
PID_FILE="$STATE_DIR/runner.pid"
WATCHDOG_LOG="$STATE_DIR/watchdog.log"
RUNNER="$PROJECT_DIR/scripts/e2e_continuous_runner.sh"
CONTRACT_CHECK="$PROJECT_DIR/scripts/validate_test_contracts.sh"

mkdir -p "$STATE_DIR"

log() {
  echo "[$(date '+%F %T')] $*" >> "$WATCHDOG_LOG"
}

if [ ! -x "$CONTRACT_CHECK" ]; then
  log "data-contract checker missing: $CONTRACT_CHECK"
  exit 2
fi

if ! SPRING_PROFILES_ACTIVE="${SPRING_PROFILES_ACTIVE:-e2e}" "$CONTRACT_CHECK" "$PROJECT_DIR" preflight >> "$WATCHDOG_LOG" 2>&1; then
  log "data-contract preflight failed; watchdog will not start/restart runner"
  exit 2
fi

if [ -f "$STATE_DIR/done.flag" ]; then
  log "done flag exists, watchdog idle"
  exit 0
fi

if [ -f "$PID_FILE" ]; then
  pid="$(cat "$PID_FILE" || true)"
  if [ -n "${pid:-}" ] && kill -0 "$pid" 2>/dev/null; then
    latest_log="$(ls -1t "$STATE_DIR"/run_*.log 2>/dev/null | head -n1 || true)"
    if [ -n "$latest_log" ]; then
      now=$(date +%s)
      mtime=$(stat -c %Y "$latest_log" 2>/dev/null || echo "$now")
      idle=$((now - mtime))
      if [ "$idle" -gt 1800 ]; then
        log "runner appears stuck (idle ${idle}s), restarting pid=$pid"
        kill "$pid" 2>/dev/null || true
        sleep 2
      else
        log "runner healthy pid=$pid idle=${idle}s"
        exit 0
      fi
    else
      log "runner pid=$pid, no run log yet"
      exit 0
    fi
  else
    log "stale pid file or process gone"
  fi
fi

nohup "$RUNNER" > "$STATE_DIR/nohup.out" 2>&1 &
new_pid=$!
echo "$new_pid" > "$PID_FILE"
log "runner started pid=$new_pid"
