x-db-env: &db_env
  POSTGRES_USER: postgres
  POSTGRES_DB: postgres
  POSTGRES_PASSWORD: postgres

x-rails-env: &rails_env
  DB_HOST: db
  # Bind the dev server to all interfaces inside the container so Docker's
  # published port reaches it from the host. Rails reads BINDING natively.
  BINDING: "0.0.0.0"
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: postgres
  BUNDLE_PATH: /bundle
  REDIS_URL: redis://redis:6379/1
  SELENIUM_REMOTE_URL: http://selenium:4444

services:
  app:
    build:
      context: ..
      dockerfile: .devcontainer/Dockerfile
    volumes:
      - ./.bashrc:/root/.bashrc:ro,cached
      - ..:/workspace:cached
      - bundle_cache:/bundle
    ports:
      - ${PORT:-3000}:3000
    command: sleep infinity
    environment:
      <<: *rails_env
    depends_on:
      - db
      - redis
      - selenium

  worker:
    build:
      context: ..
      dockerfile: .devcontainer/Dockerfile
    volumes:
      - ..:/workspace:cached
      - bundle_cache:/bundle
    command: bundle exec sidekiq
    restart: unless-stopped
    environment:
      <<: *rails_env
    depends_on:
      - redis

  redis:
    image: redis:latest
    volumes:
      - redis-data:/data
    restart: unless-stopped
  
  db:
    image: postgres:16
    volumes:
      - postgres-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    restart: unless-stopped
    environment:
      <<: *db_env

  selenium:
    image: selenium/standalone-chromium:latest
    ports:
      - "4444:4444"
      - "7900:7900"
    shm_size: 2gb
    restart: unless-stopped

volumes:
  postgres-data:
  redis-data:
  bundle_cache:
