# Dockerfile para workers dedicados (sin Nginx)
FROM php:8.2-cli-alpine

# Configurar zona horaria Colombia
ENV TZ=America/Bogota

# Instalar dependencias
RUN apk add --no-cache \
    bash \
    su-exec \
    libzip \
    libzip-dev \
    icu \
    icu-dev \
    icu-libs \
    oniguruma \
    oniguruma-dev \
    mysql-client \
    redis \
    zlib-dev \
    tzdata \
    && cp /usr/share/zoneinfo/America/Bogota /etc/localtime \
    && echo "America/Bogota" > /etc/timezone

# Instalar extensiones PHP
RUN docker-php-ext-install -j$(nproc) \
    pdo \
    pdo_mysql \
    mysqli \
    zip \
    intl \
    mbstring \
    pcntl \
    opcache

# Instalar Redis extension (versión fija para cache reproducible)
RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
    && pecl install redis-6.0.2 \
    && docker-php-ext-enable redis \
    && apk del .build-deps

# Crear usuario
RUN addgroup -g 1000 -S www && \
    adduser -u 1000 -S www -G www

# Copiar Composer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# Working directory
WORKDIR /var/www/html

# Copiar código
COPY --chown=www:www . .

# Instalar dependencias
RUN if [ -f composer.json ]; then \
    composer install --no-dev --optimize-autoloader --no-interaction; \
    fi

# Dar permisos
RUN chmod +x worker.php

# Entrypoint: arregla permisos del volumen montado y ejecuta como www
COPY docker/worker-entrypoint.sh /usr/local/bin/worker-entrypoint.sh
RUN chmod +x /usr/local/bin/worker-entrypoint.sh

# Health check
HEALTHCHECK --interval=60s --timeout=5s --start-period=30s --retries=3 \
    CMD pgrep -f "worker.php" || exit 1

ENTRYPOINT ["/usr/local/bin/worker-entrypoint.sh"]
CMD ["php", "worker.php", "--daemon"]
