#!/bin/bash
# health_check.sh — pre-bid dependency verification for claude-code-opsman
# Runs all the tooling checks listed in agent-claude-code-opsman.md
# Exit code 0 = all good. Non-zero = at least one dep missing.

PASS=0
FAIL=0

check() {
    local name="$1"
    local cmd="$2"
    local fix_hint="$3"
    if eval "$cmd" >/dev/null 2>&1; then
        echo "  [PASS] $name"
        PASS=$((PASS + 1))
    else
        echo "  [FAIL] $name"
        echo "         fix: $fix_hint"
        FAIL=$((FAIL + 1))
    fi
}

echo "===== LFCS bid pipeline health check ====="
echo ""

echo "1. Python + libs"
check "Python 3.12+" "python --version | grep -E 'Python 3\.(1[2-9]|[2-9][0-9])'" "install Python 3.12+ from python.org"
check "openpyxl"     "python -c 'import openpyxl'"     "pip install openpyxl"
check "reportlab"    "python -c 'import reportlab'"    "pip install reportlab"
check "python-docx"  "python -c 'import docx'"         "pip install python-docx"
check "pdf2image"    "python -c 'import pdf2image'"    "pip install pdf2image"
check "anthropic"    "python -c 'import anthropic'"    "pip install anthropic   (only needed for API-path vision fallback)"

echo ""
echo "2. Poppler / pdftoppm (for PDF vision-pass via Read tool)"
check "pdftoppm in PATH" "where pdftoppm" "install Poppler from https://github.com/oschwartz10612/poppler-windows/releases — extract to C:\\Users\\mclou\\tools\\poppler\\ and add bin to PATH"

echo ""
echo "3. gws CLI + opsman config"
check "gws CLI"     "where gws"                                              "npm install -g @google-workspace/gws-cli"
check "gws-opsman config dir" "ls 'C:\\Users\\mclou\\.config\\gws-opsman'"   "see reference_opsman_gws_cli.md memory file"
check "gws-opsman client_secret"  "test -f 'C:\\Users\\mclou\\.config\\gws-opsman\\client_secret.json'"  "(re)authorise as opsman.systems@gmail.com"
check "gws-opsman credentials"    "test -f 'C:\\Users\\mclou\\.config\\gws-opsman\\credentials.enc'"     "(re)authorise as opsman.systems@gmail.com"

echo ""
echo "4. gws auth round-trip (smoke test — list one Drive file)"
check "gws auth works" "GOOGLE_WORKSPACE_CLI_CONFIG_DIR='C:\\Users\\mclou\\.config\\gws-opsman' gws drive files list --params '{\"pageSize\":1,\"fields\":\"files(id)\"}' | grep -q 'files'" "run 'gws auth' to re-authorise"

echo ""
echo "5. Vault structure"
check "vault root exists"               "test -d 'HQ-Vault'"  "you may not be in the rateright-growth-deploy repo root"
check "_System/CLAUDE.md exists"        "test -f 'HQ-Vault/_System/CLAUDE.md'" "vault corrupted — git status?"
check "11_OpsMan_LFCS/_Brain exists"    "test -d 'HQ-Vault/11_OpsMan_LFCS/_Brain'" "vault corrupted"
check "agent-claude-code-opsman doc"    "test -f 'HQ-Vault/11_OpsMan_LFCS/_Brain/agent-claude-code-opsman.md'" "git pull — onboarding doc not yet on this checkout"
check "lfcs-drive-upload.sh helper"     "test -f 'scripts/lfcs/lfcs-drive-upload.sh'" "git pull — helper not yet on this checkout"
check "build_2629_pricing.py template"  "test -f 'scripts/lfcs/build_2629_pricing.py'" "git pull"
check "pricing-checklist.md"            "test -f 'HQ-Vault/11_OpsMan_LFCS/Operations/lfcs-standards/pricing-checklist.md'" "git pull"

echo ""
echo "6. Git remote + branch"
check "origin/main remote configured" "git remote get-url origin | grep -q rateright-growth" "git remote add origin https://github.com/mcloughlinmichaelr-debug/rateright-growth.git"
check "on main branch"                "git rev-parse --abbrev-ref HEAD | grep -q '^main$'"   "git checkout main"

echo ""
echo "7. Skill files (CC sees them automatically; this just confirms presence on disk)"
check "lfcs-project-init"            "test -f 'HQ-Vault/11_OpsMan_LFCS/Operations/skills/lfcs-project-init/SKILL.md'" "git pull"
check "lfcs-drawing-extract-text"    "test -f 'HQ-Vault/11_OpsMan_LFCS/Operations/skills/lfcs-drawing-extract-text/SKILL.md'" "git pull"
check "lfcs-drawing-extract-vision"  "test -f 'HQ-Vault/11_OpsMan_LFCS/Operations/skills/lfcs-drawing-extract-vision/SKILL.md'" "git pull"
check "lfcs-airtable-sync"           "test -f 'HQ-Vault/11_OpsMan_LFCS/Operations/skills/lfcs-airtable-sync/SKILL.md'" "git pull"
check "lfcs-bid-prep-doc"            "test -f 'HQ-Vault/11_OpsMan_LFCS/Operations/skills/lfcs-bid-prep-doc/SKILL.md'" "git pull"
check "lfcs-incl-excl-draft"         "test -f 'HQ-Vault/11_OpsMan_LFCS/Operations/skills/lfcs-incl-excl-draft/SKILL.md'" "git pull"

echo ""
echo "===== Summary ====="
echo "PASS: $PASS"
echo "FAIL: $FAIL"
if [ "$FAIL" -gt 0 ]; then
    echo ""
    echo "Fix the failing items above before running any bid pipeline. See agent-claude-code-opsman.md tooling table for context."
    exit 1
fi
echo ""
echo "All green. Ready to run the bid runbook."
exit 0
