name: mmi

services:
    mmi-php:
        build:
            context: ../../../infra/docker/php83-fpm
        volumes:
            - ${DOCUMENT_ROOT:-./www}:/var/www/html
            - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
            - ./.env.local:/var/www/html/.env.local:ro
            - ${HOME}/.ssh/id_rsa:/root/.ssh/id_rsa:ro
            - ${HOME}/.ssh/known_hosts:/root/.ssh/known_hosts:ro
        depends_on:
        - redis
        networks: [ mmi-priv, mozart-network, sail]
        extra_hosts:
            - "host.docker.internal:host-gateway"
            - "www.mozartdigital.docker:host-gateway"
            - "api.mozartdigital.docker:host-gateway"
            - "www.monmarcheimmobilier.docker:host-gateway"
        command: >
            sh -c "chmod -R 777 /var/www/html/storage && 
                   chmod -R 777 /var/www/html/bootstrap/cache && 
                   php-fpm"
    node-builder:
        image: node:20-alpine
        working_dir: /var/www/html
        volumes:
            - '.:/var/www/html'
            - ../../../infra/certs:/etc/ssl/certs:ro
        command: sh -c "npm ci && npx vite --host --port 5174"
        environment:
            SERVER_NAME_VITE: ${SERVER_NAME}
        ports:
            - "5174:5174"
        networks: [ mmi-priv, mozart-network ]
    nginx:
        image: nginx:alpine
        depends_on: [ mmi-php ]
        volumes:
            - ${DOCUMENT_ROOT:-./www}:/var/www/html
            - ./config/nginx/conf.d:/etc/nginx/conf.d
        networks: [ mmi-priv, mozart-network ]
        expose:
            - "80"
        labels:
            - traefik.enable=true
            - traefik.http.routers.mmi.rule=Host(`${SERVER_NAME}`)
            - traefik.http.routers.mmi.entrypoints=websecure
            - traefik.http.routers.mmi.tls=true
            - traefik.http.services.mmi.loadbalancer.server.port=80
            - traefik.docker.network=mozart-network
        extra_hosts:
            - "host.docker.internal:host-gateway"
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sail-redis:/data'
        networks:
            - sail
            - mozart-network
        healthcheck:
            test:
                - CMD
                - redis-cli
                - ping
            retries: 3
            timeout: 5s
    # typesense:
    #     image: 'typesense/typesense:0.25.2'
    #     ports:
    #         - '${FORWARD_TYPESENSE_PORT:-8108}:8108'
    #     environment:
    #         TYPESENSE_DATA_DIR: '${TYPESENSE_DATA_DIR:-/typesense-data}'
    #         TYPESENSE_API_KEY: '${TYPESENSE_API_KEY:-xyz}'
    #         TYPESENSE_ENABLE_CORS: '${TYPESENSE_ENABLE_CORS:-true}'
    #     volumes:
    #         - 'sail-typesense:/typesense-data'
    #     networks:
    #         - sail
    #     healthcheck:
    #         test:
    #             - CMD
    #             - wget
    #             - '--no-verbose'
    #             - '--spider'
    #             - 'http://localhost:8108/health'
    #         retries: 5
    #         timeout: 7s


    # Queue Worker Service
    horizon-worker:
        build:
            context: ../../../infra/docker/php83-fpm  # Utiliser la même image que mmi-php
        depends_on:
            - redis  # Ensure Redis is up before starting the queue worker
        environment:
            WWWUSER: '${WWWUSER}'
            APP_ENV: ${APP_ENV}
            APP_KEY: ${APP_KEY}
            APP_DEBUG: ${APP_DEBUG}
            QUEUE_CONNECTION: ${QUEUE_CONNECTION:-redis}
        volumes:
            - '.:/var/www/html'
        working_dir: /var/www/html
        networks:
            - sail
            - mozart-network
        extra_hosts:
            - "www.mozartdigital.docker:host-gateway"
        command: >
            sh -c "php artisan horizon"
    
    # Pulse Worker Service
    pulse-worker:
        build:
            context: ../../../infra/docker/php83-fpm  # Utiliser la même image que mmi-php
        depends_on:
            - redis  # Ensure Redis is up before starting the pulse worker
        environment:
            WWWUSER: '${WWWUSER}'
            APP_ENV: ${APP_ENV}
            APP_KEY: ${APP_KEY}
            APP_DEBUG: ${APP_DEBUG}
            QUEUE_CONNECTION: ${QUEUE_CONNECTION:-redis}
        volumes:
            - '.:/var/www/html'
        working_dir: /var/www/html
        networks:
            - sail
            - mozart-network
        command: >
            sh -c "php artisan pulse:check"
    # Mail service
    mailpit:
        image: 'axllent/mailpit:latest'
        ports:
            - '${FORWARD_MAILPIT_PORT:-1025}:1025'
            - '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail

networks:
    mmi-priv:
    sail:
       driver: bridge
    mozart-network:
        external: true
volumes:
    # sail-mysql:
    #     driver: local
    sail-redis:
        driver: local
    # sail-typesense:
    #     driver: local
