services:
  backend:
    build: ./backend
    container_name: bellsystems-backend
    env_file: .env
    volumes:
      - ./backend:/app
      - ./data:/app/data
      - ./data/built_melodies:/app/storage/built_melodies
      - ./data/firmware:/app/storage/firmware
      - ./data/flash_assets:/app/storage/flash_assets
      - ./data/melody_binaries:/app/storage/melody_binaries
      - ./data/firebase-service-account.json:/app/firebase-service-account.json:ro
    ports:
      - "8000:8000"
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - internal

  frontend:
    build: ./frontend
    container_name: bellsystems-frontend
    networks:
      - internal

  nginx:
    image: nginx:alpine
    container_name: bellsystems-nginx
    ports:
      - "90:80"     # access v2 on localhost:8001
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - backend
      - frontend
    networks:
      - internal

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

networks:
  internal:
    driver: bridge
