#!/bin/bash
set -e

echo "🚀 Control Center UI - Setup Script"
echo "=================================="

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to print colored output
print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if we're in the right directory
if [ ! -f "Cargo.toml" ]; then
    print_error "Please run this script from the control-center-ui directory"
    exit 1
fi

print_status "Checking prerequisites..."

# Check for Rust
if ! command -v rustc &> /dev/null; then
    print_error "Rust is not installed. Please install Rust first:"
    echo "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
    exit 1
fi

# Check for Node.js
if ! command -v node &> /dev/null; then
    print_error "Node.js is not installed. Please install Node.js first."
    exit 1
fi

# Check for trunk
if ! command -v trunk &> /dev/null; then
    print_status "Installing Trunk..."
    cargo install trunk
fi

# Add wasm32 target
print_status "Adding wasm32-unknown-unknown target..."
rustup target add wasm32-unknown-unknown

# Install Node.js dependencies
print_status "Installing Node.js dependencies..."
pnpm install

# Build TailwindCSS
print_status "Building TailwindCSS..."
pnpm run css:build

# Check Rust dependencies
print_status "Checking Rust dependencies..."
cargo check

print_success "Setup completed successfully!"
echo ""
echo "🎉 Ready to develop!"
echo ""
echo "Available commands:"
echo "  npm run dev         - Start development server"
echo "  npm run dev:open    - Start development server and open browser"
echo "  npm run build       - Build for production"
echo "  npm run css:watch   - Watch TailwindCSS changes"
echo "  npm run clean       - Clean build artifacts"
echo ""
echo "Development server will be available at: http://localhost:3000"
echo "API proxy configured for: http://localhost:8080"
echo ""
print_status "Happy coding! 🦀"
