# KobraX Full Stack — KX-Bridge + Obico Self-Hosted + Spoolman
#
# Für Portainer: Stack → Add Stack → Upload → diese Datei wählen
#
# Voraussetzung: Obico-Images einmalig in Gitea-Registry pushen:
#   docker tag obico-server-web:latest    gitea.it-drui.de/viewit/obico-web:latest
#   docker tag obico-server-ml_api:latest gitea.it-drui.de/viewit/obico-ml:latest
#   docker tag obico-server-tasks:latest  gitea.it-drui.de/viewit/obico-tasks:latest
#   docker push gitea.it-drui.de/viewit/obico-web:latest
#   docker push gitea.it-drui.de/viewit/obico-ml:latest
#   docker push gitea.it-drui.de/viewit/obico-tasks:latest
#
# Persistente Daten: /mnt/dockerdata/KobraXStack/<service>/
#
# Ports:
#   7125  — KX-Bridge  (Moonraker-API)
#   3334  — Obico      (Web-UI)
#   7912  — Spoolman   (Web-UI)
#
# Obico Admin-Account nach dem ersten Start:
#   docker exec obico-web python manage.py createsuperuser

x-obico-base: &obico-base
  restart: unless-stopped
  volumes:
    - /mnt/dockerdata/KobraXStack/obico/data:/data
    - /mnt/dockerdata/KobraXStack/obico/frontend:/frontend
  depends_on:
    - obico-redis
  environment:
    DEBUG: "False"
    REDIS_URL: "redis://obico-redis:6379"
    DATABASE_URL: "sqlite:////data/db.sqlite3"
    INTERNAL_MEDIA_HOST: "http://obico-web:3334"
    ML_API_HOST: "http://obico-ml:3333"
    ACCOUNT_ALLOW_SIGN_UP: "False"
    SITE_USES_HTTPS: "False"
    SITE_IS_PUBLIC: "False"
    DJANGO_SECRET_KEY: "change-me-to-a-random-secret-key-before-use"
    WEBPACK_LOADER_ENABLED: "False"
  networks:
    - kobrax-stack

services:

  # ── KX-Bridge ───────────────────────────────────────────────
  kx-bridge:
    image: gitea.it-drui.de/viewit/kx-bridge:latest
    container_name: kx-bridge
    restart: unless-stopped
    ports:
      - "7125:7125"
    volumes:
      - /mnt/dockerdata/KobraXStack/kx-bridge/config:/app/config
      - /mnt/dockerdata/KobraXStack/kx-bridge/data:/app/data
    networks:
      - kobrax-stack
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  # ── Spoolman ────────────────────────────────────────────────
  spoolman:
    image: ghcr.io/donkie/spoolman:latest
    container_name: spoolman
    restart: unless-stopped
    ports:
      - "7912:8000"
    volumes:
      - /mnt/dockerdata/KobraXStack/spoolman:/home/app/.local/share/spoolman
    networks:
      - kobrax-stack
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  # ── Obico Redis ─────────────────────────────────────────────
  obico-redis:
    image: redis:7.2-alpine
    container_name: obico-redis
    restart: unless-stopped
    volumes:
      - /mnt/dockerdata/KobraXStack/obico/redis:/data
    networks:
      - kobrax-stack
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      start_period: 10s
      interval: 15s
      timeout: 5s
      retries: 10
    logging:
      driver: json-file
      options:
        max-size: "5m"
        max-file: "2"

  # ── Obico ML API ────────────────────────────────────────────
  obico-ml:
    image: gitea.it-drui.de/viewit/obico-ml:latest
    container_name: obico-ml
    restart: unless-stopped
    command: bash -c "gunicorn --bind 0.0.0.0:3333 --workers 1 wsgi"
    working_dir: /app
    environment:
      DEBUG: "False"
      FLASK_APP: "server.py"
    networks:
      - kobrax-stack
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider http://127.0.0.1:3333/hc/ || exit 1"]
      start_period: 30s
      interval: 30s
      timeout: 10s
      retries: 3
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  # ── Obico Web ───────────────────────────────────────────────
  obico-web:
    <<: *obico-base
    image: gitea.it-drui.de/viewit/obico-web:latest
    container_name: obico-web
    ports:
      - "3334:3334"
    depends_on:
      - obico-ml
      - obico-redis
    command: >
      sh -c 'python manage.py migrate &&
             python manage.py shell -c "from django.contrib.sites.models import Site; s=Site.objects.first(); s.domain=\"192.168.178.204:3334\"; s.name=\"Obico\"; s.save()" &&
             python manage.py collectstatic --noinput &&
             daphne -b 0.0.0.0 -p 3334 config.routing:application'
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider http://127.0.0.1:3334/hc/ || exit 1"]
      start_period: 60s
      interval: 90s
      timeout: 20s
      retries: 3
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  # ── Obico Tasks (Celery) ────────────────────────────────────
  obico-tasks:
    <<: *obico-base
    image: gitea.it-drui.de/viewit/obico-tasks:latest
    container_name: obico-tasks
    command: sh -c "celery -A config worker --beat -l info -c 2 -Q realtime,celery"
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  # ── moonraker-obico Plugin ──────────────────────────────────
  # Verbindet KX-Bridge mit dem Obico-Server (Spaghetti-Detektion, Remote-UI)
  # Voraussetzung: /mnt/dockerdata/KobraXStack/moonraker-obico/moonraker-obico.cfg
  # muss existieren und einen gültigen auth_token enthalten.
  #
  # Token holen (nach erstem obico-web Start):
  #   docker exec obico-web python manage.py shell -c "
  #   from app.models import OneTimeVerificationCode, User
  #   from django.utils import timezone; from datetime import timedelta; import random
  #   u = User.objects.first()
  #   c = OneTimeVerificationCode.objects.create(user=u, code='%06d' % random.randint(100000,999999), expired_at=timezone.now()+timedelta(hours=2))
  #   print('CODE:', c.code)"
  #   curl -X POST 'http://localhost:3334/api/v1/octo/verify/?code=<CODE>'
  #   → printer.auth_token aus der Antwort in die cfg eintragen
  moonraker-obico:
    image: gitea.it-drui.de/viewit/moonraker-obico:latest
    container_name: moonraker-obico
    restart: unless-stopped
    network_mode: host
    volumes:
      - /mnt/dockerdata/KobraXStack/moonraker-obico:/opt/printer_data/config
      - /mnt/dockerdata/KobraXStack/moonraker-obico/logs:/opt/printer_data/logs
    command: ["-c", "/opt/printer_data/config/moonraker-obico.cfg"]
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

networks:
  kobrax-stack:
    driver: bridge

# Verzeichnisse müssen auf dem Host existieren:
# mkdir -p /mnt/dockerdata/KobraXStack/kx-bridge/config \
#           /mnt/dockerdata/KobraXStack/kx-bridge/data \
#           /mnt/dockerdata/KobraXStack/spoolman \
#           /mnt/dockerdata/KobraXStack/obico/data \
#           /mnt/dockerdata/KobraXStack/obico/frontend \
#           /mnt/dockerdata/KobraXStack/obico/redis \
#           /mnt/dockerdata/KobraXStack/moonraker-obico/logs
# Spoolman benötigt UID/GID 1000:
# sudo chown -R 1000:1000 /mnt/dockerdata/KobraXStack/spoolman
#
# moonraker-obico Config anlegen (auth_token nach Obico-Setup eintragen):
# cp /path/to/moonraker-obico.cfg.example /mnt/dockerdata/KobraXStack/moonraker-obico/moonraker-obico.cfg
