#!/usr/bin/env bash
# Checks 4 and 5 of the card: one whole night on a simulated clock at 60x, answered line by line.
# Run from the project folder after `go build -o bedtime .`. Writes out/e2e-transcript.txt and out/e2e-log.txt.
set -u
cd "$(dirname "$0")/.."
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
mkdir -p out
fails=0
check() { if [ "$1" = "$2" ]; then echo "ok    $3"; else echo "FAIL  $3: got [$1] want [$2]"; fails=$((fails + 1)); fi; }

mkfifo "$tmp/in" "$tmp/out"
t0=$(date +%s%N)
BEDTIME_LEDGER="$tmp/ledger.tsv" ./bedtime 23:00 --clock-start 2026-10-13T22:58:00 --clock-speed 60 \
  --then "date +%s > $tmp/done" < "$tmp/in" > "$tmp/out" &
pid=$!
exec 3> "$tmp/in" 4< "$tmp/out"
: > "$tmp/said"
while IFS= read -r line <&4; do
  printf '%s\n' "$line" >> "$tmp/said"
  printf '%s\n' "$line" >> "$tmp/transcript"
  case "$line" in
  "Asking is open"*)
    sleep 0.05
    echo "five more minutes" >&3
    printf '%s\n' "five more minutes" >> "$tmp/transcript" # what the terminal would echo
    ;;
  esac
done
wait "$pid"
rc=$?
t1=$(date +%s%N)
exec 3>&- 4<&-
wall_ms=$(((t1 - t0) / 1000000))
cp "$tmp/transcript" out/e2e-transcript.txt

# Check 4
check "$rc" 0 "exit code"
check "$(grep -c '^Asking is open' "$tmp/said")" 8 "lines starting Asking is open"
check "$(grep '^Granted' "$tmp/said")" "$(cat <<'X'
Granted 15 minutes. Remaining that can ever be granted: 15 minutes.
Granted 7 minutes 30 seconds. Remaining that can ever be granted: 7 minutes 30 seconds.
Granted 3 minutes 45 seconds. Remaining that can ever be granted: 3 minutes 45 seconds.
Granted 1 minute 52.5 seconds. Remaining that can ever be granted: 1 minute 52.5 seconds.
Granted 56.25 seconds. Remaining that can ever be granted: 56.25 seconds.
Granted 28.125 seconds. Remaining that can ever be granted: 28.125 seconds.
Granted 14.062 seconds. Remaining that can ever be granted: 14.063 seconds.
X
)" "seven Granted lines match the schedule"
check "$(grep '^Not granted' "$tmp/said")" "Not granted. The next grant would be 7.031 seconds, under the 10 second minimum. Remaining that can ever be granted: 14.063 seconds." "one Not granted line"
check "$(grep -c '^Not yet' "$tmp/said")" 0 "no Not yet line"
check "$(tail -n 2 "$tmp/said" | tr '\n' ' ')" "Bedtime. Sleeping. " "Bedtime. and Sleeping. at the end"
check "$(test -f "$tmp/done" && echo yes)" yes "the --then command ran"
check "$([ "$wall_ms" -ge 31000 ] && [ "$wall_ms" -le 34000 ] && echo yes)" yes "wall time ${wall_ms} ms between 31 and 34 s"

# late send: every ask must land under 10 simulated seconds after its window opened
late=$(awk -F'\t' '
  function msof(s,  a) { split(s, a, ":"); return ((a[1] * 60 + a[2]) * 60 + a[3]) * 1000 }
  BEGIN { prev = msof("23:00:00") }
  $4 == "ask" { t = msof(substr($1, 12, 12)); if (t - (prev - 60000) >= 10000) print "late send at " substr($1, 12, 12)
                match($5, /deadline=[0-9:.]+/); prev = msof(substr($5, RSTART + 9, RLENGTH - 9)) }' "$tmp/ledger.tsv")
check "$late" "" "no late send"

# Check 5
check "$(cut -f4 "$tmp/ledger.tsv" | tr '\n' ' ')" "start ask ask ask ask ask ask ask ask end " "ledger events in order"
check "$(grep -c 'result=granted' "$tmp/ledger.tsv") $(grep -c 'result=declined' "$tmp/ledger.tsv")" "7 1" "7 granted, 1 declined"
check "$(grep -o 'deadline=[0-9:.]*' "$tmp/ledger.tsv" | tail -n 8 | tr '\n' ' ')" \
  "deadline=23:15:00 deadline=23:22:30 deadline=23:26:15 deadline=23:28:07.5 deadline=23:29:03.75 deadline=23:29:31.875 deadline=23:29:45.937 deadline=23:29:45.937 " \
  "ask deadlines match the schedule"
check "$(tail -n 1 "$tmp/ledger.tsv" | cut -f5)" "action=then" "end action=then"
BEDTIME_LEDGER="$tmp/ledger.tsv" ./bedtime log > "$tmp/log"
cp "$tmp/log" out/e2e-log.txt
check "$(sed -n 1p "$tmp/log")" "Night       Bedtime  Starts  Asks  Granted  Added                      Ended  Slept" "log header"
check "$(sed -n 2p "$tmp/log" | sed 's/23:29:46$/23:29:45/')" "2026-10-13  23:00    1       8     7        29 minutes 45.937 seconds  then   23:29:45" "log row"
check "$(sed -n 4p "$tmp/log")" "Nights recorded: 1. Nights with at least one ask: 1." "log count"

[ "$fails" -eq 0 ] && echo "e2e: all checks passed" || echo "e2e: $fails failed"
exit "$fails"
