#!/usr/bin/env bash
# Step 09: Run check-prerequisites.sh in the VM and verify results.
# Cloud Repo and env files are expected to be MISSING (not testable without auth).

step_name="Final prerequisite check"

# Components that should be OK after our test run
EXPECTED_OK=(
  "WSL installed"
  "Volta"
  "Node.js"
  "Yarn"
  "tsc"
  "ts-node"
  "ts-mocha"
  "Python"
  "webpack"
)

# WSL components (only checked if --skip-wsl was NOT set)
EXPECTED_OK_WSL=(
  "Redis"
  "redis-cli"
  "PostgreSQL"
  "localv1db"
)

# Components that are expected to be MISSING (can't test without private repo)
EXPECTED_MISSING=(
  "Cloud repo"
  "node_modules"
  ".env"
)

run() {
  # Copy the latest check-prerequisites.sh into the VM
  vm_exec copy-to "$SKILL_DIR/bin/check-prerequisites.sh" "$GUEST_TEMP\\check-prerequisites.sh"

  # Git Bash should be available via Volta's node install or we use WSL bash.
  # Run the check script via WSL's bash since Windows doesn't have native bash.
  log_info "Running check-prerequisites.sh in the VM..."

  # Copy the script to a WSL-accessible location and run it
  vm_exec cmd "wsl -d Ubuntu -- bash -c \"cp /mnt/c/bigstack-test/check-prerequisites.sh /tmp/ && CLOUD_DIR=/nonexistent bash /tmp/check-prerequisites.sh\"" \
    --timeout 120 || true
}

verify() {
  # The check script will report issues for Cloud Repo and env files (expected).
  # We verify by running individual tool checks directly.
  local pass=0 fail=0

  log_info "Verifying Windows-side tools..."
  for tool_cmd in "volta --version" "node --version" "yarn --version" "tsc --version" "ts-node --version" "ts-mocha --version" "python --version" "webpack --version"; do
    local tool_name="${tool_cmd%% *}"
    if vm_exec cmd "$tool_cmd" &>/dev/null; then
      log_pass "$tool_name"
      pass=$((pass + 1))
    else
      log_fail "$tool_name not found"
      fail=$((fail + 1))
    fi
  done

  if [[ "${SKIP_WSL:-false}" != "true" ]]; then
    log_info "Verifying WSL-side services..."
    local redis_ping
    redis_ping=$(vm_exec wsl "redis-cli ping" 2>/dev/null || echo "")
    if [[ "$redis_ping" == *"PONG"* ]]; then
      log_pass "Redis responding"
      pass=$((pass + 1))
    else
      log_fail "Redis not responding"
      fail=$((fail + 1))
    fi

    local pg_ready
    pg_ready=$(vm_exec wsl "pg_isready" 2>/dev/null || echo "")
    if echo "$pg_ready" | grep -q "accepting connections"; then
      log_pass "PostgreSQL accepting connections"
      pass=$((pass + 1))
    else
      log_fail "PostgreSQL not accepting connections"
      fail=$((fail + 1))
    fi

    local db_check
    db_check=$(vm_exec wsl "sudo -u postgres psql -lqt" 2>/dev/null || echo "")
    if echo "$db_check" | grep -q "localv1db"; then
      log_pass "localv1db exists"
      pass=$((pass + 1))
    else
      log_fail "localv1db not found"
      fail=$((fail + 1))
    fi
  fi

  echo ""
  log_info "Final: $pass passed, $fail failed"
  [[ $fail -eq 0 ]]
}
