#!/usr/bin/env bash
set -u

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
EVIDENCE="$ROOT/evidence/portable-qa-install.txt"
TMP_HOME=""
TMP_CLAUDE_HOME=""

mkdir -p "$ROOT/evidence"
: > "$EVIDENCE"

cleanup() {
  if [ -n "$TMP_HOME" ]; then
    rm -rf "$TMP_HOME"
  fi
  if [ -n "$TMP_CLAUDE_HOME" ]; then
    rm -rf "$TMP_CLAUDE_HOME"
  fi
}
trap cleanup EXIT INT TERM

{
  echo "LazyClaude portable install QA"
  echo "ROOT: $ROOT"
  date
} >> "$EVIDENCE"

TMP_HOME="$(mktemp -d "${TMPDIR:-/tmp/}lazyclaude-portable-qa.XXXXXX")"
TMP_CLAUDE_HOME="$(mktemp -d "${TMPDIR:-/tmp/}lazyclaude-claude-qa.XXXXXX")"
export LAZYCLAUDE_HOME="$TMP_HOME"
export CLAUDE_CONFIG_DIR="$TMP_CLAUDE_HOME"

run_step() {
  local label="$1"
  shift
  {
    echo
    echo "## $label"
    "$@"
    echo "STATUS:$?"
  } >> "$EVIDENCE" 2>&1
  local status
  status="$(tail -n 1 "$EVIDENCE" | sed 's/^STATUS://')"
  if [ "$status" != "0" ]; then
    echo "${label}_FAIL"
    exit "$status"
  fi
}

run_step INSTALL node "$ROOT/bin/lazyclaude-ai.js" install

PLUGIN_PATH="$(node "$ROOT/bin/lazyclaude-ai.js" path)"
{
  echo
  echo "## PATH"
  echo "$PLUGIN_PATH"
} >> "$EVIDENCE"

if [ ! -f "$PLUGIN_PATH/.claude-plugin/plugin.json" ]; then
  echo "INSTALL_FAIL: plugin manifest missing" >> "$EVIDENCE"
  echo "INSTALL_FAIL"
  exit 1
fi
if [ ! -f "$TMP_CLAUDE_HOME/plugins/installed_plugins.json" ]; then
  echo "INSTALL_FAIL: Claude plugin registry missing" >> "$EVIDENCE"
  echo "INSTALL_FAIL"
  exit 1
fi
if [ ! -f "$TMP_CLAUDE_HOME/settings.json" ]; then
  echo "INSTALL_FAIL: Claude settings missing" >> "$EVIDENCE"
  echo "INSTALL_FAIL"
  exit 1
fi
if [ ! -f "$TMP_CLAUDE_HOME/plugins/known_marketplaces.json" ]; then
  echo "INSTALL_FAIL: Claude known marketplaces missing" >> "$EVIDENCE"
  echo "INSTALL_FAIL"
  exit 1
fi
if [ ! -f "$TMP_HOME/marketplaces/lazyclaude-ai/.claude-plugin/marketplace.json" ]; then
  echo "INSTALL_FAIL: LazyClaude local marketplace missing" >> "$EVIDENCE"
  echo "INSTALL_FAIL"
  exit 1
fi
node -e 'const fs = require("fs"); const p = process.argv[1]; const registry = JSON.parse(fs.readFileSync(`${p}/plugins/installed_plugins.json`, "utf8")); const entry = registry.plugins["lazyclaude@lazyclaude-ai"]?.[0]; if (!entry || entry.scope !== "user" || !entry.installPath.includes("/plugins/cache/lazyclaude-ai/lazyclaude/")) process.exit(1);' "$TMP_CLAUDE_HOME"
node -e 'const fs = require("fs"); const p = process.argv[1]; const h = process.argv[2]; const settings = JSON.parse(fs.readFileSync(`${p}/settings.json`, "utf8")); const market = settings.extraKnownMarketplaces?.["lazyclaude-ai"]; if (settings.enabledPlugins?.["lazyclaude@lazyclaude-ai"] !== true || market?.source?.source !== "directory" || market.source.path !== `${h}/marketplaces/lazyclaude-ai`) process.exit(1);' "$TMP_CLAUDE_HOME" "$TMP_HOME"
node -e 'const fs = require("fs"); const p = process.argv[1]; const h = process.argv[2]; const known = JSON.parse(fs.readFileSync(`${p}/plugins/known_marketplaces.json`, "utf8")); const market = known["lazyclaude-ai"]; if (market?.source?.source !== "directory" || market.installLocation !== `${h}/marketplaces/lazyclaude-ai`) process.exit(1);' "$TMP_CLAUDE_HOME" "$TMP_HOME"
echo "INSTALL_PASS" >> "$EVIDENCE"

run_step DOCTOR node "$ROOT/bin/lazyclaude-ai.js" doctor
echo "DOCTOR_PASS" >> "$EVIDENCE"

if command -v claude >/dev/null 2>&1; then
  run_step CLAUDE_PLUGIN_DETAILS claude plugin details lazyclaude@lazyclaude-ai
  if ! grep -Eq "Skills \\([0-9]+\\)" "$EVIDENCE" || ! grep -q "ulw-loop" "$EVIDENCE" || ! grep -q "ulw-plan" "$EVIDENCE" || ! grep -q "Hooks (4)" "$EVIDENCE"; then
    echo "CLAUDE_PLUGIN_DETAILS_FAIL: expected LazyClaude skills/hooks inventory" >> "$EVIDENCE"
    echo "CLAUDE_PLUGIN_DETAILS_FAIL"
    exit 1
  fi
else
  echo "CLAUDE_PLUGIN_DETAILS_SKIP: claude executable not found" >> "$EVIDENCE"
fi

run_step DRY_RUN_UNINSTALL node "$ROOT/bin/lazyclaude-ai.js" --dry-run uninstall
if [ ! -d "$PLUGIN_PATH" ]; then
  echo "DRY_RUN_UNINSTALL_FAIL: plugin removed during dry-run" >> "$EVIDENCE"
  exit 1
fi

run_step UNINSTALL node "$ROOT/bin/lazyclaude-ai.js" uninstall
if [ -e "$TMP_HOME/current" ] || [ -e "$TMP_HOME/lazyclaude-ai" ] || [ -e "$TMP_CLAUDE_HOME/plugins/cache/lazyclaude-ai" ]; then
  echo "UNINSTALL_FAIL: managed state remains" >> "$EVIDENCE"
  echo "UNINSTALL_FAIL"
  exit 1
fi
node -e 'const fs = require("fs"); const p = process.argv[1]; const settings = JSON.parse(fs.readFileSync(`${p}/settings.json`, "utf8")); if (settings.enabledPlugins?.["lazyclaude@lazyclaude-ai"] !== undefined) process.exit(1);' "$TMP_CLAUDE_HOME"
node -e 'const fs = require("fs"); const p = process.argv[1]; const settings = JSON.parse(fs.readFileSync(`${p}/settings.json`, "utf8")); const known = JSON.parse(fs.readFileSync(`${p}/plugins/known_marketplaces.json`, "utf8")); if (settings.extraKnownMarketplaces?.["lazyclaude-ai"] !== undefined || known["lazyclaude-ai"] !== undefined) process.exit(1);' "$TMP_CLAUDE_HOME"
echo "UNINSTALL_PASS" >> "$EVIDENCE"

cleanup
TMP_HOME=""
TMP_CLAUDE_HOME=""
echo "CLEANUP: removed portable QA temp home" >> "$EVIDENCE"
echo "PORTABLE_QA_PASS"
