services:
  db:
    networks:
      - nexxo
    image: mariadb:11
    container_name: nexxo_db
    restart: unless-stopped
    env_file: src/.env
    environment:
      MARIADB_DATABASE: ${DB_DATABASE}
      MARIADB_USER: ${DB_USERNAME}
      MARIADB_PASSWORD: ${DB_PASSWORD}
      MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    ports:
      - "3306:3306"
    volumes:
      - ./db_data:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mariadb-admin ping -h localhost -u root -p$$MARIADB_ROOT_PASSWORD || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 20
      start_period: 20s

  app:
    networks:
      - nexxo
    build:
      context: .
      dockerfile: docker/php/Dockerfile
      args:
        UID: 1000
        GID: 1000
    image: nexxo-php
    container_name: nexxo_app
    ports:
      - "5173:5173"
    restart: unless-stopped
    working_dir: /var/www/html
    volumes:
      - ./src:/var/www/html
    env_file: src/.env
    environment:
      DB_HOST: db
      REDIS_HOST: redis
      XDG_CONFIG_HOME: /tmp
      PSYSH_CONFIG_DIR: /tmp/psysh
    depends_on:
      db:
        condition: service_healthy

  web:
    networks:
      - nexxo
    image: nginx:alpine
    container_name: nexxo_web
    restart: unless-stopped
    ports:
      - "80:80"
    volumes:
      - ./src:/var/www/html:ro
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - app

  redis:
    networks:
      - nexxo
    image: redis:7-alpine
    container_name: nexxo_redis
    restart: unless-stopped
    ports:
      - "127.0.0.1:6379:6379"
    command: ["redis-server", "--appendonly", "yes"]

  mail-worker:
    networks:
      - nexxo
    image: nexxo-php
    container_name: nexxo_mail_worker
    restart: unless-stopped
    env_file: src/.env
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    command: php artisan app:process-mail-queue
    depends_on:
      db:
        condition: service_healthy

  worker:
    networks:
      - nexxo
    image: nexxo-php
    container_name: nexxo_worker
    restart: unless-stopped
    env_file: src/.env
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    command: php artisan queue:work --sleep=1 --tries=3 --timeout=120
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started

  scheduler:
    image: nexxo-php
    container_name: nexxo_scheduler
    restart: unless-stopped
    networks:
      - nexxo
    volumes:
      - ./src:/var/www/html
    env_file: src/.env
    environment:
      DB_HOST: db
      REDIS_HOST: redis
    command: >
      sh -c '
        until nc -z db 3306; do sleep 1; done;
        php artisan schedule:work
      '
    depends_on:
      - db

  reverb:
    networks:
      - nexxo
    image: nexxo-php
    container_name: nexxo_reverb
    command: php artisan reverb:start --host=0.0.0.0 --port=8080
    env_file: src/.env
    ports:
      - "8080:8080"
    restart: unless-stopped
    volumes:
      - ./src:/var/www/html
    depends_on:
      - redis

  ntfy:
    image: binwiederhier/ntfy
    container_name: nexxo_ntfy
    restart: unless-stopped
    ports:
      - "8082:80"
    volumes:
      - ./docker/ntfy/cache:/var/cache/ntfy
      - ./docker/ntfy/config:/etc/ntfy
    command:
      - serve


  horizon:
    networks:
      - nexxo
    image: nexxo-php
    container_name: nexxo_horizon
    command: php artisan horizon
    env_file: src/.env
    volumes:
      - ./src:/var/www/html
    depends_on:
      - redis

networks:
  nexxo:
    driver: bridge
