# 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 ""
    echo "📝 Unified logs below (API server + Demo site):"
    echo "🔌 [SERVER] = API server logs"
    echo "🌐 [DEMO] = Demo site logs"
    echo ""
    
    # Function to cleanup background processes
    cleanup() {
        echo ""
        echo "🛑 Shutting down servers..."
        kill $SERVER_PID $DEMO_PID 2>/dev/null || true
        wait $SERVER_PID $DEMO_PID 2>/dev/null || true
        echo "✅ Shutdown complete"
        exit 0
    }
    trap cleanup SIGINT SIGTERM
    
    # Start API server with prefixed output
    echo "🔌 Starting API server (localhost:8080)..."
    INSERTR_DATABASE_PATH=./dev.db ./insertr serve --dev-mode 2>&1 | sed 's/^/🔌 [SERVER] /' &
    SERVER_PID=$!
    
    # Wait for server startup
    echo "⏳ Waiting for API server startup..."
    sleep 3
    
    # Check server health
    if curl -s http://localhost:8080/health > /dev/null 2>&1; then
        echo "✅ API server ready!"
    else
        echo "⚠️  API server may not be ready yet"
    fi
    
    echo ""
    echo "🌐 Starting demo site (localhost:3000)..."
    echo "📝 Full-stack ready - edit content with real-time persistence!"
    echo ""
    
    # Start demo site with prefixed output (this will block) - use local installation
    cd {{justfile_directory()}} && npx --prefer-offline live-server demo-site --port=3000 --host=localhost --open=/index.html 2>&1 | sed 's/^/🌐 [DEMO] /' &
    DEMO_PID=$!
    
    # Wait for both processes
    wait $DEMO_PID $SERVER_PID

# Demo site only (for specific use cases)


# Start development server for about page
dev-about: build-lib build
    #!/usr/bin/env bash
    echo "🚀 Starting full-stack development (about page)..."
    INSERTR_DATABASE_PATH=./dev.db ./insertr serve --dev-mode &
    SERVER_PID=$!
    sleep 3
    npx --prefer-offline live-server demo-site --port=3000 --host=localhost --open=/about.html
    kill $SERVER_PID 2>/dev/null || true

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

# Start demo for a specific test site
demo site="":
    #!/usr/bin/env bash
    if [ "{{site}}" = "" ]; then
        echo "📋 Available Insertr Demo Sites:"
        echo "=================================="
        echo ""
        echo "🏠 Built-in Demo:"
        echo "   default              - Default insertr demo site"
        echo ""
        echo "🌐 Test Site Demos:"
        echo "   dan-eden             - Dan Eden's portfolio"
        echo "   simple               - Simple test site"
        echo ""
        echo "📝 Usage:"
        echo "   just demo default              - Start default demo"
        echo "   just demo dan-eden             - Start Dan Eden portfolio demo"
        echo "   just demo simple               - Start simple test site demo"
        echo ""
        echo "💡 Note: Sites are auto-enhanced on first run"
    elif [ "{{site}}" = "default" ] || [ "{{site}}" = "demo" ]; then
        echo "🚀 Starting default demo site..."
        just dev
    elif [ "{{site}}" = "dan-eden" ]; then
        if [ ! -d "./test-sites/simple/dan-eden-portfolio-demo" ]; then
            echo "🔧 Dan Eden demo not ready - auto-enhancing now..."
            just build
            ./insertr auto-enhance test-sites/simple/dan-eden-portfolio --output test-sites/simple/dan-eden-portfolio-temp --config test-sites/simple/dan-eden-portfolio/insertr.yaml
            ./insertr enhance test-sites/simple/dan-eden-portfolio-temp --output test-sites/simple/dan-eden-portfolio-demo --config test-sites/simple/dan-eden-portfolio/insertr.yaml
            rm -rf test-sites/simple/dan-eden-portfolio-temp
        fi
        echo "🚀 Starting Dan Eden portfolio demo..."
        just demo-site "dan-eden" "./test-sites/simple/dan-eden-portfolio-demo" "3000"
    elif [ "{{site}}" = "simple" ]; then
        if [ ! -d "./test-sites/simple/test-simple-demo" ]; then
            echo "🔧 Simple demo not ready - auto-enhancing now..."
            just build
            ./insertr auto-enhance test-sites/simple/test-simple --output test-sites/simple/test-simple-temp --config test-sites/simple/test-simple/insertr.yaml
            ./insertr enhance test-sites/simple/test-simple-temp --output test-sites/simple/test-simple-demo --config test-sites/simple/test-simple/insertr.yaml
            rm -rf test-sites/simple/test-simple-temp
        fi
        echo "🚀 Starting simple test site demo..."
        just demo-site "simple" "./test-sites/simple/test-simple-demo" "3000"
    else
        echo "❌ Unknown demo site: {{site}}"
        echo ""
        echo "📋 Available demo sites:"
        echo "   default              - Default demo site" 
        echo "   dan-eden             - Dan Eden portfolio"
        echo "   simple               - Simple test site"
        echo ""
        echo "🔧 Other commands:"
        echo "   just demo                - Show all demo sites"
        exit 1
    fi

# Generic demo site launcher (internal command)
demo-site site_id path port="3000": build
    #!/usr/bin/env bash
    echo "🚀 Starting {{site_id}} demo..."
    echo "📁 Path: {{path}}"
    echo "🌐 Port: {{port}}"
    echo "================================================"
    echo ""
    
    # Function to cleanup background processes
    cleanup() {
        echo ""
        echo "🛑 Shutting down servers..."
        kill $SERVER_PID $DEMO_PID 2>/dev/null || true
        wait $SERVER_PID $DEMO_PID 2>/dev/null || true
        echo "✅ Shutdown complete"
        exit 0
    }
    trap cleanup SIGINT SIGTERM
    
    # Start API server
    echo "🔌 Starting API server (localhost:8080)..."
    INSERTR_DATABASE_PATH=./dev.db ./insertr serve --dev-mode 2>&1 | sed 's/^/🔌 [{{site_id}}] /' &
    SERVER_PID=$!
    
    # Wait for server startup
    echo "⏳ Waiting for API server startup..."
    sleep 3
    
    # Check server health
    if curl -s http://localhost:8080/health > /dev/null 2>&1; then
        echo "✅ API server ready!"
    else
        echo "⚠️  API server may not be ready yet"
    fi
    
    echo ""
    echo "🌐 Starting {{site_id}} (localhost:{{port}})..."
    echo "📝 Demo ready - test insertr functionality!"
    echo ""
    
    # Start demo site
    npx --prefer-offline live-server "{{path}}" --port={{port}} --host=localhost --open=/index.html 2>&1 | sed 's/^/🌐 [{{site_id}}] /' &
    DEMO_PID=$!
    
    # Wait for both processes
    wait $DEMO_PID $SERVER_PID

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

# Build only the JavaScript library
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="demo-site" output="dist":
    ./insertr enhance {{input}} --output {{output}} --mock

# === Content API Server Commands ===

# Start content API server (default port 8080)
serve port="8080":
    INSERTR_DATABASE_PATH=./dev.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 and backups
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 and backups"



# 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 site:"
    @ls -la demo-site/index.html demo-site/about.html 2>/dev/null || echo "   Missing demo files"
    @echo ""
    @echo "🚀 Development Commands:"
    @echo "   just dev             - Full-stack development (recommended)"
    @echo "   just demo [site]     - Start specific demo site (or show available demos)"

    @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 "./test-sites/simple/dan-eden-portfolio-demo" ]; then
        rm -rf "./test-sites/simple/dan-eden-portfolio-demo"
        echo "🗑️  Removed: dan-eden-portfolio-demo"
    fi
    
    if [ -d "./test-sites/simple/test-simple-demo" ]; then
        rm -rf "./test-sites/simple/test-simple-demo"
        echo "🗑️  Removed: test-simple-demo"
    fi
    
    # Clean up any temporary directories
    if [ -d "./test-sites/simple/dan-eden-portfolio-temp" ]; then
        rm -rf "./test-sites/simple/dan-eden-portfolio-temp"
        echo "🗑️  Removed: dan-eden-portfolio-temp"
    fi
    
    if [ -d "./test-sites/simple/test-simple-temp" ]; then
        rm -rf "./test-sites/simple/test-simple-temp"
        echo "🗑️  Removed: test-simple-temp"
    fi
    
    # Legacy directories (cleanup from old workflow)
    for legacy_dir in dan-eden-portfolio-auto-enhanced dan-eden-portfolio-full dan-eden-portfolio-auto dan-eden-portfolio-auto-v2 dan-eden-portfolio-auto-enhanced test-simple-auto-enhanced test-simple-full dan-eden-portfolio-enhanced test-simple-enhanced; do
        if [ -d "./test-sites/simple/${legacy_dir}" ]; then
            rm -rf "./test-sites/simple/${legacy_dir}"
            echo "🗑️  Removed: ${legacy_dir} (legacy)"
        fi
    done
    
    if [ -d "./test-sites/simple/dan-eden-portfolio-full" ]; then
        rm -rf "./test-sites/simple/dan-eden-portfolio-full"
        echo "🗑️  Removed: dan-eden-portfolio-full"
    fi
    
    if [ -d "./test-sites/simple/test-simple-auto-enhanced" ]; then
        rm -rf "./test-sites/simple/test-simple-auto-enhanced"
        echo "🗑️  Removed: test-simple-auto-enhanced"
    fi
    
    if [ -d "./test-sites/simple/test-simple-full" ]; then
        rm -rf "./test-sites/simple/test-simple-full"
        echo "🗑️  Removed: test-simple-full"
    fi
    

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


