#!/usr/bin/env bash
# Optionaler TEMPORÄRER UI-Test nach erfolgreichem Terminal-MVP.
# Startet Open WebUI mit einer untergeordneten KNP-Kennzeichnung.
#
# Verwendung:
#   sudo bash 05-enable-openwebui.sh ./local-llm.env

set -euo pipefail
CONFIG="${1:-./local-llm.env}"
[[ -r "$CONFIG" ]] || { echo "Konfiguration fehlt: $CONFIG"; exit 1; }
# shellcheck disable=SC1090
source "$CONFIG"

for v in DATA_ROOT SERVER_LAN_IP OPEN_WEBUI_PORT; do
  [[ -n "${!v:-}" && "${!v}" != __*__ ]] || { echo "Bitte $v in $CONFIG setzen."; exit 1; }
done
[[ "${EUID}" -eq 0 ]] || { echo "Bitte mit sudo/root ausführen."; exit 1; }

BRAND_DIR="$DATA_ROOT/branding"
install -d -o root -g root -m 0755 "$BRAND_DIR"

# Vorläufiges, lokal erzeugtes Wordmark nach dem öffentlichen KNP-Auftritt.
# Sobald ein offizielles CI-SVG vorliegt, diese Datei 1:1 ersetzen.
if [[ ! -f "$BRAND_DIR/knp-wordmark.svg" ]]; then
cat >"$BRAND_DIR/knp-wordmark.svg" <<'SVG'
<svg xmlns="http://www.w3.org/2000/svg" width="420" height="100" viewBox="0 0 420 100" role="img" aria-label="knp.bauphysik">
  <rect width="420" height="100" fill="#ffffff"/>
  <g fill="#8fbf3f">
    <circle cx="18" cy="24" r="5"/><circle cx="34" cy="24" r="5"/><circle cx="50" cy="24" r="5"/>
    <circle cx="18" cy="40" r="5"/><circle cx="34" cy="40" r="5"/>
    <circle cx="18" cy="56" r="5"/>
  </g>
  <text x="76" y="60" font-family="Arial,Helvetica,sans-serif" font-size="40" font-weight="500" fill="#262626">knp.bauphysik:</text>
</svg>
SVG
fi

# Compose-Overlay ergänzt Name und KNP-Hinweis; Open WebUI bleibt als technische Basis erkennbar.
cat >"$DATA_ROOT/stack/compose.knp-ui.yml" <<'EOF'
services:
  open-webui:
    environment:
      WEBUI_NAME: "knp.bauphysik AI"
      WEBUI_BANNERS: >-
        [{"id":"knp-internal-ai","type":"success","content":"<img src=\"/knp-brand/knp-wordmark.svg\" width=\"170\" height=\"42\"> <span style=\"color:#8fbf3f;font-weight:700;\">Interne KI-Testoberfläche</span><br><small>Technische Testoberfläche auf Basis von Open WebUI. Produktiv ist später die eigene KNP Office-AI-Webapp vorgesehen.</small>","dismissible":true,"timestamp":1791380000}]
EOF

cd "$DATA_ROOT/stack"
docker compose -f compose.yml -f compose.knp-ui.yml --profile test-ui up -d open-webui

cat >/etc/nginx/sites-available/office-ai <<EOF
server {
    listen ${SERVER_LAN_IP}:80;
    server_name _;

    location /knp-brand/ {
        alias ${BRAND_DIR}/;
        add_header Cache-Control "no-store";
    }

    location / {
        proxy_pass http://127.0.0.1:${OPEN_WEBUI_PORT};
        proxy_http_version 1.1;
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
    }
}
EOF

ln -sf /etc/nginx/sites-available/office-ai /etc/nginx/sites-enabled/office-ai
rm -f /etc/nginx/sites-enabled/default
nginx -t
systemctl reload nginx

echo "TEMPORÄRER TEST: http://$SERVER_LAN_IP/"
echo "KNP-Wordmark: $BRAND_DIR/knp-wordmark.svg"
echo "Nur Testdaten verwenden. Diese Direktverbindung zu Ollama ist NICHT die Produktionsarchitektur."
echo "Hinweis: Die Akzentfarbe #8fbf3f ist eine visuelle Annäherung; offizielles CI-Asset später ersetzen."
