# Vereego Docker Compose Configuration
# Production-ready multi-container setup

services:
    # ==========================================================================
    # Laravel PHP-FPM Application
    # ==========================================================================
    app:
        build:
            context: .
            dockerfile: docker/php/Dockerfile
        container_name: vereego-app
        restart: unless-stopped
        working_dir: /var/www/html
        environment:
            - APP_ENV=${APP_ENV:-production}
            - APP_DEBUG=${APP_DEBUG:-false}
            - APP_KEY=${APP_KEY}
            - APP_URL=${APP_URL:-http://localhost}
            - DB_CONNECTION=pgsql
            - DB_HOST=db
            - DB_PORT=5432
            - DB_DATABASE=${DB_DATABASE:-vereego}
            - DB_USERNAME=${DB_USERNAME:-vereego}
            - DB_PASSWORD=${DB_PASSWORD:-secret}
            - REDIS_HOST=redis
            - REDIS_PORT=6379
            - CACHE_STORE=redis
            - SESSION_DRIVER=redis
            - QUEUE_CONNECTION=redis
            - AUTO_MIGRATE=${AUTO_MIGRATE:-false}
        volumes:
            - app-storage:/var/www/html/storage
            - app-cache:/var/www/html/bootstrap/cache
        depends_on:
            db:
                condition: service_healthy
            redis:
                condition: service_healthy
        networks:
            - vereego-network
        healthcheck:
            test: ["CMD-SHELL", "php-fpm -t || exit 1"]
            interval: 30s
            timeout: 10s
            retries: 3
            start_period: 60s

    # ==========================================================================
    # Nginx Web Server
    # ==========================================================================
    nginx:
        build:
            context: .
            dockerfile: docker/nginx/Dockerfile
        container_name: vereego-nginx
        restart: unless-stopped
        ports:
            - "${APP_PORT:-8000}:80"
            - "${APP_SSL_PORT:-8443}:443"
        volumes:
            - app-storage:/var/www/html/storage:ro
        depends_on:
            - app
        networks:
            - vereego-network
        healthcheck:
            test:
                [
                    "CMD",
                    "wget",
                    "--no-verbose",
                    "--tries=1",
                    "--spider",
                    "http://localhost/health",
                ]
            interval: 30s
            timeout: 10s
            retries: 3
            start_period: 10s

    # ==========================================================================
    # PostgreSQL Database
    # ==========================================================================
    db:
        image: postgres:16-alpine
        container_name: vereego-db
        restart: unless-stopped
        environment:
            - POSTGRES_DB=${DB_DATABASE:-vereego}
            - POSTGRES_USER=${DB_USERNAME:-vereego}
            - POSTGRES_PASSWORD=${DB_PASSWORD:-secret}
        volumes:
            - db-data:/var/lib/postgresql/data
        ports:
            - "${DB_PORT:-5432}:5432"
        networks:
            - vereego-network
        healthcheck:
            test:
                [
                    "CMD-SHELL",
                    "pg_isready -U ${DB_USERNAME:-vereego} -d ${DB_DATABASE:-vereego}",
                ]
            interval: 10s
            timeout: 5s
            retries: 5
            start_period: 30s

    # ==========================================================================
    # Redis Cache & Queue
    # ==========================================================================
    redis:
        image: redis:7-alpine
        container_name: vereego-redis
        restart: unless-stopped
        command: redis-server --appendonly yes --maxmemory 128mb --maxmemory-policy allkeys-lru
        volumes:
            - redis-data:/data
        ports:
            - "${REDIS_PORT:-6379}:6379"
        networks:
            - vereego-network
        healthcheck:
            test: ["CMD", "redis-cli", "ping"]
            interval: 10s
            timeout: 5s
            retries: 5
            start_period: 10s

    # ==========================================================================
    # Queue Worker (Laravel Horizon alternative)
    # ==========================================================================
    queue:
        build:
            context: .
            dockerfile: docker/php/Dockerfile
        container_name: vereego-queue
        restart: unless-stopped
        working_dir: /var/www/html
        command: php artisan queue:work redis --sleep=3 --tries=3 --max-time=3600
        environment:
            - APP_ENV=${APP_ENV:-production}
            - APP_DEBUG=${APP_DEBUG:-false}
            - APP_KEY=${APP_KEY}
            - DB_CONNECTION=pgsql
            - DB_HOST=db
            - DB_PORT=5432
            - DB_DATABASE=${DB_DATABASE:-vereego}
            - DB_USERNAME=${DB_USERNAME:-vereego}
            - DB_PASSWORD=${DB_PASSWORD:-secret}
            - REDIS_HOST=redis
            - REDIS_PORT=6379
            - QUEUE_CONNECTION=redis
        volumes:
            - app-storage:/var/www/html/storage
        depends_on:
            db:
                condition: service_healthy
            redis:
                condition: service_healthy
            app:
                condition: service_healthy
        networks:
            - vereego-network

    # ==========================================================================
    # Scheduler (Laravel Task Scheduling)
    # ==========================================================================
    scheduler:
        build:
            context: .
            dockerfile: docker/php/Dockerfile
        container_name: vereego-scheduler
        restart: unless-stopped
        working_dir: /var/www/html
        command: sh -c "while true; do php artisan schedule:run --verbose --no-interaction; sleep 60; done"
        environment:
            - APP_ENV=${APP_ENV:-production}
            - APP_DEBUG=${APP_DEBUG:-false}
            - APP_KEY=${APP_KEY}
            - DB_CONNECTION=pgsql
            - DB_HOST=db
            - DB_PORT=5432
            - DB_DATABASE=${DB_DATABASE:-vereego}
            - DB_USERNAME=${DB_USERNAME:-vereego}
            - DB_PASSWORD=${DB_PASSWORD:-secret}
            - REDIS_HOST=redis
            - REDIS_PORT=6379
        volumes:
            - app-storage:/var/www/html/storage
        depends_on:
            db:
                condition: service_healthy
            redis:
                condition: service_healthy
            app:
                condition: service_healthy
        networks:
            - vereego-network

# =============================================================================
# Networks
# =============================================================================
networks:
    vereego-network:
        driver: bridge
        name: vereego-network

# =============================================================================
# Volumes
# =============================================================================
volumes:
    db-data:
        name: vereego-db-data
    redis-data:
        name: vereego-redis-data
    app-storage:
        name: vereego-app-storage
    app-cache:
        name: vereego-app-cache
