#!/bin/bash
set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
USER_NAME="$(whoami)"
VM_DIR="/opt/victoriametrics"

echo "🔧 Setting up Weather Station..."

# ── VictoriaMetrics ───────────────────────────────────────────────────────────

if ! systemctl is-enabled --quiet victoriametrics 2>/dev/null; then
    echo "📦 Installing VictoriaMetrics..."
    sudo mkdir -p $VM_DIR/data
    cd $VM_DIR
    wget -qO vm.tar.gz "https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.146.0/victoria-metrics-linux-arm64-v1.146.0.tar.gz"
    sudo tar -xzf vm.tar.gz
    sudo chmod +x $VM_DIR/victoria-metrics-prod
    rm /tmp/vm.tar.gz

    sudo tee /etc/systemd/system/victoriametrics.service > /dev/null <<EOF
[Unit]
Description=VictoriaMetrics
After=network.target

[Service]
ExecStart=$VM_DIR/victoria-metrics-prod \
    -storageDataPath=$VM_DIR/data \
    -httpListenAddr=:8428 \
    -retentionPeriod=12
Restart=always
RestartSec=5
User=root

[Install]
WantedBy=multi-user.target
EOF

    sudo systemctl daemon-reload
    sudo systemctl enable --now victoriametrics
else
    echo "✅ VictoriaMetrics already installed, skipping..."
fi

# ── Server / Node.js ──────────────────────────────────────────────────────────

if ! command -v node &> /dev/null; then
    echo "📦 Installing Node.js..."
    curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
    sudo apt install -y nodejs
else
    echo "✅ Node.js already installed, skipping..."
fi

if [ ! -d "$SCRIPT_DIR/server/public/index.html" ]; then
    echo "🌐 Building Client..."
    cd "$SCRIPT_DIR/client"
    npm ci
    npx vite build
else
    echo "✅ Client installed, skipping..."
fi

if [ ! -d "$SCRIPT_DIR/server/node_modules" ]; then
    echo "🌐 Installing Server..."
    cd "$SCRIPT_DIR/server"
    npm ci
else
    echo "✅ Server installed, skipping..."
fi

if ! systemctl is-enabled --quiet weather-station 2>/dev/null; then
    echo "⚙️ Installing server service..."
    sudo tee /etc/systemd/system/weather-station.service > /dev/null <<EOF
[Unit]
Description=Weather Station API
After=network.target victoriametrics.service

[Service]
WorkingDirectory=$SCRIPT_DIR/server
ExecStart=npm run start
Restart=always
RestartSec=5
User=$USER_NAME

[Install]
WantedBy=multi-user.target
EOF
    sudo systemctl daemon-reload
    sudo systemctl enable --now weather-sensors
else
    echo "✅ Server service already installed, skipping..."
fi

# ── Sensors / Python venv ─────────────────────────────────────────────────────

if [ ! -d "$SCRIPT_DIR/sensors/venv" ]; then
    echo "🐍 Setting up Python environment..."
    cd "$SCRIPT_DIR/sensors"
    python3 -m venv venv
    venv/bin/pip install --upgrade pip
    venv/bin/pip install -r requirements.txt
else
    echo "✅ Python environment already set up, skipping..."
fi

if ! systemctl is-enabled --quiet weather-sensors 2>/dev/null; then
    echo "⚙️ Installing sensor service..."
    sudo tee /etc/systemd/system/weather-sensors.service > /dev/null <<EOF
[Unit]
Description=Weather Station Sensors
After=network.target victoriametrics.service

[Service]
WorkingDirectory=$SCRIPT_DIR/sensors
ExecStart=$SCRIPT_DIR/sensors/venv/bin/python main.py
Restart=always
RestartSec=5
User=$USER_NAME

[Install]
WantedBy=multi-user.target
EOF
    sudo systemctl daemon-reload
    sudo systemctl enable --now weather-sensors
else
    echo "✅ Sensor service already installed, skipping..."
fi

echo "✅ All done!"
echo "   VictoriaMetrics UI: http://localhost:8428"
echo "   Sensor logs: journalctl -fu weather-sensors"
