services:
    # Main app -laravel
    app:
        &base-app
        build:
            context: .
            dockerfile: Dockerfile
            args:
                - UID=${UID:-1000}
                - GID=${GID:-1000}
        restart: unless-stopped
        tty: true
        environment:
            - SERVICE_NAME=app
            - SERVICE_TAGS=production
        volumes:
            - ./:/var/www
            - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
        networks:
            - str-network
        depends_on:
            db:
                condition: service_healthy
            redis:
                condition: service_healthy
            webserver:
                condition: service_started
            websocket:
                condition: service_started
        profiles:
            - local
            - prod

    # Laravel`s queue
    app-queue:
        <<: *base-app
        command: php artisan queue:work --tries=3
        environment:
            - SERVICE_NAME=app-queue

    # Laravel`s scheduler
    app-schedule:
        <<: *base-app
        command: php artisan schedule:work
        environment:
            SERVICE_NAME: ${COMPOSE_PROJECT_NAME}-schedule
        hostname: ${COMPOSE_PROJECT_NAME}-schedule

    # Nginx Service ----------------------------------------------------------------------------------------------------
    webserver:
        &base-nginx
        build:
            context: .
            dockerfile: docker/nginx/Dockerfile
        restart: unless-stopped
        tty: true
        command: '/bin/sh -c ''while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g "daemon off;"'''
        ports:
            - ${WEB_PORT:-80}:80
        volumes:
            - ./public:/var/frontend
            - ./:/var/www
            - ./docker/nginx/conf.d/:/etc/nginx/conf.d/
        networks:
            - str-network
        profiles:
            - local
            - prod
    # Vite Service -----------------------------------------------------------------------------------------------------
    vite:
        <<: *base-nginx
        restart: no
        command: '/bin/sh -c ''npm run dev'''
        ports:
            - 5172:5172
        profiles:
            - local

    #MySQL Service
    db:
        image: mysql:8.0
        command: --default-authentication-plugin=mysql_native_password
        restart: unless-stopped
        tty: true
        environment:
            - MYSQL_DATABASE=${DB_DATABASE}
            - MYSQL_ROOT_PASSWORD=root
            - MYSQL_USER=${DB_USERNAME}
            - MYSQL_PASSWORD=${DB_PASSWORD}
            - SERVICE_TAGS=production
            - SERVICE_NAME=mysql
            - TZ=${APP_TIMEZONE}
        healthcheck:
            test: mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD
            start_period: 5s
            interval: 5s
            timeout: 5s
            retries: 255
        volumes:
            - ./docker/mysql/my.cnf:/etc/my.cnf
            - ./docker/database:/var/lib/mysql
        networks:
            - str-network
        profiles:
            - local
            - prod

    websocket:
        build:
            context: docker/simple-ws
            dockerfile: Dockerfile
        volumes:
            - ./docker/simple-ws/server.js:/app/server.js
        restart: unless-stopped
        environment:
            - JWT_SECRET=${JWT_SECRET}
            - REDIS_PASSWORD=${REDIS_PASSWORD}
        tty: true
        command: '/bin/sh -c ''node server'''
        networks:
            - str-network
        depends_on:
            redis:
                condition: service_healthy
        profiles:
            - local
            - prod

    redis:
        image: "redis:alpine"
        restart: unless-stopped
        command: redis-server --requirepass ${REDIS_PASSWORD}
        environment:
            - REDIS_REPLICATION_MODE=master
        networks:
            - str-network
        healthcheck:
            test: redis-cli --raw --pass $$REDIS_PASSWORD incr ping
            start_period: 1s
            interval: 1s
            timeout: 3s
            retries: 255
        profiles:
            - local
            - prod

#Docker Networks
networks:
    str-network:
        driver: bridge
        name: ${COMPOSE_PROJECT_NAME}-network
