# MailFlow Production Setup Script
# Dieses Script hilft beim Setup für Production

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "MailFlow Production Setup" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Prüfe ob Node.js installiert ist
Write-Host "[1/5] Prüfe Node.js Installation..." -ForegroundColor Yellow
try {
    $nodeVersion = node --version
    Write-Host "✓ Node.js gefunden: $nodeVersion" -ForegroundColor Green
} catch {
    Write-Host "✗ Node.js ist nicht installiert!" -ForegroundColor Red
    Write-Host "  Bitte installiere Node.js von https://nodejs.org/" -ForegroundColor Yellow
    exit 1
}

# Prüfe ob PM2 installiert ist
Write-Host ""
Write-Host "[2/5] Prüfe PM2 Installation..." -ForegroundColor Yellow
try {
    $pm2Version = pm2 --version
    Write-Host "✓ PM2 gefunden: $pm2Version" -ForegroundColor Green
} catch {
    Write-Host "✗ PM2 ist nicht installiert" -ForegroundColor Yellow
    Write-Host "  Installiere PM2..." -ForegroundColor Yellow
    npm install -g pm2
    Write-Host "✓ PM2 installiert" -ForegroundColor Green
}

# Backend Setup
Write-Host ""
Write-Host "[3/5] Backend Setup..." -ForegroundColor Yellow
$serverPath = Join-Path $PSScriptRoot "server"
if (Test-Path $serverPath) {
    Set-Location $serverPath
    
    # Prüfe .env Datei
    if (-not (Test-Path ".env")) {
        Write-Host "⚠ .env Datei nicht gefunden!" -ForegroundColor Yellow
        Write-Host "  Erstelle .env aus env.example..." -ForegroundColor Yellow
        Copy-Item "env.example" ".env"
        Write-Host "  ⚠ WICHTIG: Bearbeite server/.env und setze die Production-Werte:" -ForegroundColor Red
        Write-Host "    - NODE_ENV=production" -ForegroundColor Yellow
        Write-Host "    - FRONTEND_URL=https://emailsorter.webklar.com" -ForegroundColor Yellow
        Write-Host "    - CORS_ORIGIN=https://emailsorter.webklar.com" -ForegroundColor Yellow
        Write-Host "    - BASE_URL=https://api.emailsorter.webklar.com (oder deine API URL)" -ForegroundColor Yellow
        Write-Host ""
        Write-Host "  Drücke Enter, wenn du die .env Datei bearbeitet hast..." -ForegroundColor Cyan
        Read-Host
    }
    
    # Installiere Dependencies
    Write-Host "  Installiere Backend Dependencies..." -ForegroundColor Yellow
    npm install
    Write-Host "✓ Backend Dependencies installiert" -ForegroundColor Green
    
    # Prüfe ob Server bereits läuft
    $pm2List = pm2 list 2>&1
    if ($pm2List -match "mailflow-api") {
        Write-Host "  Server läuft bereits. Neustart..." -ForegroundColor Yellow
        pm2 restart mailflow-api
    } else {
        Write-Host "  Starte Backend Server mit PM2..." -ForegroundColor Yellow
        pm2 start index.mjs --name mailflow-api
        pm2 save
    }
    
    Write-Host "✓ Backend Server gestartet" -ForegroundColor Green
    Write-Host "  Status: pm2 status" -ForegroundColor Cyan
    Write-Host "  Logs: pm2 logs mailflow-api" -ForegroundColor Cyan
} else {
    Write-Host "✗ Server Verzeichnis nicht gefunden!" -ForegroundColor Red
}

# Frontend Build
Write-Host ""
Write-Host "[4/5] Frontend Build..." -ForegroundColor Yellow
$clientPath = Join-Path $PSScriptRoot "client"
if (Test-Path $clientPath) {
    Set-Location $clientPath
    
    # Prüfe .env.production
    if (-not (Test-Path ".env.production")) {
        Write-Host "  Erstelle .env.production..." -ForegroundColor Yellow
        $envContent = @"
VITE_APPWRITE_ENDPOINT=https://appwrite.webklar.com/v1
VITE_APPWRITE_PROJECT_ID=DEINE_PROJEKT_ID
VITE_API_URL=https://api.emailsorter.webklar.com
"@
        Set-Content -Path ".env.production" -Value $envContent
        Write-Host "  ⚠ WICHTIG: Bearbeite client/.env.production und setze die richtigen Werte!" -ForegroundColor Red
        Write-Host ""
        Write-Host "  Drücke Enter, wenn du die .env.production Datei bearbeitet hast..." -ForegroundColor Cyan
        Read-Host
    }
    
    # Installiere Dependencies
    Write-Host "  Installiere Frontend Dependencies..." -ForegroundColor Yellow
    npm install
    Write-Host "✓ Frontend Dependencies installiert" -ForegroundColor Green
    
    # Build
    Write-Host "  Baue Frontend für Production..." -ForegroundColor Yellow
    npm run build
    Write-Host "✓ Frontend Build abgeschlossen" -ForegroundColor Green
    Write-Host "  Build-Ordner: client/dist" -ForegroundColor Cyan
    Write-Host "  ⚠ WICHTIG: Deploye den client/dist Ordner zu deinem Web-Server!" -ForegroundColor Yellow
} else {
    Write-Host "✗ Client Verzeichnis nicht gefunden!" -ForegroundColor Red
}

# Zusammenfassung
Write-Host ""
Write-Host "[5/5] Zusammenfassung" -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "✓ Backend Setup abgeschlossen" -ForegroundColor Green
Write-Host "✓ Frontend Build abgeschlossen" -ForegroundColor Green
Write-Host ""
Write-Host "⚠ NOCH ZU TUN:" -ForegroundColor Red
Write-Host ""
Write-Host "1. APPWRITE CORS KONFIGURIEREN:" -ForegroundColor Yellow
Write-Host "   - Gehe zu https://appwrite.webklar.com" -ForegroundColor White
Write-Host "   - Öffne dein Projekt" -ForegroundColor White
Write-Host "   - Settings → Platforms → Add Platform" -ForegroundColor White
Write-Host "   - Hostname: emailsorter.webklar.com" -ForegroundColor White
Write-Host "   - Origin: https://emailsorter.webklar.com" -ForegroundColor White
Write-Host ""
Write-Host "2. FRONTEND DEPLOYEN:" -ForegroundColor Yellow
Write-Host "   - Kopiere client/dist zu deinem Web-Server" -ForegroundColor White
Write-Host "   - Stelle sicher, dass die Domain richtig konfiguriert ist" -ForegroundColor White
Write-Host ""
Write-Host "3. BACKEND ÜBERWACHEN:" -ForegroundColor Yellow
Write-Host "   - pm2 status (Server Status prüfen)" -ForegroundColor White
Write-Host "   - pm2 logs emailsorter-api (Logs ansehen)" -ForegroundColor White
Write-Host "   - pm2 monit (Live Monitoring)" -ForegroundColor White
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan

Set-Location $PSScriptRoot
