version: "3.9"

services:
  api:
    image: ghcr.io/roast-route/subscription-api:${APP_VERSION:-latest}
    restart: unless-stopped
    environment:
      NODE_ENV: production
      PORT: "8080"
      DATABASE_URL: postgresql://coffee:${DB_PASSWORD}@postgres:5432/subscriptions
      REDIS_URL: redis://redis:6379/0
      STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY}
      STRIPE_WEBHOOK_SECRET: ${STRIPE_WEBHOOK_SECRET}
      SESSION_SECRET: ${SESSION_SECRET}
      DEFAULT_CURRENCY: USD
      ORDER_CUTOFF_TIME: "16:00"
      ROASTING_TIMEZONE: America/New_York
    ports:
      - "8080:8080"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--spider", "http://localhost:8080/health"]
      interval: 15s
      timeout: 5s
      retries: 5
      start_period: 20s

  worker:
    image: ghcr.io/roast-route/subscription-api:${APP_VERSION:-latest}
    restart: unless-stopped
    command: ["npm", "run", "worker"]
    environment:
      NODE_ENV: production
      DATABASE_URL: postgresql://coffee:${DB_PASSWORD}@postgres:5432/subscriptions
      REDIS_URL: redis://redis:6379/0
      STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY}
      SHIPPING_PROVIDER_TOKEN: ${SHIPPING_PROVIDER_TOKEN}
      ROASTING_TIMEZONE: America/New_York
    depends_on:
      api:
        condition: service_healthy

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

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

volumes:
  postgres_data:
  redis_data: