#!/usr/bin/env bash
# Install TicketGate as a persistent production service (requires sudo)
set -euo pipefail

ROOT="/var/www/html/ticket-gate"
cd "$ROOT"

echo "==> Building..."
npm install
npm run build

echo "==> Installing systemd service..."
sudo cp deploy/ticket-gate.service /etc/systemd/system/ticket-gate.service
sudo systemctl daemon-reload
sudo systemctl enable ticket-gate
sudo systemctl restart ticket-gate

echo "==> Installing Apache reverse proxy..."
sudo cp deploy/ticket-gate.apache.conf /etc/apache2/sites-available/ticket-gate.conf
sudo a2enmod proxy proxy_http headers rewrite ssl
sudo a2ensite ticket-gate.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

echo "==> Status"
sudo systemctl status ticket-gate --no-pager
echo ""
echo "Health: curl -s http://127.0.0.1:3847/api/health"
echo "Public (after DNS): curl -s https://ticket-gate.sitecreateur.fr/api/health"
echo ""
echo "IMPORTANT: Point ticket-gate.sitecreateur.fr DNS to this server's IP."
echo "Gmail callback: https://ticket-gate.sitecreateur.fr/api/gmail/callback"
