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

tmp_dir="$(mktemp -d)"
cleanup() {
  rm -rf "$tmp_dir"
}
trap cleanup EXIT

repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
agentrail="${repo_dir}/scripts/agentrail"

assert_grep() {
  local pattern="$1"
  local file="$2"
  local message="$3"

  if ! grep -q -- "$pattern" "$file"; then
    echo "$message" >&2
    echo "--- output ---" >&2
    cat "$file" >&2
    exit 1
  fi
}

assert_not_grep() {
  local pattern="$1"
  local file="$2"
  local message="$3"

  if grep -q -- "$pattern" "$file"; then
    echo "$message" >&2
    echo "--- output ---" >&2
    cat "$file" >&2
    exit 1
  fi
}

make_fixture() {
  local fixture="$1"
  mkdir -p "$fixture"
  git -C "$fixture" init --quiet
  "$agentrail" install --target "$fixture" >/dev/null
}

base="${tmp_dir}/base"
make_fixture "$base"

"$agentrail" skills list --target "$base" >"${tmp_dir}/skills-list.out"
assert_grep "frontend-web" "${tmp_dir}/skills-list.out" "skills list did not print frontend-web"
assert_grep "skills/frontend-web/SKILL.md" "${tmp_dir}/skills-list.out" "skills list did not print local skill paths"
assert_grep "Build and modify web frontends" "${tmp_dir}/skills-list.out" "skills list did not print short descriptions"

"$agentrail" skills resolve "Build a React component" --target "$base" >"${tmp_dir}/resolve-react.out"
assert_grep "frontend-web" "${tmp_dir}/resolve-react.out" "React task did not resolve frontend-web"
assert_grep "task keyword: react" "${tmp_dir}/resolve-react.out" "React resolution did not include a concrete reason"

tauri="${tmp_dir}/tauri"
make_fixture "$tauri"
mkdir -p "$tauri/src-tauri" "$tauri/src"
cat >"${tauri}/package.json" <<'JSON'
{
  "dependencies": {
    "@tauri-apps/api": "latest",
    "react": "latest",
    "vite": "latest"
  }
}
JSON
touch "$tauri/src-tauri/tauri.conf.json" "$tauri/src/App.tsx"
"$agentrail" skills resolve "Fix app window behavior" --target "$tauri" >"${tmp_dir}/resolve-tauri.out"
assert_grep "desktop-tauri" "${tmp_dir}/resolve-tauri.out" "Tauri fixture did not resolve desktop-tauri"
assert_grep "frontend-web" "${tmp_dir}/resolve-tauri.out" "Tauri fixture did not also resolve frontend-web"

"$agentrail" skills resolve "fix Tauri desktop UI bug" --target "$tauri" >"${tmp_dir}/resolve-tauri-ui.out"
assert_grep "desktop-tauri" "${tmp_dir}/resolve-tauri-ui.out" "Requested Tauri UI fixture did not resolve desktop-tauri"
assert_grep "frontend-web" "${tmp_dir}/resolve-tauri-ui.out" "Requested Tauri UI fixture did not resolve frontend-web"
assert_grep "docs-current" "${tmp_dir}/resolve-tauri-ui.out" "Requested Tauri UI fixture did not resolve docs-current"
assert_grep "reason:" "${tmp_dir}/resolve-tauri-ui.out" "Requested Tauri UI fixture did not include resolution reasons"

rust="${tmp_dir}/rust"
make_fixture "$rust"
cat >"${rust}/Cargo.toml" <<'TOML'
[package]
name = "plain-rust"
version = "0.1.0"
edition = "2021"
TOML
"$agentrail" skills resolve "Refactor Rust error handling" --target "$rust" >"${tmp_dir}/resolve-rust.out"
assert_not_grep "desktop-tauri" "${tmp_dir}/resolve-rust.out" "Plain Rust fixture resolved desktop-tauri unexpectedly"

tauri_src="${tmp_dir}/tauri-src"
make_fixture "$tauri_src"
mkdir -p "$tauri_src/src-tauri"
touch "$tauri_src/src-tauri/Cargo.toml"
"$agentrail" skills resolve "Fix app window behavior" --target "$tauri_src" >"${tmp_dir}/resolve-tauri-src.out"
assert_grep "desktop-tauri" "${tmp_dir}/resolve-tauri-src.out" "src-tauri fixture did not resolve desktop-tauri"

tauri_config="${tmp_dir}/tauri-config"
make_fixture "$tauri_config"
touch "$tauri_config/tauri.conf.json"
"$agentrail" skills resolve "Fix app window behavior" --target "$tauri_config" >"${tmp_dir}/resolve-tauri-config.out"
assert_grep "desktop-tauri" "${tmp_dir}/resolve-tauri-config.out" "Tauri config fixture did not resolve desktop-tauri"

tauri_dep="${tmp_dir}/tauri-dep"
make_fixture "$tauri_dep"
cat >"${tauri_dep}/package.json" <<'JSON'
{
  "dependencies": {
    "@tauri-apps/api": "latest"
  }
}
JSON
"$agentrail" skills resolve "Fix app window behavior" --target "$tauri_dep" >"${tmp_dir}/resolve-tauri-dep.out"
assert_grep "desktop-tauri" "${tmp_dir}/resolve-tauri-dep.out" "@tauri-apps dependency fixture did not resolve desktop-tauri"

backend="${tmp_dir}/backend"
make_fixture "$backend"
mkdir -p "$backend/server/routes"
touch "$backend/server/routes/users.ts"
"$agentrail" skills resolve "Add validation to the API endpoint" --target "$backend" >"${tmp_dir}/resolve-backend.out"
assert_grep "backend-api" "${tmp_dir}/resolve-backend.out" "Backend fixture did not resolve backend-api"

devops="${tmp_dir}/devops"
make_fixture "$devops"
mkdir -p "$devops/.github/workflows"
touch "$devops/.github/workflows/ci.yml" "$devops/Dockerfile"
"$agentrail" skills resolve "Update CI deploy config" --target "$devops" >"${tmp_dir}/resolve-devops.out"
assert_grep "devops-deploy" "${tmp_dir}/resolve-devops.out" "Devops fixture did not resolve devops-deploy"

docs="${tmp_dir}/docs"
make_fixture "$docs"
mkdir -p "$docs/docs"
touch "$docs/docs/api.md"
"$agentrail" skills resolve "Check the latest SDK documentation" --target "$docs" >"${tmp_dir}/resolve-docs.out"
assert_grep "docs-current" "${tmp_dir}/resolve-docs.out" "Docs fixture did not resolve docs-current"

empty="${tmp_dir}/empty"
make_fixture "$empty"
"$agentrail" skills resolve "Tidy small internal task" --target "$empty" >"${tmp_dir}/resolve-empty.out"
assert_grep "No skills resolved." "${tmp_dir}/resolve-empty.out" "Unrelated fixture should not resolve every bundled skill"
assert_not_grep "frontend-web" "${tmp_dir}/resolve-empty.out" "Unrelated fixture resolved frontend-web unexpectedly"

"$agentrail" prompt issue 123 --skill frontend-web --no-auto-skills --target "$base" >"${tmp_dir}/prompt-explicit.out"
assert_grep "frontend-web" "${tmp_dir}/prompt-explicit.out" "Explicit prompt skill was not included"
assert_grep "path: skills/frontend-web/SKILL.md" "${tmp_dir}/prompt-explicit.out" "Explicit prompt skill did not include the local SKILL.md path"
assert_grep "explicit --skill" "${tmp_dir}/prompt-explicit.out" "Explicit prompt skill did not include a reason"
assert_grep "Read these SKILL.md files before editing" "${tmp_dir}/prompt-explicit.out" "Prompt did not tell workers to read resolved skills before editing"
assert_grep "does not apply after inspection" "${tmp_dir}/prompt-explicit.out" "Prompt did not tell workers to report non-applicable resolved skills"

"$agentrail" prompt issue 123 --no-auto-skills --target "$tauri" >"${tmp_dir}/prompt-no-auto.out"
assert_not_grep "desktop-tauri" "${tmp_dir}/prompt-no-auto.out" "--no-auto-skills did not disable automatic matches"
assert_grep "Automatic skill resolution disabled" "${tmp_dir}/prompt-no-auto.out" "--no-auto-skills was not visible in the prompt"

"$agentrail" prompt issue 123 --target "$tauri" >"${tmp_dir}/prompt-tauri.out"
assert_grep "desktop-tauri" "${tmp_dir}/prompt-tauri.out" "Tauri prompt did not include desktop-tauri"
assert_grep "frontend-web" "${tmp_dir}/prompt-tauri.out" "Tauri prompt did not include frontend-web"
assert_grep "reason:" "${tmp_dir}/prompt-tauri.out" "Tauri prompt did not include reasons"

if "$agentrail" prompt issue 123 --skill does-not-exist --target "$base" >"${tmp_dir}/unknown-skill.out" 2>&1; then
  echo "prompt issue accepted an unknown explicit skill" >&2
  exit 1
fi
assert_grep "Unknown skill: does-not-exist" "${tmp_dir}/unknown-skill.out" "Unknown explicit skill did not fail clearly"

uninstalled="${tmp_dir}/uninstalled"
mkdir -p "$uninstalled"
if "$agentrail" prompt issue 123 --skill frontend-web --target "$uninstalled" >"${tmp_dir}/unavailable-skill.out" 2>&1; then
  echo "prompt issue accepted an unavailable explicit skill" >&2
  exit 1
fi
assert_grep "Unavailable skill: frontend-web" "${tmp_dir}/unavailable-skill.out" "Unavailable explicit skill did not fail clearly"

echo "skill resolution test passed"
