#!/bin/bash
# check-dependencies.sh
# Author: Matthew Raymer
# Date: 2025-08-19
# Description: Dependency validation script for TimeSafari development environment
# This script checks for critical dependencies required for building the application.

# Exit on any error
set -e

# Source common utilities
source "$(dirname "$0")/common.sh"

print_header "TimeSafari Dependency Validation"

log_info "Checking development environment dependencies..."

# Check Node.js version
if command -v node &> /dev/null; then
    NODE_VERSION=$(node --version)
    log_info "Node.js version: $NODE_VERSION"
    
    # Extract major version number
    MAJOR_VERSION=$(echo $NODE_VERSION | sed 's/v\([0-9]*\)\..*/\1/')
    if [ "$MAJOR_VERSION" -lt 18 ]; then
        log_error "Node.js version $NODE_VERSION is too old. Please upgrade to Node.js 18 or later."
        exit 1
    fi
else
    log_error "Node.js is not installed. Please install Node.js 18 or later."
    exit 1
fi

# Check npm version
if command -v npm &> /dev/null; then
    NPM_VERSION=$(npm --version)
    log_info "npm version: $NPM_VERSION"
else
    log_error "npm is not installed. Please install npm."
    exit 1
fi

# Check if node_modules exists
if [ ! -d "node_modules" ]; then
    log_error "node_modules directory not found."
    log_info "Please run: npm install"
    exit 1
fi

# Check critical dependencies
log_info "Validating critical packages..."

CRITICAL_DEPS=("tsx" "capacitor-assets" "vite")

for dep in "${CRITICAL_DEPS[@]}"; do
    if [ -f "node_modules/.bin/$dep" ]; then
        log_success "✓ $dep found"
    else
        log_error "✗ $dep not found in node_modules/.bin"
        log_info "This usually means the package wasn't installed properly."
        log_info "Try running: npm install"
        exit 1
    fi
done

# Check TypeScript via npx
if npx tsc --version &> /dev/null; then
    TSC_VERSION=$(npx tsc --version)
    log_success "✓ TypeScript found: $TSC_VERSION"
else
    log_error "✗ TypeScript not accessible via npx"
    log_info "Try running: npm install"
    exit 1
fi

# Check Capacitor CLI
if command -v npx &> /dev/null; then
    if npx cap --version &> /dev/null; then
        CAP_VERSION=$(npx cap --version)
        log_success "✓ Capacitor CLI version: $CAP_VERSION"
    else
        log_error "✗ Capacitor CLI not accessible via npx"
        log_info "Try running: npm install @capacitor/cli"
        exit 1
    fi
else
    log_error "npx is not available. Please ensure npm is properly installed."
    exit 1
fi

# Check Android development tools
if command -v adb &> /dev/null; then
    log_success "✓ Android Debug Bridge (adb) found"
else
    log_warn "⚠ Android Debug Bridge (adb) not found"
    log_info "This is only needed for Android development and testing."
fi

if command -v gradle &> /dev/null; then
    GRADLE_VERSION=$(gradle --version | head -n 1)
    log_success "✓ Gradle found: $GRADLE_VERSION"
else
    log_warn "⚠ Gradle not found in PATH"
    log_info "This is only needed if building outside of Android Studio."
fi

log_success "Dependency validation completed successfully!"
log_info "Your development environment is ready for TimeSafari development."

print_footer "Dependency Validation"
