FROM php:8.4.8-cli

# Install system dependencies including libraries for GD and other extensions
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    libonig-dev \
    libxml2-dev \
    libzip-dev \
    libicu-dev \
    zip \
    unzip \
    procps \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean

# Configure GD extension with JPEG and FreeType support
RUN docker-php-ext-configure gd --with-freetype --with-jpeg

# Install PHP extensions for worker functionality and web features
RUN docker-php-ext-install -j$(nproc) \
    pdo_mysql \
    mbstring \
    exif \
    pcntl \
    posix \
    sockets \
    gd \
    zip \
    intl \
    opcache

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /var/www/html

# Copy application files
COPY . .

# Install dependencies (composer.lock wird automatisch erstellt falls nicht vorhanden)
# Check if composer.json exists, if not create a minimal one
RUN if [ ! -f composer.json ]; then \
        echo "Creating minimal composer.json..."; \
        echo '{\
    "name": "worker/app",\
    "description": "Worker application",\
    "type": "project",\
    "require": {\
        "php": ">=8.4"\
    },\
    "autoload": {\
        "psr-4": {\
            "App\\\\": "src/"\
        }\
    },\
    "minimum-stability": "stable",\
    "prefer-stable": true\
}' > composer.json; \
    fi && \
    composer install \
        --no-dev \
        --optimize-autoloader \
        --no-interaction || echo "Composer install skipped or failed - continuing without dependencies"

# Create startup script for permission fixing
RUN echo '#!/bin/bash\n\
set -e\n\
\n\
echo "🔧 Fixing permissions..."\n\
\n\
# Create directories if they do not exist\n\
mkdir -p /var/www/html/src/Framework/CommandBus/storage/queue\n\
mkdir -p /var/www/html/storage/logs\n\
mkdir -p /var/www/html/storage/cache\n\
\n\
# Fix permissions on mounted volumes\n\
chown -R www-data:www-data /var/www/html/storage || true\n\
chown -R www-data:www-data /var/www/html/src/Framework/CommandBus/storage || true\n\
chmod -R 775 /var/www/html/storage || true\n\
chmod -R 775 /var/www/html/src/Framework/CommandBus/storage || true\n\
\n\
echo "✅ Permissions fixed"\n\
echo "🚀 Starting worker..."\n\
\n\
# Switch to www-data user and run the worker\n\
exec gosu www-data php /var/www/html/worker.php\n' > /usr/local/bin/start-worker.sh \
    && chmod +x /usr/local/bin/start-worker.sh

# Install gosu for better user switching (alternative to su-exec for Debian)
RUN apt-get update && apt-get install -y gosu && rm -rf /var/lib/apt/lists/*

# Create necessary directories and set permissions
RUN mkdir -p \
    /var/www/html/src/Framework/CommandBus/storage/queue \
    /var/www/html/storage/logs \
    /var/www/html/storage/cache \
    && chown -R www-data:www-data /var/www/html/storage \
    && chmod -R 775 /var/www/html/storage

# Create queue storage directory with proper permissions
RUN mkdir -p /var/www/html/src/Framework/CommandBus/storage \
    && chown -R www-data:www-data /var/www/html/src/Framework/CommandBus/storage \
    && chmod -R 775 /var/www/html/src/Framework/CommandBus/storage

# Health check for the worker
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD ps aux | grep -v grep | grep "worker.php" || exit 1

# Use startup script instead of direct PHP command
CMD ["/usr/local/bin/start-worker.sh"]
