#!/usr/bin/env bash
# Install All Required Tools
# Installs Terraform, kubectl, and Helm

set -euo pipefail


SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/init.sh"

log() {
    log_success "[✓] $1"
}

error() {
    log_error "[✗] $1"
    exit 1
}

warn() {
    log_warn "[!] $1"
}

info() {
    log_info "[i] $1"
}

section() {
    echo
    log_info "=== $1 ==="
}

section "Installing All Required Tools"

# Install Terraform
section "Terraform"
if [ -f "$SCRIPT_DIR/install-terraform.sh" ]; then
    "$SCRIPT_DIR/install-terraform.sh"
else
    warn "Terraform installation script not found"
fi

# Install kubectl
section "kubectl"
if [ -f "$SCRIPT_DIR/install-kubectl.sh" ]; then
    "$SCRIPT_DIR/install-kubectl.sh"
else
    warn "kubectl installation script not found"
fi

# Install Helm
section "Helm"
if [ -f "$SCRIPT_DIR/install-helm.sh" ]; then
    "$SCRIPT_DIR/install-helm.sh"
else
    warn "Helm installation script not found"
fi

# Verify all installations
section "Verification"
TOOLS=("terraform" "kubectl" "helm")
ALL_INSTALLED=true

for tool in "${TOOLS[@]}"; do
    if command -v "$tool" &> /dev/null; then
        VERSION=$($tool version 2>/dev/null | head -n 1 || echo "installed")
        log "$tool: $VERSION"
    else
        error "$tool is not installed"
        ALL_INSTALLED=false
    fi
done

if [ "$ALL_INSTALLED" = true ]; then
    section "Installation Complete"
    log "All tools installed successfully"
    info "You can now proceed with deployment"
else
    error "Some tools failed to install"
fi

