services:
    # 1. Laravel Application Service (PHP-FPM)
    app:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: invengrid-app
        restart: unless-stopped
        environment:
            APP_ENV: production
            APP_DEBUG: 'false'
            DB_HOST: mysql
            REDIS_HOST: redis
            # Remember to configure DB_DATABASE, DB_USERNAME, and DB_PASSWORD in your .env
        volumes:
            - public-assets:/var/www/html/public
            - storage-data:/var/www/html/storage
        depends_on:
            mysql:
                condition: service_healthy
            redis:
                condition: service_healthy
        networks:
            - invengrid-network

    # 2. Webserver Service (Nginx)
    webserver:
        image: nginx:alpine
        container_name: invengrid-webserver
        restart: unless-stopped
        ports:
            - '80:80'
            - '443:443'
        volumes:
            - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf:ro
            - public-assets:/var/www/html/public:ro
	    - storage-data:/var/www/html/storage:ro
        depends_on:
            - app
        networks:
            - invengrid-network

    # 3. Database Service (MySQL)
    mysql:
        image: mysql:8.4
        container_name: invengrid-mysql
        restart: unless-stopped
        environment:
            MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
            MYSQL_DATABASE: ${DB_DATABASE}
            MYSQL_USER: ${DB_USERNAME}
            MYSQL_PASSWORD: ${DB_PASSWORD}
        volumes:
            - mysql-data:/var/lib/mysql
        networks:
            - invengrid-network
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
            interval: 5s

    # 4. Cache & Queue Service (Redis)
    redis:
        image: redis:alpine
        container_name: invengrid-redis
        restart: unless-stopped
        volumes:
            - redis-data:/data
        networks:
            - invengrid-network
        healthcheck:
            test: ["CMD", "redis-cli", "ping"]
            retries: 3
            timeout: 5s
            interval: 5s

networks:
    invengrid-network:
        driver: bridge

volumes:
    public-assets:
    storage-data:
    mysql-data:
    redis-data:
