# docker-compose.yml
version: '3.8'

services:
  bridge-api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: aiva-bridge-api
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgresql://postgres:password@db:5432/postgres
      - TELNYX_API_KEY=${TELNYX_API_KEY}
      - TELNYX_ASSISTANT_ID=${TELNYX_ASSISTANT_ID}
      - AIVA_PHONE_NUMBER=${AIVA_PHONE_NUMBER:-+61 7 3130 4377}
      - API_KEY=${API_KEY}
      - BRIDGE_HOST=0.0.0.0
      - BRIDGE_PORT=8000
      - LOG_LEVEL=${LOG_LEVEL:-INFO}
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    volumes:
      - ./app:/app/app
      - ./logs:/app/logs
    command: uvicorn main:app --host 0.0.0.0 --port 8000 --workers 2
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  poller:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: aiva-poller
    environment:
      - DATABASE_URL=postgresql://postgres:password@db:5432/postgres
      - TELNYX_API_KEY=${TELNYX_API_KEY}
      - POLLER_INTERVAL=${POLLER_INTERVAL:-5}
      - LOG_LEVEL=${LOG_LEVEL:-INFO}
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./app:/app/app
      - ./logs:/app/logs
    command: python poller.py
    restart: unless-stopped

  db:
    image: postgres:15-alpine
    container_name: aiva-postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./migrations:/docker-entrypoint-initdb.d
    ports:
      - "25432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

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

  prometheus:
    image: prom/prometheus:v2.48.0
    container_name: aiva-prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
    restart: unless-stopped

volumes:
  postgres_data:
  redis_data:
  prometheus_data: