services:
  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: nexumi
      POSTGRES_USER: nexumi
      POSTGRES_PASSWORD: nexumi
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - internal
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U nexumi -d nexumi"]
      interval: 5s
      timeout: 5s
      retries: 10

  redis:
    image: redis:7-alpine
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
    networks:
      - internal
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10

  bot:
    build:
      context: .
      dockerfile: apps/bot/Dockerfile
    env_file: .env
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - backups:/backups
    networks:
      - internal
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "node -e \"fetch('http://127.0.0.1:8080/health').then((r)=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))\""
        ]
      interval: 15s
      timeout: 5s
      retries: 5
      start_period: 40s

  webui:
    build:
      context: .
      dockerfile: apps/webui/Dockerfile
    env_file: .env
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - internal
      - traefik-network
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=traefik-network"
      - "traefik.http.routers.nexumi-dashboard.rule=Host(`dashboard.nexumi.de`)"
      - "traefik.http.routers.nexumi-dashboard.entrypoints=websecure"
      - "traefik.http.routers.nexumi-dashboard.tls.certresolver=letsencrypt"
      - "traefik.http.services.nexumi-dashboard.loadbalancer.server.port=3000"
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "node -e \"fetch('http://127.0.0.1:3000/api/health').then((r)=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))\""
        ]
      interval: 15s
      timeout: 5s
      retries: 5
      start_period: 30s

volumes:
  postgres_data:
  redis_data:
  backups:

networks:
  traefik-network:
    external: true
  internal:
    driver: bridge
