#!/bin/bash
# check-dependencies.sh
# Checks if all required dependencies are installed

set -euo pipefail

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

MISSING=0
OPTIONAL_MISSING=0

check_required() {
    local cmd=$1
    local name=$2
    
    if command -v "$cmd" &> /dev/null; then
        echo -e "${GREEN}✓${NC} $name"
        return 0
    else
        echo -e "${RED}✗${NC} $name (REQUIRED)"
        ((MISSING++))
        return 1
    fi
}

check_optional() {
    local cmd=$1
    local name=$2
    
    if command -v "$cmd" &> /dev/null; then
        echo -e "${GREEN}✓${NC} $name (optional)"
        return 0
    else
        echo -e "${YELLOW}⚠${NC} $name (optional, not installed)"
        ((OPTIONAL_MISSING++))
        return 1
    fi
}

main() {
    echo ""
    echo "╔══════════════════════════════════════════════════════════════╗"
    echo "║          Dependency Check                                    ║"
    echo "╚══════════════════════════════════════════════════════════════╝"
    echo ""
    
    echo "Required Dependencies:"
    echo "----------------------"
    check_required "kubectl" "kubectl (Kubernetes CLI)"
    check_required "curl" "curl"
    check_required "jq" "jq (JSON processor)"
    echo ""
    
    echo "Optional Dependencies:"
    echo "----------------------"
    check_optional "go" "Go (for building provider)"
    check_optional "make" "make (for building)"
    check_optional "docker" "Docker (for container builds)"
    check_optional "kind" "kind (for local Kubernetes)"
    check_optional "terraform" "Terraform (for infrastructure)"
    check_optional "yamllint" "yamllint (for YAML validation)"
    check_optional "dig" "dig (for DNS testing)"
    check_optional "nslookup" "nslookup (for DNS testing)"
    echo ""
    
    if [ $MISSING -eq 0 ]; then
        echo -e "${GREEN}✓ All required dependencies are installed${NC}"
        if [ $OPTIONAL_MISSING -gt 0 ]; then
            echo -e "${YELLOW}⚠ Some optional dependencies are missing (not critical)${NC}"
        fi
        exit 0
    else
        echo -e "${RED}✗ Missing $MISSING required dependency/dependencies${NC}"
        echo ""
        echo "Install missing dependencies:"
        echo "  Ubuntu/Debian: sudo apt-get install -y kubectl curl jq"
        echo "  macOS: brew install kubectl curl jq"
        exit 1
    fi
}

main "$@"

