#!/bin/bash
# Tenuo Development Setup
#
# This script:
# 1. Generates a root keypair for the control plane
# 2. Creates .env file with the keys
# 3. Starts the Docker Compose stack
#
# Usage:
#   ./tenuo-core/demo/setup.sh        # Setup and start
#   ./tenuo-core/demo/setup.sh --build # Rebuild images first

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$(dirname "$(dirname "$SCRIPT_DIR")")")"

echo "=============================================================="
echo "  TENUO DEVELOPMENT SETUP"
echo "=============================================================="
echo ""

cd "$PROJECT_ROOT"

# Check for Docker
if ! command -v docker &> /dev/null; then
    echo "Error: Docker is not installed"
    exit 1
fi

if ! command -v docker compose &> /dev/null; then
    echo "Error: Docker Compose is not installed"
    exit 1
fi

# Generate keypair if .env doesn't exist
if [ ! -f .env ] || [ "$1" == "--regenerate" ]; then
    echo "[INFO] Generating root keypair..."
    
    # Build the CLI first if needed
    if [ ! -f tenuo-core/target/release/tenuo ]; then
        echo "  Building Tenuo CLI..."
        cd tenuo-core
        cargo build --release --bin tenuo --features "control-plane,data-plane" 2>/dev/null
        cd ..
    fi
    
    # Generate keypair
    KEYPAIR_JSON=$(./tenuo-core/target/release/tenuo keygen)
    SECRET_KEY=$(echo "$KEYPAIR_JSON" | grep -o '"secret_key": "[^"]*"' | cut -d'"' -f4)
    PUBLIC_KEY=$(echo "$KEYPAIR_JSON" | grep -o '"public_key": "[^"]*"' | cut -d'"' -f4)
    
    # Write .env file
    cat > .env << EOF
# Tenuo Development Configuration
# Generated by dev-setup.sh - DO NOT COMMIT TO VERSION CONTROL

# Control Plane secret key (keep this secret!)
TENUO_SECRET_KEY=${SECRET_KEY}

# Public key (share with data planes)
TENUO_PUBLIC_KEY=${PUBLIC_KEY}
EOF

    echo "[OK] Generated keypair"
    echo "  Public Key: ${PUBLIC_KEY}"
    echo "  Saved to .env"
    echo ""
else
    echo "[INFO] Using existing .env file"
    source .env
    echo "  Public Key: ${TENUO_PUBLIC_KEY}"
    echo ""
fi

# Build images if requested
if [ "$1" == "--build" ]; then
    echo "[INFO] Building Docker images..."
    docker compose build
    echo ""
fi

# Start the stack
echo "[INFO] Starting Tenuo stack..."
echo ""
docker compose up -d control-plane authorizer

echo ""
echo "==============================================================="
echo "Tenuo is running!"
echo "==============================================================="
echo ""
echo "Control Plane: http://localhost:8080"
echo "  - GET  /health           - Health check"
echo "  - GET  /v1/public-key    - Get public key"
echo "  - POST /v1/warrants      - Issue a warrant"
echo ""
echo "Authorizer:    http://localhost:9090 (when running serve mode)"
echo ""
echo "Quick test:"
echo "  curl http://localhost:8080/health"
echo "  curl http://localhost:8080/v1/public-key"
echo ""
echo "Issue a warrant:"
echo '  curl -X POST http://localhost:8080/v1/warrants \'
echo '    -H "Content-Type: application/json" \'
echo '    -d '"'"'{"tool":"test","constraints":{},"ttl_seconds":60}'"'"
echo ""
echo "Run the demo:"
echo "  docker compose up"
echo ""
echo "View logs:"
echo "  docker compose logs -f"
echo ""
echo "Stop:"
echo "  docker compose down"
echo ""

