#!/bin/bash
# Dealspace Installation Script
# Run as root on Shannon (82.24.174.112)

set -e

INSTALL_DIR=/opt/dealspace
SERVICE_USER=johan

echo "=== Dealspace Installation ==="

# Create user if missing
if ! id "$SERVICE_USER" &>/dev/null; then
    echo "Creating user $SERVICE_USER..."
    useradd -r -s /bin/bash -d /home/$SERVICE_USER -m $SERVICE_USER
fi

# Create directory structure
echo "Creating directories..."
mkdir -p $INSTALL_DIR/{bin,data,store,logs,backups,migrations}

# Generate MASTER_KEY if .env doesn't exist
if [ ! -f "$INSTALL_DIR/.env" ]; then
    echo "Creating .env with new MASTER_KEY..."
    MASTER_KEY=$(openssl rand -hex 32)
    cat > $INSTALL_DIR/.env <<ENVEOF
# Core
MASTER_KEY=$MASTER_KEY
DB_PATH=$INSTALL_DIR/data/dealspace.db
STORE_PATH=$INSTALL_DIR/store
PORT=8080
ENV=production

# Auth
SESSION_TTL_HOURS=1
REFRESH_TTL_DAYS=7

# Seeding (set to true on first run only, then remove)
SEED_DEMO=false

# Email (Stalwart SMTP at mail.jongsma.me)
SMTP_HOST=mail.jongsma.me
SMTP_PORT=587
SMTP_USER=
SMTP_PASS=
SMTP_FROM=noreply@muskepo.com

# AI (Fireworks — zero retention)
FIREWORKS_API_KEY=

# Monitoring
NTFY_URL=https://ntfy.inou.com/inou-alerts
NTFY_TOKEN=tk_k120jegay3lugeqbr9fmpuxdqmzx5
ENVEOF
    chmod 600 $INSTALL_DIR/.env
    echo "⚠️  MASTER_KEY generated. Back it up securely. NEVER CHANGE after data is written."
else
    echo ".env already exists, skipping..."
fi

# Set permissions
echo "Setting permissions..."
chown -R $SERVICE_USER:$SERVICE_USER $INSTALL_DIR
chmod 755 $INSTALL_DIR
chmod 700 $INSTALL_DIR/data $INSTALL_DIR/store $INSTALL_DIR/backups
chmod 755 $INSTALL_DIR/bin $INSTALL_DIR/logs

# Install systemd service
echo "Installing systemd service..."
cp "$(dirname "$0")/dealspace.service" /etc/systemd/system/dealspace.service
systemctl daemon-reload
systemctl enable dealspace

# Install backup script
echo "Installing backup script..."
cp "$(dirname "$0")/backup.sh" $INSTALL_DIR/backup.sh
chmod +x $INSTALL_DIR/backup.sh

# Install healthcheck script
echo "Installing healthcheck script..."
cp "$(dirname "$0")/healthcheck.sh" $INSTALL_DIR/healthcheck.sh
chmod +x $INSTALL_DIR/healthcheck.sh

# Install cron jobs
echo "Installing cron jobs..."
CRON_TMP=$(mktemp)

# Backup: daily at 3 AM
# Healthcheck: every 5 minutes
cat > $CRON_TMP <<CRONEOF
# Dealspace backup - daily at 3 AM
0 3 * * * $INSTALL_DIR/backup.sh >> $INSTALL_DIR/logs/backup.log 2>&1

# Dealspace healthcheck - every 5 minutes
*/5 * * * * $INSTALL_DIR/healthcheck.sh
CRONEOF

crontab -u $SERVICE_USER $CRON_TMP
rm $CRON_TMP

echo ""
echo "=== Installation Complete ==="
echo ""
echo "Next steps:"
echo "  1. Edit $INSTALL_DIR/.env with SMTP and Fireworks credentials"
echo "  2. Deploy the binary: make deploy"
echo "  3. Start the service: systemctl start dealspace"
echo "  4. Check status: systemctl status dealspace"
echo "  5. View logs: journalctl -u dealspace -f"
echo ""
echo "Cron jobs installed:"
echo "  - Daily backup at 3 AM"
echo "  - Healthcheck every 5 minutes"
