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

ROOT=$(cd "$(dirname "$0")" && pwd)
ENROLL="$ROOT/land-enroll"
SHIP_INIT="$ROOT/../workflows/lib/ship-init.sh"
GUARD="$ROOT/../workflows/hooks/install-land-guard.sh"
PASS=0 FAIL=0

ok() { PASS=$((PASS + 1)); printf '  ok   %s\n' "$1"; }
bad() { FAIL=$((FAIL + 1)); printf '  FAIL %s\n     %s\n' "$1" "$2"; }
run() { LAND_ENROLL_SHIP_INIT="$SHIP_INIT" LAND_ENROLL_GUARD_INSTALL="$GUARD" bash "$ENROLL" "$@"; }

mkrepo() {
  local name=$1 trunk=$2 policy=${3:-} dir bare repo
  dir=$(mktemp -d "/tmp/land-enroll-${name}-XXXX")
  bare="$dir/origin.git"
  repo="$dir/repo"
  git init -q --bare -b "$trunk" "$bare"
  git init -q -b "$trunk" "$repo"
  git -C "$repo" config user.name test
  git -C "$repo" config user.email test@example.invalid
  printf '{"packageManager":"pnpm@9","scripts":{"test":"true"}}\n' > "$repo/package.json"
  printf 'lock\n' > "$repo/pnpm-lock.yaml"
  if [[ "$policy" == "merge-to-main" ]]; then
    mkdir -p "$repo/.github/workflows"
    printf 'on:\n  push:\n    branches: [%s]\njobs:\n  deploy:\n    steps: [cloudflare pages deploy]\n' "$trunk" > "$repo/.github/workflows/deploy.yml"
  fi
  [[ -z "$policy" ]] || printf '%s\n' "$policy" > "$repo/.land-policy"
  git -C "$repo" add -A
  git -C "$repo" commit -q -m seed
  git -C "$repo" remote add origin "$bare"
  git -C "$repo" push -q -u origin "$trunk"
  git -C "$repo" remote set-head origin "$trunk"
  printf '%s' "$repo"
}

cleanup() { rm -rf "$(dirname "$1")"; }

echo 'land-enroll tests:'

bash "$ENROLL" --help >/dev/null 2>&1; rc=$?
[[ $rc -eq 2 ]] \
  && ok 'help: returns usage without repository resolution' || bad 'help usage' "rc=$rc"

R=$(mkrepo main main merge-to-main)
out=$(run "$R" --postlandcmd true 2>&1); rc=$?
[[ $rc -eq 0 && "$out" == *'"mode":"merge-to-main"'* && "$out" == *'"trunk":"main"'* && "$out" == *'"queue":"stage-a"'* ]] \
  && ok 'main trunk: merge-to-main enrollment enables queue' || bad 'main trunk enrollment' "rc=$rc $out"
[[ -d "$R/.worktrees" && "$(git -C "$R" config --get harness.landGuardRef)" == refs/heads/main ]] \
  && grep -q "POSTLANDCMD='true'" "$R/.claude/scripts/ship.sh" \
  && ok 'main trunk: isolation and actual-trunk guard installed' || bad 'main guard' 'missing isolation, guard ref, or post-land command'
bash "$R/.claude/scripts/test-ship.sh" >/dev/null 2>&1 \
  && ok 'main trunk: generated smoke passes' || bad 'main smoke' 'generated smoke failed'
cleanup "$R"

R=$(mkrepo master master merge-to-main)
out=$(run "$R" 2>&1); rc=$?
[[ $rc -eq 0 && "$out" == *'"trunk":"master"'* && "$(git -C "$R" config --get harness.landGuardRef)" == refs/heads/master ]] \
  && ok 'master trunk: enrolls and guards master' || bad 'master trunk enrollment' "rc=$rc $out"
cleanup "$R"

R=$(mkrepo pr main pr)
out=$(run "$R" 2>&1); rc=$?
[[ $rc -eq 0 && "$out" == *'"mode":"pr"'* && "$out" == *'"queue":"disabled"'* && -z "$(git -C "$R" config --get harness.landQueue 2>/dev/null || true)" ]] \
  && ok 'pr policy: preserves PR mode and refuses queue' || bad 'pr policy enrollment' "rc=$rc $out"
cleanup "$R"

R=$(mkrepo dirty main pr)
printf 'x\n' > "$R/untracked"
run "$R" >/dev/null 2>&1; rc=$?
[[ $rc -eq 3 && ! -e "$R/.claude/scripts/ship.sh" ]] \
  && ok 'dirty root: fails before writes' || bad 'dirty root' "rc=$rc"
cleanup "$R"

R=$(mkrepo no-origin main pr)
git -C "$R" remote remove origin
run "$R" >/dev/null 2>&1; rc=$?
[[ $rc -eq 3 && ! -e "$R/.claude/scripts/ship.sh" ]] \
  && ok 'missing origin: fails before writes' || bad 'missing origin' "rc=$rc"
cleanup "$R"

R=$(mkrepo ambiguous main pr)
git -C "$R" branch master
git -C "$R" remote set-head origin -d
run "$R" >/dev/null 2>&1; rc=$?
[[ $rc -eq 3 && ! -e "$R/.claude/scripts/ship.sh" ]] \
  && ok 'ambiguous trunk: fails before writes' || bad 'ambiguous trunk' "rc=$rc"
cleanup "$R"

R=$(mkrepo worker main pr)
WT="$(dirname "$R")/worker"
git -C "$R" worktree add -q -b feature "$WT"
run "$WT" >/dev/null 2>&1; rc=$?
[[ $rc -eq 3 && ! -e "$WT/.claude/scripts/ship.sh" ]] \
  && ok 'worktree input: refuses non-standalone checkout' || bad 'worktree input' "rc=$rc"
cleanup "$R"

printf '\nPASS=%d FAIL=%d\n' "$PASS" "$FAIL"
[[ $FAIL -eq 0 ]]
