version: '3.8'

services:
  # PostgreSQL Database
  db:
    image: postgres:15-alpine
    container_name: stiftung-db
    restart: unless-stopped
    environment:
      POSTGRES_DB: stiftung
      POSTGRES_USER: stiftung_user
      POSTGRES_PASSWORD: ${DB_PASSWORD:-stiftung_password}
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8 --lc-collate=C --lc-ctype=C"
    volumes:
      - ./data/db:/var/lib/postgresql/data
      - ./data/backups:/backups
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U stiftung_user -d stiftung"]
      interval: 30s
      timeout: 10s
      retries: 3
    networks:
      - stiftung-network

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: stiftung-redis
    restart: unless-stopped
    volumes:
      - ./data/redis:/data
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
    networks:
      - stiftung-network

  # Main Web Application
  web:
    build:
      context: ../app
      dockerfile: Dockerfile
    container_name: stiftung-web
    restart: unless-stopped
    environment:
      - POSTGRES_DB=stiftung
      - POSTGRES_USER=stiftung_user
      - POSTGRES_PASSWORD=${DB_PASSWORD:-stiftung_password}
      - DB_HOST=db
      - DB_PORT=5432
      - REDIS_URL=redis://redis:6379/0
      - PAPERLESS_URL=${PAPERLESS_URL}
      - PAPERLESS_TOKEN=${PAPERLESS_TOKEN}
      - PAPERLESS_API_URL=${PAPERLESS_URL}
      - PAPERLESS_API_TOKEN=${PAPERLESS_TOKEN}
      - SECRET_KEY=${SECRET_KEY}
      - DEBUG=${DEBUG:-False}
      - ALLOWED_HOSTS=${ALLOWED_HOSTS:-localhost,127.0.0.1}
      - DJANGO_SECRET_KEY=${SECRET_KEY}
      - DJANGO_DEBUG=${DEBUG:-0}
      - DJANGO_ALLOWED_HOSTS=${ALLOWED_HOSTS:-localhost,127.0.0.1}
      - CSRF_TRUSTED_ORIGINS=${CSRF_TRUSTED_ORIGINS:-http://localhost:8081}
    volumes:
      - ./data/uploads:/app/uploads
      - ./data/backups:/app/backups
      - ./logs:/app/logs
    ports:
      - "8081:8000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health/"]
      interval: 30s
      timeout: 10s
      retries: 3
    networks:
      - stiftung-network

  # Celery Worker for Background Tasks
  worker:
    build:
      context: ../app
      dockerfile: Dockerfile
    container_name: stiftung-worker
    restart: unless-stopped
    command: celery -A core worker -l info
    environment:
      - POSTGRES_DB=stiftung
      - POSTGRES_USER=stiftung_user
      - POSTGRES_PASSWORD=${DB_PASSWORD:-stiftung_password}
      - DB_HOST=db
      - DB_PORT=5432
      - REDIS_URL=redis://redis:6379/0
      - PAPERLESS_URL=${PAPERLESS_URL}
      - PAPERLESS_TOKEN=${PAPERLESS_TOKEN}
      - PAPERLESS_API_URL=${PAPERLESS_URL}
      - PAPERLESS_API_TOKEN=${PAPERLESS_TOKEN}
      - SECRET_KEY=${SECRET_KEY}
      - DEBUG=${DEBUG:-False}
      - DJANGO_SECRET_KEY=${SECRET_KEY}
      - DJANGO_DEBUG=${DEBUG:-0}
      - DJANGO_ALLOWED_HOSTS=${ALLOWED_HOSTS:-localhost,127.0.0.1}
    volumes:
      - ./data/uploads:/app/uploads
      - ./data/backups:/app/backups
      - ./logs:/app/logs
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - stiftung-network

  # Celery Beat for Scheduled Tasks
  beat:
    build:
      context: ../app
      dockerfile: Dockerfile
    container_name: stiftung-beat
    restart: unless-stopped
    command: celery -A core beat -l info
    environment:
      - POSTGRES_DB=stiftung
      - POSTGRES_USER=stiftung_user
      - POSTGRES_PASSWORD=${DB_PASSWORD:-stiftung_password}
      - DB_HOST=db
      - DB_PORT=5432
      - REDIS_URL=redis://redis:6379/0
      - PAPERLESS_URL=${PAPERLESS_URL}
      - PAPERLESS_TOKEN=${PAPERLESS_TOKEN}
      - PAPERLESS_API_URL=${PAPERLESS_URL}
      - PAPERLESS_API_TOKEN=${PAPERLESS_TOKEN}
      - SECRET_KEY=${SECRET_KEY}
      - DEBUG=${DEBUG:-False}
      - DJANGO_SECRET_KEY=${SECRET_KEY}
      - DJANGO_DEBUG=${DEBUG:-0}
      - DJANGO_ALLOWED_HOSTS=${ALLOWED_HOSTS:-localhost,127.0.0.1}
    volumes:
      - ./data/uploads:/app/uploads
      - ./data/backups:/app/backups
      - ./logs:/app/logs
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - stiftung-network


networks:
  stiftung-network:
    driver: bridge

volumes:
  db_data:
  redis_data:
  uploads:
  backups:
  logs:
