version: "3.9"

# Deploy layout expected: /srv/voip/pbx-app/  <- this whole project (unzipped)
# Run from inside pbx-app/docker/:  docker compose up -d --build
#
# The PHP app joins both the internal network (to reach MariaDB) and the
# external "web" network (so your existing Caddy container can reverse_proxy
# to it by container name). Asterisk only needs the internal network for its
# DB connection — SIP/RTP is published directly as UDP ports below, since
# Caddy (HTTP/HTTPS only) can't proxy SIP traffic.

services:
  db:
    image: mariadb:11
    container_name: voip_db
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
      - ../migrations:/docker-entrypoint-initdb.d:ro
    networks:
      - voip_internal

  app:
    build:
      context: ..
      dockerfile: docker/app/Dockerfile
    container_name: voip_app
    restart: unless-stopped
    depends_on:
      - db
    environment:
      DB_HOST: db
      DB_PORT: 3306
      DB_NAME: ${DB_NAME}
      DB_USER: ${DB_USER}
      DB_PASS: ${DB_PASSWORD}
      APP_ENV: production
      SESSION_NAME: pbx_session
      SIGNALWIRE_SIP_DOMAIN: ${SIGNALWIRE_SIP_DOMAIN}
      TTS_PROVIDER: ${TTS_PROVIDER:-polly}
      AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID:-}
      AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY:-}
      AWS_REGION: ${AWS_REGION:-us-east-1}
      TRANSCRIPTION_PROVIDER: ${TRANSCRIPTION_PROVIDER:-whisper}
      OPENAI_API_KEY: ${OPENAI_API_KEY:-}
      SMTP_HOST: ${SMTP_HOST:-}
      SMTP_PORT: ${SMTP_PORT:-587}
      SMTP_USER: ${SMTP_USER:-}
      SMTP_PASS: ${SMTP_PASS:-}
      SMTP_FROM: ${SMTP_FROM:-pbx@localhost}
      SMTP_ENCRYPTION: ${SMTP_ENCRYPTION:-tls}
      ASTERISK_VOICEMAIL_SPOOL: /var/spool/asterisk/voicemail/default
      ASTERISK_VOICEMAIL_CONF_PATH: /etc/asterisk-shared/voicemail_pbxapp.conf
    volumes:
      - ../storage:/var/www/html/storage
      - asterisk_conf_shared:/etc/asterisk-shared
    networks:
      - web
      - voip_internal

  asterisk:
    build:
      context: ./asterisk
    container_name: voip_asterisk
    restart: unless-stopped
    depends_on:
      - db
    environment:
      DB_HOST: db
      DB_PORT: 3306
      DB_NAME: ${DB_NAME}
      DB_USER: ${DB_USER}
      DB_PASS: ${DB_PASSWORD}
      SIGNALWIRE_SIP_DOMAIN: ${SIGNALWIRE_SIP_DOMAIN}
      SIGNALWIRE_SIP_USERNAME: ${SIGNALWIRE_SIP_USERNAME}
      SIGNALWIRE_SIP_PASSWORD: ${SIGNALWIRE_SIP_PASSWORD}
      # Must be your VPS's actual public IP — used so Asterisk advertises the
      # correct address in SIP/SDP when running inside a container (otherwise
      # it advertises its internal Docker IP, which nothing outside can reach).
      PUBLIC_IP: ${PUBLIC_IP}
    ports:
      - "5060:5060/udp"
      - "5061:5061/tcp"
      - "10000-10500:10000-10500/udp"
    volumes:
      - ..:/var/www/pbx-app
      - asterisk_data:/var/lib/asterisk
      - asterisk_spool:/var/spool/asterisk
      - asterisk_conf_shared:/etc/asterisk-shared
    networks:
      - voip_internal

networks:
  web:
    external: true
  voip_internal:
    driver: bridge

volumes:
  db_data:
  asterisk_data:
  asterisk_spool:
  asterisk_conf_shared:
