# Insertr Development Commands
# Use `just <command>` to run any of these tasks

# Default recipe - show available commands
default:
    @just --list

# Install all dependencies (root + lib)
install:
    npm install
    cd lib && npm install

# Start full-stack development (primary workflow)
dev: build-lib build
    #!/usr/bin/env bash
    echo "🚀 Starting Full-Stack Insertr Development..."
    echo "================================================"
    echo ""
    
    # Note: Sites are auto-enhanced by the server on startup
    
    echo ""
    echo "🔌 Starting Insertr server with all sites..."
    echo ""
    
    # Function to cleanup background processes
    cleanup() {
        echo ""
        echo "🛑 Shutting down server..."
        kill $SERVER_PID 2>/dev/null || true
        wait $SERVER_PID 2>/dev/null || true
        echo "✅ Shutdown complete"
        exit 0
    }
    trap cleanup SIGINT SIGTERM
    
    # Start server (serves API + all static sites)
    INSERTR_DATABASE_PATH=./insertr.db ./insertr serve --dev-mode &
    SERVER_PID=$!
    
    echo ""
    echo "🌐 All sites available at:"
    echo "   Default:       http://localhost:8080/sites/default/"
    echo "   Simple:        http://localhost:8080/sites/simple/"  
    echo "   Dan Eden:      http://localhost:8080/sites/dan-eden-portfolio/"
    echo "   Devigo (NO):   http://localhost:8080/sites/devigo-web/"
    echo ""
    echo "📝 Full-stack ready - edit content with real-time persistence!"
    echo "🔄 Press Ctrl+C to shutdown"
    echo ""
    
    # Wait for server process
    wait $SERVER_PID

# Start development server for about page  
dev-about: build-lib build
    #!/usr/bin/env bash
    echo "🚀 Starting full-stack development..."
    echo "🌐 About page available at: http://localhost:8080/sites/default/about.html"
    INSERTR_DATABASE_PATH=./insertr.db ./insertr serve --dev-mode

# Check project status and validate setup
check:
    npm run check



# Build the entire project (library + unified binary)
build:
    npm run build

# Build only the JavaScript library (includes CSS)
build-lib:
    npm run build:lib

# Watch library files for changes (auto-rebuild)
watch:
    cd lib && npm run dev

# Start Air hot-reload for unified binary development
air:
    air

# Build unified binary only
build-insertr:
    go build -o insertr .

# Run insertr help
help:
    ./insertr --help



# Enhance demo site (build-time content injection)
enhance input="demos/default" output="dist":
    ./insertr enhance {{input}} --output {{output}}



# === Content API Server Commands ===

# Start content API server (default port 8080)
serve port="8080":
    INSERTR_DATABASE_PATH=./insertr.db ./insertr serve --port {{port}} --dev-mode

# Start API server in production mode
serve-prod port="8080" db="./insertr.db":
    INSERTR_DATABASE_PATH={{db}} ./insertr serve --port {{port}}



# Check API server health
health port="8080":
    @echo "🔍 Checking API server health..."
    @curl -s http://localhost:{{port}}/health | jq . || echo "❌ Server not responding at localhost:{{port}}"

# Clean all build artifacts
clean:
    rm -rf lib/dist
    rm -rf insertr
    rm -rf tmp
    rm -rf dist
    rm -rf node_modules
    rm -rf lib/node_modules
    rm -f dev.db
    rm -f insertr.db
    @echo "🧹 Cleaned all build artifacts"



# Lint code (placeholder for now)
lint:
    npm run lint

# Run tests (placeholder for now)  
test:
    npm run test

# Development workflow: install deps, build lib, start dev server
dev-setup: install build-lib dev

# Production workflow: install deps, build everything
prod-build: install build

# Show project status
status:
    @echo "🏗️  Insertr Project Status"
    @echo "========================="
    @echo "📁 Root files:"
    @ls -la package.json justfile go.mod insertr.yaml 2>/dev/null || echo "   Missing files"
    @echo "\n📚 Library files:"
    @ls -la lib/package.json lib/src lib/dist 2>/dev/null || echo "   Missing library components"
    @echo "\n🔧 Unified binary:"
    @ls -la insertr main.go cmd/ internal/ 2>/dev/null || echo "   Missing unified binary components"
    @echo "\n🌐 Demo sites:"
    @ls -la demos/default/index.html demos/simple/index.html demos/dan-eden-portfolio/index.html demos/devigo-web/index.html 2>/dev/null || echo "   Some demo files missing"
    @echo ""
    @echo "🚀 Development Commands:"
    @echo "   just dev             - Full-stack development (recommended)"
    @echo "   just serve           - API server only (localhost:8080)"
    @echo "   just enhance         - Build-time content injection"
    @echo ""
    @echo "🔍 Check server: just health"


# Generate sqlc code (for database schema changes)
sqlc:
    sqlc generate







# Clean generated demo directories
clean-demos:
    #!/usr/bin/env bash
    echo "🧹 Cleaning generated demo directories..."
    echo "========================================="
    
    # Demo directories
    if [ -d "./demos/default_enhanced" ]; then
        rm -rf "./demos/default_enhanced"
        echo "🗑️  Removed: default_enhanced"
    fi
    
    if [ -d "./demos/simple_enhanced" ]; then
        rm -rf "./demos/simple_enhanced"
        echo "🗑️  Removed: simple_enhanced"
    fi
    
    if [ -d "./demos/dan-eden-portfolio_enhanced" ]; then
        rm -rf "./demos/dan-eden-portfolio_enhanced"
        echo "🗑️  Removed: dan-eden-portfolio_enhanced"
    fi
    
    if [ -d "./demos/devigo-web_enhanced" ]; then
        rm -rf "./demos/devigo-web_enhanced"
        echo "🗑️  Removed: devigo-web_enhanced"
    fi
    
    # Clean up any temporary directories
    if [ -d "./demos/dan-eden-portfolio-temp" ]; then
        rm -rf "./demos/dan-eden-portfolio-temp"
        echo "🗑️  Removed: dan-eden-portfolio-temp"
    fi
    
    if [ -d "./demos/simple-temp" ]; then
        rm -rf "./demos/simple-temp"
        echo "🗑️  Removed: simple-temp"
    fi
    
    # Legacy directories (cleanup from old workflow)
    for legacy_dir in demo-site demo-site_enhanced demo_enhanced simple/test-simple simple/dan-eden-portfolio; do
        if [ -d "./demos/${legacy_dir}" ]; then
            rm -rf "./demos/${legacy_dir}"
            echo "🗑️  Removed: ${legacy_dir} (legacy)"
        fi
    done
    
    # Clean up legacy directories in simple subdirectory
    if [ -d "./demos/simple" ] && [ -z "$(find ./demos/simple -maxdepth 1 -name '*.html' -o -name '*.yaml')" ]; then
        # If simple directory exists but contains no HTML/YAML files, it's legacy
        rm -rf "./demos/simple"/* 2>/dev/null || true
    fi
    

    
    echo ""
    echo "✅ Demo cleanup complete!"
    echo "🔧 Sites will auto-enhance when you run demo commands"


