services:
    app:
        build:
            context: .
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP:-1000}'
        container_name: ${DOCKER_CONTAINER_NAME}_app
        working_dir: /var/www/html
        volumes:
            - .:/var/www/html
        networks:
            - devituz
        environment:
            APP_ENV: local
            WWWUSER: '${WWWUSER:-1000}'
            XDEBUG_MODE: '${XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${XDEBUG_CONFIG:-client_host=host.docker.internal}'
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        depends_on:
            mysql:
                condition: service_healthy
        restart: unless-stopped

    horizon:
        build:
            context: .
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP:-1000}'
        container_name: ${DOCKER_CONTAINER_NAME}_horizon
        command: php artisan horizon
        volumes:
            - .:/var/www/html
        networks:
            - devituz
        depends_on:
            mysql:
                condition: service_healthy
            redis:
                condition: service_healthy
        restart: unless-stopped

    scheduler:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: ${DOCKER_CONTAINER_NAME}_scheduler
        command: sh -c "while [ true ]; do php artisan schedule:run --verbose --no-interaction; sleep 60; done"
        volumes:
            - .:/var/www/html
        depends_on:
            mysql:
                condition: service_healthy
            redis:
                condition: service_healthy
        networks:
            - devituz
        restart: unless-stopped

    nginx:
        image: nginx:1.25-alpine
        container_name: ${DOCKER_CONTAINER_NAME}_nginx
        ports:
            - '${APP_PORT:-8034}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        volumes:
            - .:/var/www/html
            - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
        depends_on:
            - app
        networks:
            - devituz
        restart: unless-stopped

    mysql:
        image: mysql:8.2
        container_name: ${DOCKER_CONTAINER_NAME}_mysql
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD:-secret}'
        volumes:
            - mysql-data:/var/lib/mysql
        networks:
            - devituz
        restart: unless-stopped
        healthcheck:
            test: [ "CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -u root -p$${DB_PASSWORD} || exit 1" ]
            interval: 10s
            timeout: 5s
            retries: 5
            start_period: 30s

    phpmyadmin:
        image: phpmyadmin/phpmyadmin:5.2.2
        container_name: ${DOCKER_CONTAINER_NAME}_phpmyadmin
        environment:
            PMA_HOST: mysql
            PMA_USER: '${DB_USERNAME}'
            PMA_PASSWORD: '${DB_PASSWORD}'
            PMA_PORT: 3306
            PMA_ARBITRARY: 1
            PMA_AUTH_TYPE: cookie
        ports:
            - "4080:80"
        volumes:
            - ./phpmyadmin/config.user.inc.php:/etc/phpmyadmin/config.user.inc.php
        depends_on:
            mysql:
                condition: service_healthy
        networks:
            - devituz
        restart: unless-stopped

    redis:
        image: redis:7.2-alpine
        container_name: ${DOCKER_CONTAINER_NAME}_redis
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - redis-data:/data
        command: [
            "redis-server",
            "--appendonly", "yes",
            "--maxmemory", "256mb",
            "--maxmemory-policy", "allkeys-lru",
            "--replica-read-only", "no"
        ]
        networks:
            - devituz
        restart: unless-stopped
        deploy:
            resources:
                limits:
                    cpus: '0.5'
                    memory: 512M
        healthcheck:
            test: ["CMD", "redis-cli", "ping"]
            interval: 10s
            timeout: 5s
            retries: 5
            start_period: 10s

volumes:
    mysql-data:
    redis-data:


networks:
    devituz:
        driver: bridge
