#!/bin/bash
# TIP: Setup script for Erik server (.82)
# Run as root or with sudo

set -euo pipefail

echo "=== TIP: Erik Server Setup ==="
echo ""

# 1. PostgreSQL 17 + TimescaleDB
echo "--- Installing PostgreSQL 17 + TimescaleDB ---"
apt-get update
apt-get install -y gnupg2 lsb-release

# TimescaleDB repo (includes PostgreSQL 17)
echo "deb https://packagecloud.io/timescale/timescaledb/debian/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/timescaledb.list
curl -fsSL https://packagecloud.io/timescale/timescaledb/gpgkey | gpg --dearmor -o /etc/apt/trusted.gpg.d/timescaledb.gpg

apt-get update
apt-get install -y timescaledb-2-postgresql-17

# Enable TimescaleDB
timescaledb-tune --quiet --yes

# pgvector
apt-get install -y postgresql-17-pgvector

systemctl restart postgresql
systemctl enable postgresql

# Create DB and user
sudo -u postgres psql <<SQL
CREATE USER tip WITH PASSWORD '${POSTGRES_PASSWORD:-***REDACTED***}';
CREATE DATABASE transceiver_db OWNER tip;
GRANT ALL PRIVILEGES ON DATABASE transceiver_db TO tip;
\c transceiver_db
CREATE EXTENSION IF NOT EXISTS timescaledb;
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SQL

echo "PostgreSQL 17 + TimescaleDB + pgvector ready."

# 2. Qdrant
echo ""
echo "--- Installing Qdrant (Docker) ---"
docker pull qdrant/qdrant:latest
docker run -d \
  --name tip-qdrant \
  --restart unless-stopped \
  -p 6333:6333 \
  -p 6334:6334 \
  -v /opt/tip/qdrant:/qdrant/storage \
  qdrant/qdrant:latest

echo "Qdrant running on port 6333."

# 3. App directory
echo ""
echo "--- Setting up app directory ---"
mkdir -p /opt/tip
cd /opt/tip

echo "=== Setup complete ==="
echo ""
echo "Next steps:"
echo "  1. Clone repo: git clone <repo> /opt/tip/app"
echo "  2. cd /opt/tip/app && npm install"
echo "  3. cp .env.example .env && edit .env"
echo "  4. npm run migrate"
echo "  5. npm run seed"
echo "  6. pm2 start ecosystem.config.js"
