version: "3.9"

services:
  api:
    image: ghcr.io/acme/podcast-api:2.8.1
    restart: unless-stopped
    ports:
      - "8080:8080"
    environment:
      APP_ENV: production
      PORT: "8080"
      DATABASE_URL: postgresql://podcast:${POSTGRES_PASSWORD}@postgres:5432/podcast
      REDIS_URL: redis://redis:6379/0
      STORAGE_BUCKET: ${STORAGE_BUCKET}
      STORAGE_REGION: ${STORAGE_REGION:-us-east-1}
      CDN_BASE_URL: ${CDN_BASE_URL}
      SESSION_SECRET: ${SESSION_SECRET}
      TRANSCODE_QUEUE: podcast-transcode
      MAX_UPLOAD_SIZE_MB: "2048"
      LOG_LEVEL: info
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost:8080/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 20s
    networks:
      - frontend
      - backend

  worker:
    image: ghcr.io/acme/podcast-worker:2.8.1
    restart: unless-stopped
    environment:
      APP_ENV: production
      DATABASE_URL: postgresql://podcast:${POSTGRES_PASSWORD}@postgres:5432/podcast
      REDIS_URL: redis://redis:6379/0
      STORAGE_BUCKET: ${STORAGE_BUCKET}
      STORAGE_REGION: ${STORAGE_REGION:-us-east-1}
      TRANSCODE_QUEUE: podcast-transcode
      FFMPEG_THREADS: "4"
      LOG_LEVEL: info
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    deploy:
      resources:
        limits:
          cpus: "4.0"
          memory: 8G
        reservations:
          cpus: "1.0"
          memory: 2G
    networks:
      - backend

  web:
    image: ghcr.io/acme/podcast-web:2.8.1
    restart: unless-stopped
    ports:
      - "80:3000"
    environment:
      NODE_ENV: production
      API_BASE_URL: http://api:8080
      PUBLIC_BASE_URL: ${PUBLIC_BASE_URL}
      ANALYTICS_ENABLED: "true"
    depends_on:
      api:
        condition: service_healthy
    networks:
      - frontend

  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: podcast
      POSTGRES_USER: podcast
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U podcast -d podcast"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - backend

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes", "--maxmemory", "1gb", "--maxmemory-policy", "allkeys-lru"]
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
    networks:
      - backend

volumes:
  postgres_data:
  redis_data:

networks:
  frontend:
  backend:
    internal: true