networks:
  default:
    driver: bridge
  postgres_network:
    driver: bridge
  core_network:
    driver: bridge
  redis_network:
    driver: bridge
  traefik:
    external: true

services:
  hub:
    restart: unless-stopped
    build:
      context: .
      dockerfile: ./apps/hub/Dockerfile
      args:
        - NEXT_PUBLIC_HUB_URL=$NEXT_PUBLIC_HUB_URL
        - NEXT_PUBLIC_HUB_SERVER_URL=$NEXT_PUBLIC_HUB_SERVER_URL
        - NEXT_PUBLIC_DISCORD_URL=$NEXT_PUBLIC_DISCORD_URL
        - NEXT_PUBLIC_MOODLE_URL=$NEXT_PUBLIC_MOODLE_URL
        - NEXT_PUBLIC_DISPATCH_URL=$NEXT_PUBLIC_DISPATCH_URL
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.hub.rule=Host(`hub.virtualairrescue.com`)"
      - "traefik.http.routers.hub.entrypoints=websecure"
      - "traefik.http.routers.hub.tls.certresolver=le"
      - "traefik.http.services.hub.loadbalancer.server.port=3000"
    environment:
      - NEXTAUTH_URL=${AUTH_HUB_URL}
      - NEXTAUTH_SECRET=${AUTH_HUB_SECRET}
    env_file:
      - .env.prod
    networks:
      - core_network
      - postgres_network
      - traefik

  hub-server:
    build:
      context: .
      dockerfile: ./apps/hub-server/Dockerfile
    container_name: hub-server
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.hub-server.rule=Host(`api.hub.virtualairrescue.com`)"
      - "traefik.http.routers.hub-server.entrypoints=websecure"
      - "traefik.http.routers.hub-server.tls.certresolver=le"
      - "traefik.http.services.hub-server.loadbalancer.server.port=3000"

    env_file:
      - .env.prod
    networks:
      - core_network
      - postgres_network
      - traefik
    depends_on:
      postgres:
        condition: service_healthy

  # Dispatch Service
  dispatch:
    restart: unless-stopped
    build:
      context: .
      dockerfile: ./apps/dispatch/Dockerfile
      args:
        - NEXT_PUBLIC_DISPATCH_URL=$NEXT_PUBLIC_DISPATCH_URL
        - NEXT_PUBLIC_HUB_URL=$NEXT_PUBLIC_HUB_URL
        - NEXT_PUBLIC_DISPATCH_SERVICE_ID=1
        - NEXT_PUBLIC_LIVEKIT_URL=$NEXT_PUBLIC_LIVEKIT_URL
        - NEXT_PUBLIC_DISPATCH_SERVER_URL=$NEXT_PUBLIC_DISPATCH_SERVER_URL
    env_file:
      - .env.prod
    deploy:
      replicas: 2
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.dispatch.rule=Host(`dispatch.virtualairrescue.com`)"
      - "traefik.http.routers.dispatch.entrypoints=websecure"
      - "traefik.http.routers.dispatch.tls.certresolver=le"
      - "traefik.http.services.dispatch.loadbalancer.server.port=3000"

    environment:
      - NEXTAUTH_URL=${AUTH_DISPATCH_URL}
      - NEXTAUTH_SECRET=${AUTH_DISPATCH_SECRET}

    networks:
      - postgres_network
      - traefik
  dispatch-server:
    restart: unless-stopped
    build:
      context: .
      dockerfile: ./apps/dispatch-server/Dockerfile
    env_file:
      - .env.prod
    deploy:
      replicas: 3
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.dispatch-server.rule=Host(`api.dispatch.virtualairrescue.com`)"
      - "traefik.http.routers.dispatch-server.entrypoints=websecure"
      - "traefik.http.routers.dispatch-server.tls.certresolver=le"
      - "traefik.http.services.dispatch-server.loadBalancer.sticky.cookie.name=server_id"
      - "traefik.http.services.dispatch-server.loadBalancer.sticky.cookie.httpOnly=true"
      - "traefik.http.services.dispatch-server.loadbalancer.server.port=3000"
    networks:
      - core_network
      - postgres_network
      - redis_network
      - traefik
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
  cadvisor:
    restart: unless-stopped
    image: gcr.io/cadvisor/cadvisor:latest
    container_name: cadvisor
    privileged: true
    ports:
      - "8090:8080"
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
    networks:
      - core_network
  prometheus:
    restart: unless-stopped
    image: prom/prometheus:latest
    container_name: prometheus
    ports:
      - "9090:9090"
    volumes:
      - "./packages/prometheus/prometheus.prod.yml:/etc/prometheus/prometheus.yml"
      - /var/run/docker.sock:/var/run/docker.sock
      - prometheus_data:/prometheus
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"
      - "--web.enable-remote-write-receiver"
    networks:
      - traefik
      - core_network

  core-server:
    restart: unless-stopped
    build:
      context: .
      dockerfile: ./apps/core-server/Dockerfile
    env_file:
      - .env.prod
    deploy:
      replicas: 1
    labels:
      - "traefik.enable=false"
    networks:
      - postgres_network
      - core_network

  docs:
    restart: unless-stopped
    build:
      context: .
      dockerfile: ./apps/docs/Dockerfile
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.docs.rule=Host(`docs.virtualairrescue.com`)"
      - "traefik.http.routers.docs.entrypoints=websecure"
      - "traefik.http.routers.docs.tls.certresolver=le"
      - "traefik.http.services.docs.loadbalancer.server.port=80"

    networks:
      - traefik

  postgres:
    restart: unless-stopped
    image: postgres:13
    container_name: postgres
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U persistant-data -d var"]
      interval: 10s
      timeout: 5s
      retries: 3

    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: persistant-data
      POSTGRES_PASSWORD: persistant-data-pw
      POSTGRES_DB: var
    volumes:
      - postgres-data:/var/lib/postgresql/data
    networks:
      - postgres_network
      - traefik

  redis:
    restart: unless-stopped
    container_name: redis
    image: redis/redis-stack:latest
    ports:
      - "6379:6379"
    volumes:
      - "redis_data:/data"
    networks:
      - redis_network
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]

  livekit:
    image: livekit/livekit-server
    command: --config /etc/livekit.yaml --node-ip 148.251.0.86
    restart: unless-stopped
    networks:
      - traefik
      - core_network
    ports:
      - "7881:7881"
      - "7882:7882/udp"
    depends_on:
      - redis
    volumes:
      - ./packages/livekit/livekit.yaml:/etc/livekit.yaml
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.livekit.rule=Host(`livekit.virtualairrescue.com`)"
      - "traefik.http.routers.livekit.entrypoints=websecure"
      - "traefik.http.routers.livekit.tls=true"
      - "traefik.http.routers.livekit.tls.certresolver=le"
      - "traefik.http.routers.livekit.service=livekit"
      - "traefik.http.services.livekit.loadbalancer.server.port=7880"

volumes:
  postgres-data:
  redis_data:
    driver: local
  portainer_data:
  prometheus_data:
