#!/bin/bash
# TypeDialog - Dependency Checker
# Verifies all critical dependencies for building TypeDialog

set -e

echo "=== Checking Critical Dependencies ==="
echo ""

ERRORS=0

# Check just
if command -v just &> /dev/null; then
    echo "✓ just $(just --version)"
else
    echo "✗ just - NOT FOUND"
    echo "  Install: cargo install just"
    echo "  Or macOS: brew install just"
    ERRORS=$((ERRORS + 1))
fi

# Check nickel
if command -v nickel &> /dev/null; then
    echo "✓ nickel $(nickel --version)"
else
    echo "✗ nickel - NOT FOUND"
    echo "  Install: cargo install nickel-lang-cli"
    echo "  Or macOS: brew install nickel"
    ERRORS=$((ERRORS + 1))
fi

# Check cargo (should always be present but verify)
if command -v cargo &> /dev/null; then
    echo "✓ cargo $(cargo --version)"
else
    echo "✗ cargo - NOT FOUND"
    echo "  Install Rust: https://rustup.rs/"
    ERRORS=$((ERRORS + 1))
fi

# Check prov-ecosystem (path dependency)
# Check shellcheck
if command -v shellcheck &> /dev/null; then
    echo "✓ shellcheck $(shellcheck --version | head -1)"
else
    echo "✗ shellcheck - NOT FOUND"
    echo "  Install: brew install shellcheck (macOS) or apt install shellcheck (Linux)"
    ERRORS=$((ERRORS + 1))
fi

# Check nushell
if command -v nu &> /dev/null; then
    echo "✓ nushell $(nu --version)"
else
    echo "✗ nushell - NOT FOUND"
    echo "  Install: brew install nushell (macOS) or cargo install nu"
    ERRORS=$((ERRORS + 1))
fi

# Check markdownlint-cli2
if command -v markdownlint-cli2 &> /dev/null; then
    echo "✓ markdownlint-cli2 $(markdownlint-cli2 --version)"
else
    echo "✗ markdownlint-cli2 - NOT FOUND"
    echo "  Install: npm install -g markdownlint-cli2"
    ERRORS=$((ERRORS + 1))
fi

echo ""
echo "=== Checking Internal Dependencies ==="
if cargo metadata --format-version=1 2>/dev/null | grep -q prov-ecosystem; then
    echo "✓ prov-ecosystem - FOUND"
else
    echo "✗ prov-ecosystem - NOT FOUND"
    echo "  Clone to: ../prov-ecosystem"
    echo "  Or run: git clone https://github.com/your-org/prov-ecosystem.git ../prov-ecosystem"
    ERRORS=$((ERRORS + 1))
fi

# Check secretumvault (path dependency)
if cargo metadata --format-version=1 2>/dev/null | grep -q secretumvault; then
    echo "✓ secretumvault - FOUND"
else
    echo "✗ secretumvault - NOT FOUND"
    echo "  Clone to: ../secretumvault"
    echo "  Or run: git clone https://github.com/your-org/secretumvault.git ../secretumvault"
    ERRORS=$((ERRORS + 1))
fi

# Summary
echo ""
echo "=== Summary ==="
if [ $ERRORS -eq 0 ]; then
    echo "✓ All dependencies found!"
    echo "  You can now build TypeDialog with: just build::all"
    exit 0
else
    echo "✗ $ERRORS dependencies missing"
    echo "  Fix the issues above before building"
    exit 1
fi
