services:
  db:
    image: postgres:16-alpine
    shm_size: 1g  # parallel workers (np. seo.sitemap_index) potrzebują >64MB /dev/shm
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-goon}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-goon}
      POSTGRES_DB: ${POSTGRES_DB:-goon}
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
      - ./alembic/init:/docker-entrypoint-initdb.d:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
      interval: 5s
      timeout: 3s
      retries: 12
      start_period: 10s
    restart: unless-stopped

  api:
    build:
      context: .
      dockerfile: Dockerfile
    # Auto-apply pending migrations on startup, then exec uvicorn. Single API
    # replica only — multi-replica deployments must run migrations out-of-band.
    command:
      - bash
      - -c
      - |
        alembic upgrade head && \
        exec uvicorn app.main:app --host 0.0.0.0 --port 8000
    env_file: .env
    environment:
      DATABASE_URL: postgresql+psycopg://${POSTGRES_USER:-goon}:${POSTGRES_PASSWORD:-goon}@db:5432/${POSTGRES_DB:-goon}
      BACKEND_PUBLIC_URL: ${BACKEND_PUBLIC_URL:-}
    ports:
      - "${API_PORT:-8000}:8000"
    depends_on:
      db:
        condition: service_healthy
    restart: unless-stopped

  worker:
    build:
      context: .
      dockerfile: Dockerfile
    command: ["python", "-m", "app.scheduler.worker"]
    env_file: .env
    environment:
      DATABASE_URL: postgresql+psycopg://${POSTGRES_USER:-goon}:${POSTGRES_PASSWORD:-goon}@db:5432/${POSTGRES_DB:-goon}
    depends_on:
      db:
        condition: service_healthy
      api:
        # Worker waits for `api` so migrations are applied before any ingest job
        # tries to write. `api` itself blocks on `db` healthcheck.
        condition: service_started
    restart: unless-stopped

volumes:
  pgdata:
