#!/bin/bash
set -e

echo "🚀 Starting Cortex Agent Provisioning for node: {{ node_id }}"

# Determine OS and Arch
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"

if [ "$ARCH" = "x86_64" ]; then
    ARCH="amd64"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
    ARCH="arm64"
fi

if [ "$OS" = "darwin" ]; then
    # Currently fallback to python method or generic binary if available
    # For now assume arm64 if darwin and M1
    if [ "$ARCH" = "amd64" ]; then
        BIN_ARCH="darwin_amd64"
    else
        BIN_ARCH="darwin_arm64"
    fi
else
    BIN_ARCH="${OS}_${ARCH}"
fi

# 1. Create .cortex/agent-node directory
INSTALL_DIR="$HOME/.cortex/agent-node"
mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"

# 2. Write agent_config.yaml
echo "[*] Writing configuration..."
mkdir -p "$HOME/.cortex"
cat > "$HOME/.cortex/agent.yaml" << 'EOF'
{{ config_yaml }}
EOF

# 3. Download standalone binary or Source fallback
echo "[*] Downloading agent core..."
INSTALLER_URL="{{ base_url }}/api/v1/agent/binary/$BIN_ARCH"

USE_SOURCE=true
if curl -sSLf "$INSTALLER_URL" -o cortex-agent; then
    chmod +x cortex-agent
    mkdir -p "$INSTALL_DIR/tmp"
    export TMPDIR="$INSTALL_DIR/tmp"
    echo "[*] Verifying binary compatibility on this target OS..."
    if ./cortex-agent --version >/dev/null 2>&1; then
        echo "✅ Downloaded and verified standalone binary for $BIN_ARCH"
        BINARY_PATH="$INSTALL_DIR/cortex-agent"
        USE_SOURCE=false
    else
        echo "⚠️ Binary failed compatibility check (e.g. GLIBC version mismatch)."
        rm -f cortex-agent
    fi
else
    echo "⚠️ Standalone binary for $BIN_ARCH was not found (404) on Hub."
fi

if [ "$USE_SOURCE" = true ]; then
    echo "[*] Falling back to Python Source + Virtualenv execution..."
    
    # Use system /tmp for bootstrapping on NAS to avoid memory mapping issues on Volume shares
    export TMPDIR="/tmp"
    mkdir -p "$TMPDIR"

    # 1. Download bootstrap_installer.py
    echo "[*] Downloading installer from {{ base_url }}/api/v1/agent/installer ..."
    if ! curl -sSLf "{{ base_url }}/api/v1/agent/installer" -o bootstrap_installer.py; then
        echo "❌ Failed to download installer."
        exit 1
    fi

    # 2. Run the installer directly
    echo "[*] Bootstrapping agent via python3..."
    # Force sync before execution to avoid Bus Error on recently written file handles
    sync && sleep 1
    if ! python3 -B bootstrap_installer.py \
        --hub "{{ base_url }}" \
        --token "{{ invite_token }}" \
        --node-id "{{ node_id }}" \
        --daemon; then
        echo "❌ Provisioning failed via python bootstrap."
        exit 1
    fi
    
    echo "✅ Python-based bootstrap provisioning successful!"
    exit 0
fi

# 4. Bootstrap daemon installation
BINARY_PATH="$INSTALL_DIR/cortex-agent"
echo "[*] Bootstrapping agent daemon..."
# Run the standalone binary with the install-daemon flags (must implement in main.py)
# Or we can just run it in the background if systemd is unavailable
systemctl --user list-units >/dev/null 2>&1 && HAS_SYSTEMD=true || HAS_SYSTEMD=false

if [ "$OS" = "darwin" ]; then
    echo "Installing macOS launchd service..."
    AGENTS_DIR="$HOME/Library/LaunchAgents"
    mkdir -p "$AGENTS_DIR"
    PLIST_PATH="$AGENTS_DIR/com.jerxie.cortex.agent.plist"
    cat > "$PLIST_PATH" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.jerxie.cortex.agent</string>
    <key>ProgramArguments</key>
    <array>
        <string>$INSTALL_DIR/cortex-agent</string>
    </array>
    <key>WorkingDirectory</key>
    <string>$INSTALL_DIR</string>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>$HOME/.cortex/agent.err.log</string>
    <key>StandardOutPath</key>
    <string>$HOME/.cortex/agent.out.log</string>
</dict>
</plist>
EOF
    launchctl unload "$PLIST_PATH" 2>/dev/null || true
    launchctl load "$PLIST_PATH"
    echo "✅ macOS launchd daemon successfully started!"
elif [ "$HAS_SYSTEMD" = true ]; then
    echo "Installing Linux systemd user service..."
    mkdir -p ~/.config/systemd/user
    mkdir -p "$INSTALL_DIR/tmp"
    cat > ~/.config/systemd/user/cortex-agent.service << EOF
[Unit]
Description=Cortex Agent Node
After=network.target

[Service]
Type=simple
Environment=TMPDIR=$INSTALL_DIR/tmp
ExecStart=$INSTALL_DIR/cortex-agent
WorkingDirectory=$INSTALL_DIR
Restart=always
RestartSec=5
StandardOutput=append:$HOME/.cortex/agent.out.log
StandardError=append:$HOME/.cortex/agent.err.log

[Install]
WantedBy=default.target
EOF
    systemctl --user daemon-reload
    systemctl --user enable cortex-agent
    systemctl --user restart cortex-agent
    loginctl enable-linger "$USER" 2>/dev/null || true
    echo "✅ Systemd Daemon successfully started!"
else
    echo "Systemd not available. Falling back to background loop (nohup)..."
    mkdir -p "$INSTALL_DIR/tmp"
    export TMPDIR="$INSTALL_DIR/tmp"
    nohup ./cortex-agent >> "$HOME/.cortex/agent.out.log" 2>&1 &
    echo $! > agent.pid
    echo "✅ Background Agent started (PID $!)"
fi

echo "✅ Provisioning complete! Node should be online in the Mesh Dashboard shortly."
