#!/usr/bin/env bash
# Validate docs/roadmap/ before pushing if any doc files changed in the push range.
# Mirrors the docs-validate CI job to catch errors locally.

remote="$1"
REPO_ROOT="$(git rev-parse --show-toplevel)"

while read local_ref local_sha remote_ref remote_sha; do
  # Skip delete pushes
  [ "$local_sha" = "0000000000000000000000000000000000000000" ] && continue

  if [ "$remote_sha" = "0000000000000000000000000000000000000000" ]; then
    range="$local_sha"
  else
    range="$remote_sha..$local_sha"
  fi

  # Check if any docs/roadmap files changed in the push range
  if git diff --name-only "$range" -- docs/roadmap/ 2>/dev/null | grep -q .; then
    echo "Validating docs/roadmap/ before push..."
    if ! rootline validate --all docs/roadmap/ > /dev/null 2>&1; then
      echo "rootline validate --all docs/roadmap/ failed. Fix errors before pushing."
      echo "Run: rootline validate --all docs/roadmap/"
      echo "Or:  rootline fix --all docs/roadmap/"
      exit 1
    fi
    echo "docs/roadmap/ validation passed"
  fi

  # Check if CLI code changed but docs or skills weren't updated
  if git diff --name-only "$range" -- cmd/rootline/ 2>/dev/null | grep -q .; then
    missing=""
    if ! git diff --name-only "$range" -- docs/ README.md CLAUDE.md 2>/dev/null | grep -q .; then
      missing="docs"
    fi
    if ! git diff --name-only "$range" -- .agents/skills/rootline/ 2>/dev/null | grep -q .; then
      missing="${missing:+$missing + }.agents/skills/rootline/"
    fi
    if [ -n "$missing" ]; then
      echo "CLI code changed but $missing were not updated."
      echo "Update the command reference in docs/ (and README.md/CLAUDE.md if affected) and .agents/skills/rootline/ in this push."
      exit 1
    fi
  fi

  # Check if CI, install scripts, or build config changed but docs weren't updated
  if git diff --name-only "$range" -- .github/ .goreleaser.yml install.sh install.ps1 Justfile go.mod 2>/dev/null | grep -q .; then
    if ! git diff --name-only "$range" -- README.md docs/ CLAUDE.md CHANGELOG.md 2>/dev/null | grep -q .; then
      echo "CI/scripts/config changed but docs were not updated."
      echo "Update README.md, docs/, CHANGELOG.md, or CLAUDE.md before pushing."
      exit 1
    fi
  fi

  # Check coverage floors if any Go source files changed (skip docs-only pushes).
  # Requires just coverage-check to be available (added by T006).
  if git diff --name-only "$range" -- '*.go' 2>/dev/null | grep -q .; then
    echo "Checking coverage..."
    # git exports GIT_DIR/GIT_WORK_TREE/GIT_INDEX_FILE/GIT_OBJECT_DIRECTORY into hook
    # processes. This used to need an `env -u ...` wrapper here, because tests that
    # shell out to git inherited them and wrote into the real repository (issue #81).
    # The isolation now lives in internal/gitenv, which every git invocation that must
    # target its own directory goes through, so no wrapper is needed.
    if ! just coverage-check > /tmp/rootline-cov.log 2>&1; then
      cat /tmp/rootline-cov.log
      echo "Coverage below threshold. Run: just coverage-check"
      exit 1
    fi
    echo "Coverage check passed."
  fi

done
