#!/bin/bash
# Development environment setup script
# Installs dependencies and sets up development environment

set -e

echo "🚀 Setting up Archipelago development environment..."

# Check prerequisites
echo "📋 Checking prerequisites..."

command -v rustc >/dev/null 2>&1 || { echo "❌ Rust is required. Install from https://rustup.rs/"; exit 1; }
command -v node >/dev/null 2>&1 || { echo "❌ Node.js is required. Install from https://nodejs.org/"; exit 1; }
command -v cargo >/dev/null 2>&1 || { echo "❌ Cargo is required. Install Rust toolchain."; exit 1; }

echo "✅ Prerequisites check passed"

# Get project root (assumes script is in scripts/)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

echo "📁 Project root: $PROJECT_ROOT"

# Check if we're in the right directory structure
if [ ! -d "$PROJECT_ROOT/core" ] && [ ! -d "$PROJECT_ROOT/neode-ui" ]; then
    echo "⚠️  Warning: This script expects to be run from the Archipelago workspace."
    echo "   If you're working with Code/Archipelago, you may need to adjust paths."
    read -p "Continue anyway? (y/n) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

# Setup frontend
if [ -d "$PROJECT_ROOT/neode-ui" ]; then
    echo "📦 Setting up frontend..."
    cd "$PROJECT_ROOT/neode-ui"
    if [ ! -d "node_modules" ]; then
        npm install
    else
        echo "   node_modules already exists, skipping install"
    fi
else
    echo "⚠️  neode-ui directory not found, skipping frontend setup"
fi

# Setup backend
if [ -d "$PROJECT_ROOT/core" ]; then
    echo "🔧 Setting up backend..."
    cd "$PROJECT_ROOT/core"
    
    # Check if Cargo.toml exists
    if [ -f "Cargo.toml" ]; then
        echo "   Fetching Rust dependencies..."
        cargo fetch
    else
        echo "⚠️  Cargo.toml not found in core/, skipping backend setup"
    fi
else
    echo "⚠️  core directory not found, skipping backend setup"
fi

# Check for Podman (optional)
if command -v podman >/dev/null 2>&1; then
    echo "✅ Podman found: $(podman --version)"
    if [[ "$OSTYPE" == "darwin"* ]]; then
        if ! podman machine list | grep -q "running"; then
            echo "⚠️  Podman machine not running. Start with: podman machine start"
        fi
    fi
else
    echo "⚠️  Podman not found (optional, needed for container features)"
    echo "   Install: https://podman.io/getting-started/installation"
fi

# Check for PostgreSQL (optional)
if command -v psql >/dev/null 2>&1; then
    echo "✅ PostgreSQL found: $(psql --version)"
else
    echo "⚠️  PostgreSQL not found (optional, needed for backend database)"
    echo "   Install: https://www.postgresql.org/download/"
    echo "   Or use Docker: docker run -d --name postgres -e POSTGRES_PASSWORD=dev -p 5432:5432 postgres:15"
fi

echo ""
echo "✅ Development environment setup complete!"
echo ""
echo "Next steps:"
echo "  1. Start backend: cd core && cargo run --bin startbox"
echo "  2. Start frontend: cd neode-ui && npm run dev"
echo ""
echo "Or use the mock backend for UI-only development:"
echo "  cd neode-ui && npm run dev:mock"
