#!/bin/bash
# Production agent deployment script

# Default configuration
DEFAULT_AGENT_PHP_PATH="$(pwd)/agents/web_agent.php"
DEFAULT_CONTAINER_NAME="agent.local"
DEFAULT_NETWORK_NAME="culiuptime_prod"

# Function to show usage
usage() {
    echo "Usage: $0 -i AGENT_ID -s AGENT_SECRET_KEY -k PLATFORM_API_KEY [OPTIONS]"
    echo ""
    echo "Required parameters:"
    echo "  -i, --agent-id          Agent ID"
    echo "  -s, --secret-key        Agent Secret Key"
    echo "  -k, --api-key          Platform API Key"
    echo ""
    echo "Optional parameters:"
    echo "  -p, --php-path         Path to web_agent.php (default: $DEFAULT_AGENT_PHP_PATH)"
    echo "  -c, --container-name   Container name (default: $DEFAULT_CONTAINER_NAME)"
    echo "  -n, --network-name     Network name (default: $DEFAULT_NETWORK_NAME)"
    echo "  -h, --help             Show this help message"
    echo ""
    echo "Example:"
    echo "  $0 -i agent-123 -s secret-key-456 -k api-key-789"
    echo "  $0 -i agent-123 -s secret-key-456 -k api-key-789 -p /custom/path/web_agent.php -P 9090"
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -i|--agent-id)
            AGENT_ID="$2"
            shift 2
            ;;
        -s|--secret-key)
            AGENT_SECRET_KEY="$2"
            shift 2
            ;;
        -k|--api-key)
            PLATFORM_API_KEY="$2"
            shift 2
            ;;
        -p|--php-path)
            AGENT_PHP_PATH="$2"
            shift 2
            ;;
        -c|--container-name)
            CONTAINER_NAME="$2"
            shift 2
            ;;
        -n|--network-name)
            NETWORK_NAME="$2"
            shift 2
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            usage
            exit 1
            ;;
    esac
done

# Set defaults for optional parameters
AGENT_PHP_PATH="${AGENT_PHP_PATH:-$DEFAULT_AGENT_PHP_PATH}"
CONTAINER_NAME="${CONTAINER_NAME:-$DEFAULT_CONTAINER_NAME}"
NETWORK_NAME="${NETWORK_NAME:-$DEFAULT_NETWORK_NAME}"

# Validate required parameters
if [[ -z "$AGENT_ID" || -z "$AGENT_SECRET_KEY" || -z "$PLATFORM_API_KEY" ]]; then
    echo "Error: Missing required parameters"
    echo ""
    usage
    exit 1
fi

# Validate PHP file exists
if [[ ! -f "$AGENT_PHP_PATH" ]]; then
    echo "Error: PHP agent file not found at: $AGENT_PHP_PATH"
    exit 1
fi

echo "Deploying agent with configuration:"
echo "  Agent ID: $AGENT_ID"
echo "  Container Name: $CONTAINER_NAME"
echo "  Network Name: $NETWORK_NAME"
echo "  PHP Path: $AGENT_PHP_PATH"
echo ""


# Stop and remove existing container
docker stop $CONTAINER_NAME 2>/dev/null || true
docker rm $CONTAINER_NAME 2>/dev/null || true

# Run the agent
# Health check expects 401 (Unauthorized) - means agent is running and HMAC auth is working
docker run -d \
  --name $CONTAINER_NAME \
  --network $NETWORK_NAME \
  --restart unless-stopped \
  -e AGENT_ID="$AGENT_ID" \
  -e AGENT_SECRET_KEY="$AGENT_SECRET_KEY" \
  -e PLATFORM_API_KEY="$PLATFORM_API_KEY" \
  -e PHP_INI_SCAN_DIR="/usr/local/etc/php/conf.d" \
  -v $AGENT_PHP_PATH:/var/www/html/culiuptime-agent.php:ro \
  --health-cmd="curl -s -o /dev/null -w '%{http_code}' http://localhost/culiuptime-agent.php | grep -q '^401$'" \
  --health-interval=30s \
  --health-timeout=10s \
  --health-retries=3 \
  php:8.1-apache

echo ""
echo "Agent deployed successfully!"
echo "Access URL: http://localhost:$PORT/culiuptime-agent.php"
echo "Container logs: docker logs $CONTAINER_NAME"
