#!/usr/bin/env bash
# smoke-graph-builder.sh — end-to-end smoke for `agentteam-insight graph`.
#
# Builds agentteam-insight, runs `graph --mode full` against the bundled tiny
# fixture repo, asserts the graph artifacts plus deterministic review.json are
# produced, and pipes the generated knowledge-graph.json through the
# compatibility validator.
#
# Usage:
#   scripts/smoke-graph-builder.sh                  # uses bundled fixture
#   scripts/smoke-graph-builder.sh /path/to/repo    # smoke a real repo
#   UNDERSTAND_ANYTHING_ROOT=/ua scripts/smoke-graph-builder.sh
#
# Exits non-zero if any step fails. Requires Go toolchain; UA parity step
# additionally requires UNDERSTAND_ANYTHING_ROOT and a built UA packages/core
# (run `pnpm --filter @understand-anything/core build` in the UA plugin dir).
set -euo pipefail

root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
fixture_default="${root}/testdata/smoke/fixture-repo"
fixture="${1:-${fixture_default}}"

if [[ ! -d "${fixture}" ]]; then
  echo "fixture repo not found: ${fixture}" >&2
  exit 2
fi

workdir="$(mktemp -d)"
trap 'rm -rf "${workdir}"' EXIT

# 1. Copy the fixture so the smoke run never mutates the source fixture.
repo="${workdir}/repo"
cp -R "${fixture}" "${repo}"

# 2. Build the agentteam-insight binary.
echo "[smoke] building agentteam-insight"
bin="${workdir}/agentteam-insight"
( cd "${root}" && go build -o "${bin}" . )

# 3. Run full mode.
echo "[smoke] running agentteam-insight graph --mode full --no-prompt on ${repo}"
"${bin}" graph --input "${repo}" --mode full --no-prompt

graph_dir="${repo}/.agentteam-insight"
graph_path="${graph_dir}/knowledge-graph.json"
meta_path="${graph_dir}/meta.json"
fingerprints_path="${graph_dir}/fingerprints.json"
config_path="${graph_dir}/config.json"
review_path="${graph_dir}/intermediate/review.json"
domain_graph_path="${graph_dir}/domain-graph.json"

# 4. Assert persisted graph artifacts and review artifact; domain-graph is
# intentionally not produced by this graph builder contract.
for f in "${graph_path}" "${meta_path}" "${fingerprints_path}" "${config_path}"; do
  if [[ ! -s "${f}" ]]; then
    echo "missing or empty artifact: ${f}" >&2
    exit 1
  fi
done
if [[ ! -s "${review_path}" ]]; then
  echo "missing or empty review artifact: ${review_path}" >&2
  exit 1
fi
if [[ -e "${domain_graph_path}" ]]; then
  echo "unexpected domain-graph.json: ${domain_graph_path}" >&2
  exit 1
fi
echo "[smoke] artifacts OK: knowledge-graph.json, meta.json, fingerprints.json, config.json, intermediate/review.json (no domain-graph.json)"

# 5. Run the compatibility validator on the produced graph when available.
ua_root="${UNDERSTAND_ANYTHING_ROOT:-}"
if [[ -z "${ua_root}" ]]; then
  if [[ -d "${HOME}/devel/ai/Understand-Anything" ]]; then
    ua_root="${HOME}/devel/ai/Understand-Anything"
  fi
fi
if [[ -z "${ua_root}" ]]; then
  echo "[smoke] UNDERSTAND_ANYTHING_ROOT unset — skipping UA parity (AgentTeam CI behavior)"
  exit 0
fi
if [[ ! -d "${ua_root}/understand-anything-plugin/packages/core/dist" ]]; then
  echo "[smoke] UA packages/core not built — skipping UA parity (run pnpm build in ${ua_root}/understand-anything-plugin)"
  exit 0
fi

echo "[smoke] running UA validateGraph via ${ua_root}"
UNDERSTAND_ANYTHING_ROOT="${ua_root}" node "${root}/scripts/validate-ua-graph.mjs" "${graph_path}" >"${workdir}/ua-result.json"
if grep -q '"success":true' "${workdir}/ua-result.json"; then
  echo "[smoke] UA validateGraph passed"
else
  echo "[smoke] UA validateGraph failed:" >&2
  cat "${workdir}/ua-result.json" >&2
  exit 1
fi
