#!/bin/bash

echo "=== YourPart Deployment mit vorhandener Konfiguration ==="

# Prüfen ob wir im richtigen Verzeichnis sind
if [ ! -f "package.json" ]; then
    echo "Error: Bitte führen Sie dieses Skript aus dem YourPart3-Root-Verzeichnis aus"
    exit 1
fi

# Frontend bauen
echo "Building frontend..."
cd frontend
npm ci
npm run build
cd ..

# Verzeichnisse erstellen
echo "Creating directories..."
sudo mkdir -p /opt/yourpart/{frontend,backend,images/{tmp,userimages,screenshots}}

# Frontend kopieren
echo "Deploying frontend..."
sudo cp -r frontend/dist /opt/yourpart/frontend/
sudo chown -R www-data:www-data /opt/yourpart/frontend
sudo chmod -R 755 /opt/yourpart/frontend

# Backend kopieren
echo "Deploying backend..."
sudo cp -r backend/* /opt/yourpart/backend/
sudo chown -R www-data:www-data /opt/yourpart/backend
sudo chmod -R 755 /opt/yourpart/backend

# .env-Datei erstellen
echo "Creating .env file..."
cat > /opt/yourpart/backend/.env << 'ENVEOF'
# Datenbank-Konfiguration (aus alter App)
DB_HOST=localhost
DB_USER=yourpart
DB_PASS=hitomisan
DB_NAME=yp2
DB_PORT=60000

# Anwendungskonfiguration
NODE_ENV=production
PORT=2020
STAGE=production

# Redis-Konfiguration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASS=your_redis_password

# Session-Konfiguration (aus alter App)
SECRET_KEY=k7e0CCw75PcmEGa
SESSION_SECRET=k7e0CCw75PcmEGa

# E-Mail-Konfiguration (aus alter App)
SMTP_HOST=smtp.1blu.de
SMTP_PORT=465
SMTP_USER=e226079_0-tsschulz
SMTP_PASS=hitomisan
SMTP_SECURE=true

# E-Mail-Einstellungen (aus alter App)
SENDER_NAME=YourPart
SENDER_EMAIL=kontakt@your-part.de

# AMQP-Konfiguration (aus alter App)
AMQP_HOST=tsschulz.de
AMQP_PORT=5672
AMQP_EXCHANGE=yourpart
AMQP_USERNAME=yourpart
AMQP_PASSWORD=yourpart

# API-Keys (aus alter App)
WEATHER_API_KEY=d0ddfcbc915f50263274211648a5dab0
NEWS_API_KEY=pub_212733602779de7708a7374d67e363bd06af4

# Pfad-Konfiguration (aus alter App)
ROOT_PATH=/opt/yourpart
IMAGES_PATH=/images
TMP_IMAGES_PATH=/images/tmp
USER_IMAGES_PATH=/images/userimages
SCREENSHOT_IMAGES_PATH=/images/screenshots

# URL-Konfiguration
BASE_URL=https://www.your-part.de
IMAGES_URL=https://www.your-part.de/images/
ACTIVATION_URL=https://www.your-part.de/activate?code=
PASSWORD_RESET_URL=https://www.your-part.de/setnewpassword?username=

# Spiel-Konfiguration (aus alter App)
START_BUDGET=10
TEST_MODE=false
PAY_PER_DAY=100

# Debug-Einstellungen
DEBUG_SQL=false
DEBUG_MESSAGES=false
DEBUG_RECREATE_DB=false
ENVEOF

sudo chown www-data:www-data /opt/yourpart/backend/.env
sudo chmod 600 /opt/yourpart/backend/.env

# Service installieren
echo "Installing service..."
sudo cp yourpart.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable yourpart.service

# Apache-Konfiguration
echo "Configuring Apache..."
sudo cp yourpart-http.conf /etc/apache2/sites-available/
sudo cp yourpart-https.conf /etc/apache2/sites-available/

# Alte Konfiguration deaktivieren
sudo a2dissite yourpart 2>/dev/null || true

# Neue Konfigurationen aktivieren
sudo a2ensite yourpart-http
sudo a2ensite yourpart-https

# Apache-Module aktivieren
sudo a2enmod proxy proxy_http proxy_wstunnel rewrite ssl
sudo systemctl reload apache2

# Service starten
echo "Starting service..."
sudo systemctl start yourpart.service

echo ""
echo "=== Deployment abgeschlossen! ==="
echo "Frontend: /opt/yourpart/frontend/dist/"
echo "Backend: /opt/yourpart/backend/"
echo "Service: yourpart.service"
echo ""
echo "Status prüfen:"
echo "  sudo systemctl status yourpart.service"
echo "  sudo systemctl status apache2"
echo "  sudo journalctl -u yourpart.service -f"
