{#- Context variables:
   - company: str (e.g., "ACME Bank")
   - industry: str (e.g., "fsi")
   - catalog: str (e.g., "acme_fsi_demo")
   - bundle_name: str (e.g., "acme-bank-fsi-demo")
   - app_name: str (e.g., "acme-bank-fsi-app")
   - warehouse_id: str
   - workspace_host: str
   - databricks_profile: str | None
   - lakebase_url: str | None
   - notification_email: str
-#}
#!/usr/bin/env bash
# Validate {{ demo_title }} deployment
# Usage: ./scripts/validate.sh [target]
set -euo pipefail

TARGET="${1:-dev}"
PASS=0
FAIL=0

echo "========================================="
echo "Validating {{ bundle_name }} (${TARGET})"
echo "========================================="

# Helper function
check() {
  local description="$1"
  local command="$2"
  echo -n "  [CHECK] ${description}... "
  if eval "${command}" > /dev/null 2>&1; then
    echo "PASS"
    PASS=$((PASS + 1))
  else
    echo "FAIL"
    FAIL=$((FAIL + 1))
  fi
}

# Section 1: Local Validation
echo ""
echo "--- Local Checks ---"
check "Bundle configuration valid" "databricks bundle validate"
check "Python lint (ruff)" "uv run ruff check src/"
check "Python types (mypy)" "uv run mypy src/"
check "Python tests (pytest)" "uv run pytest tests/ -v"
check "demo_manifest.json exists" "test -f conf/demo_manifest.json"
check "CLAUDE.md exists" "test -f CLAUDE.md"
check "databricks.yml exists" "test -f databricks.yml"
check "requirements.txt exists" "test -f requirements.txt"
check "setup_catalog.py exists" "test -f src/data_generation/setup_catalog.py"

# Section 2: Structure Validation
echo ""
echo "--- Structure Checks ---"
check "src/data_generation/ exists" "test -d src/data_generation"
check "src/pipelines/ exists" "test -d src/pipelines"
check "src/ml/ exists" "test -d src/ml"
check "src/dashboards/ exists" "test -d src/dashboards"
check "resources/jobs/ exists" "test -d resources/jobs"
check "resources/pipelines/ exists" "test -d resources/pipelines"
check "docs/ exists" "test -d docs"
check "data_generation CLAUDE.md exists" "test -f src/data_generation/CLAUDE.md"
check "pipelines CLAUDE.md exists" "test -f src/pipelines/CLAUDE.md"
check "ml CLAUDE.md exists" "test -f src/ml/CLAUDE.md"

# Section 3: No Secrets Check
echo ""
echo "--- Security Checks ---"
check "No .env file committed" "! test -f .env"
check "No hardcoded secrets" "! grep -r 'password\|secret\|api_key' src/ --include='*.py' -l"

# Summary
echo ""
echo "========================================="
echo "Results: ${PASS} passed, ${FAIL} failed"
echo "========================================="

if [ "${FAIL}" -gt 0 ]; then
  echo "VALIDATION FAILED -- fix the issues above before deploying."
  exit 1
else
  echo "ALL CHECKS PASSED"
  exit 0
fi
