#!/usr/bin/env bash
# Ensure prerequisites for: LINK funding, PMM (Chain 138), CCIPLogger deploy.
# Run from repo root (proxmox) or smom-dbis-138. Fixes what can be automated and reports the rest.
# Usage: ./scripts/deployment/ensure-prerequisites.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# PROJECT_ROOT = smom-dbis-138
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$PROJECT_ROOT"
# Load .env via dotenv (RPC CR/LF trim). Fallback: raw source.
if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "${PROJECT_ROOT:-$REPO_ROOT}"
elif [[ -n "${PROJECT_ROOT:-}" && -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
elif [[ -n "${REPO_ROOT:-}" && -f "$REPO_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$REPO_ROOT/.env"
  set +a
fi

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

ok()  { echo -e "${GREEN}✓${NC} $1"; }
warn() { echo -e "${YELLOW}⚠${NC} $1"; }
fail() { echo -e "${RED}✗${NC} $1"; }

echo "=== Prerequisites (LINK funding, PMM, CCIPLogger) ==="
echo ""

# 1) .env
if [[ ! -f "$PROJECT_ROOT/.env" ]]; then
  fail ".env not found in $PROJECT_ROOT"
  exit 1
fi
ok ".env exists"
set -a
source "$PROJECT_ROOT/.env" 2>/dev/null || true
set +a

[[ -z "${PRIVATE_KEY:-}" ]] && fail "PRIVATE_KEY not set in .env" || ok "PRIVATE_KEY set"
[[ -z "${RPC_URL_138:-}" ]] && warn "RPC_URL_138 not set" || ok "RPC_URL_138 set"

# 2) npm install in smom-dbis-138 (for Hardhat / CCIPLogger)
echo ""
echo "--- Dependencies (CCIPLogger / Hardhat) ---"
if [[ -f "$PROJECT_ROOT/package.json" ]]; then
  if [[ ! -d "$PROJECT_ROOT/node_modules/hardhat" ]]; then
    warn "Hardhat not found in node_modules. Running: npm install --legacy-peer-deps"
    npm install --legacy-peer-deps 2>/dev/null || true
  fi
  if [[ -d "$PROJECT_ROOT/node_modules/hardhat" ]]; then
    ok "Hardhat available (node_modules)"
  else
    warn "Hardhat still missing; run from $PROJECT_ROOT: npm install --legacy-peer-deps"
  fi
else
  warn "No package.json in $PROJECT_ROOT"
fi

# 3) LINK funding prereqs
echo ""
echo "--- LINK funding ---"
[[ -z "${LINK_TOKEN_CHAIN138:-${LINK_TOKEN:-}}" ]] && warn "LINK_TOKEN_CHAIN138/LINK_TOKEN not set" || ok "Chain 138 LINK token set"
[[ -z "${CCIPWETH9_BRIDGE_CHAIN138:-}" ]] && warn "CCIPWETH9_BRIDGE_CHAIN138 not set" || ok "CCIPWETH9_BRIDGE_CHAIN138 set"
[[ -z "${MAINNET_CCIP_WETH9_BRIDGE:-}" ]] && warn "MAINNET_CCIP_WETH9_BRIDGE not set" || ok "Mainnet bridge vars set"
echo "  Deployer must have LINK on each chain. Run: scripts/deployment/fund-ccip-bridges-with-link.sh (use DRY_RUN=1 first)."

# 4) PMM prereqs
echo ""
echo "--- PMM (Chain 138) ---"
if [[ -z "${DODO_VENDING_MACHINE_ADDRESS:-}" || "${DODO_VENDING_MACHINE_ADDRESS}" =~ ^0x?0*$ ]]; then
  warn "DODO_VENDING_MACHINE_ADDRESS not set. Set to DODO DVM factory on Chain 138 to run PMM (see docs/deployment/ALL_MAINNETS_DEPLOYMENT_RUNBOOK.md)."
else
  ok "DODO_VENDING_MACHINE_ADDRESS set"
fi
[[ -z "${COMPLIANT_USDT_ADDRESS:-}" ]] && warn "COMPLIANT_USDT_ADDRESS not set" || ok "COMPLIANT_USDT_ADDRESS set"
[[ -z "${COMPLIANT_USDC_ADDRESS:-}" ]] && warn "COMPLIANT_USDC_ADDRESS not set" || ok "COMPLIANT_USDC_ADDRESS set"
[[ -z "${OFFICIAL_USDT_ADDRESS:-}" ]] && warn "OFFICIAL_USDT_ADDRESS not set" || ok "OFFICIAL_USDT_ADDRESS set"
[[ -z "${OFFICIAL_USDC_ADDRESS:-}" ]] && warn "OFFICIAL_USDC_ADDRESS not set" || ok "OFFICIAL_USDC_ADDRESS set"
echo "  Then run: scripts/deployment/run-pmm-and-pools.sh"

# 5) CCIPLogger
echo ""
echo "--- CCIPLogger ---"
echo "  Optional. From $PROJECT_ROOT run: scripts/deployment/deploy-ccip-logger-all-chains.sh"
echo "  If Hardhat compile fails (e.g. missing @emoney/interfaces), fix deps or skip CCIPLogger."
echo ""
echo "=== Done. Address any ⚠ items above, then run the task scripts. ==="
