#!/usr/bin/env nu

# VAPORA Development Environment Setup
# Phase 0: Workspace initialization script
# Follows NUSHELL_GUIDELINES.md - 17 rules

# Check if Rust toolchain is installed
def check-rust []: bool {
    (which rustc | length) > 0
}

# Check if cargo is available
def check-cargo []: bool {
    (which cargo | length) > 0
}

# Get Rust version information
def get-rust-version []: string {
    if (check-rust) {
        (rustc --version | str trim)
    } else {
        ""
    }
}

# Validate minimum Rust version (1.75+)
def validate-rust-version []: record {
    let version = (get-rust-version)

    if ($version == "") {
        {
            valid: false,
            error: "Rust not installed"
        }
    } else {
        {
            valid: true,
            error: null
        }
    }
}

# Check if .env file exists
def check-env-file []: bool {
    (".env" | path exists)
}

# Create .env file from template
def create-env-file []: void {
    let template = "# VAPORA Environment Variables
# Phase 0: Configuration template

# Server
VAPORA_HOST=127.0.0.1
VAPORA_PORT=3000

# Database (required)
VAPORA_DB_URL=surreal://localhost:8000/vapora

# NATS JetStream
VAPORA_NATS_URL=nats://localhost:4222

# Authentication (set in production)
VAPORA_JWT_SECRET=change-me-in-production

# LLM Providers (optional)
ANTHROPIC_API_KEY=
OPENAI_API_KEY=
GOOGLE_API_KEY=
OLLAMA_URL=http://localhost:11434

# Logging
VAPORA_LOG_LEVEL=info
VAPORA_LOG_JSON=false
"

    $template | save -f ".env"
}

# Main setup function
def main []: void {
    print "=== VAPORA Development Setup ==="
    print ""

    # Step 1: Check Rust
    print "Checking Rust installation..."
    let rust_check = (validate-rust-version)

    if (not $rust_check.valid) {
        print $"ERROR: ($rust_check.error)"
        print "Please install Rust from https://rustup.rs/"
        exit 1
    }

    print $"✓ Rust installed: (get-rust-version)"

    # Step 2: Check cargo
    if (not (check-cargo)) {
        print "ERROR: cargo not found"
        exit 1
    }
    print "✓ Cargo available"

    # Step 3: Create .env if missing
    print ""
    print "Checking environment configuration..."

    if (not (check-env-file)) {
        print "Creating .env file from template..."
        create-env-file
        print "✓ .env file created"
        print "⚠ Please edit .env and set required variables"
    } else {
        print "✓ .env file exists"
    }

    # Step 4: Verify workspace structure
    print ""
    print "Verifying workspace structure..."

    let required_dirs = [
        "crates",
        "config",
        "scripts"
    ]

    for dir in $required_dirs {
        if ($dir | path exists) {
            print $"✓ [$dir]/ directory exists"
        } else {
            print $"✗ [$dir]/ directory missing"
            exit 1
        }
    }

    # Step 5: Summary
    print ""
    print "=== Setup Complete ==="
    print ""
    print "Next steps:"
    print "  1. Edit .env file with your configuration"
    print "  2. Run: nu scripts/build.nu"
    print "  3. Run: nu scripts/test.nu"
    print ""
    print "VAPORA v0.2.0 - Phase 0 workspace ready"
}
