version: "3.9"

services:
  web:
    image: ghcr.io/roastcycle/subscription-api:${APP_VERSION:-latest}
    command: bundle exec puma -C config/puma.rb
    ports:
      - "8080:8080"
    environment:
      RAILS_ENV: production
      PORT: "8080"
      DATABASE_URL: postgres://roastcycle:${POSTGRES_PASSWORD}@postgres:5432/roastcycle_production
      REDIS_URL: redis://redis:6379/0
      STRIPE_WEBHOOK_SECRET: ${STRIPE_WEBHOOK_SECRET}
      STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY}
      SHIPPO_API_TOKEN: ${SHIPPO_API_TOKEN}
      DEFAULT_ROAST_CADENCE_DAYS: "14"
      INVENTORY_RESERVE_HOURS: "48"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    restart: unless-stopped

  worker:
    image: ghcr.io/roastcycle/subscription-api:${APP_VERSION:-latest}
    command: bundle exec sidekiq -C config/sidekiq.yml
    environment:
      RAILS_ENV: production
      DATABASE_URL: postgres://roastcycle:${POSTGRES_PASSWORD}@postgres:5432/roastcycle_production
      REDIS_URL: redis://redis:6379/0
      STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY}
      SHIPPO_API_TOKEN: ${SHIPPO_API_TOKEN}
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    restart: unless-stopped

  scheduler:
    image: ghcr.io/roastcycle/subscription-api:${APP_VERSION:-latest}
    command: bundle exec good_job start --queues=subscription_renewals,shipment_batches,inventory_reorder
    environment:
      RAILS_ENV: production
      DATABASE_URL: postgres://roastcycle:${POSTGRES_PASSWORD}@postgres:5432/roastcycle_production
      REDIS_URL: redis://redis:6379/0
      RENEWAL_CUTOFF_HOUR_UTC: "16"
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped

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

  redis:
    image: redis:7-alpine
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
    restart: unless-stopped

volumes:
  postgres_data:
  redis_data: