# Dockerfile para DESARROLLO
# Basado en el Dockerfile principal pero con herramientas de desarrollo

FROM php:8.2-fpm-alpine

# Instalar dependencias del sistema
RUN apk add --no-cache \
    nginx \
    supervisor \
    curl \
    vim \
    git \
    unzip \
    bash \
    mysql-client \
    redis \
    ffmpeg \
    # Dependencias para XDebug y desarrollo
    autoconf \
    g++ \
    make \
    # Librerías PHP
    libpng-dev \
    libjpeg-turbo-dev \
    freetype-dev \
    libzip-dev \
    icu-dev \
    oniguruma-dev

# Instalar extensiones PHP
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) \
        pdo \
        pdo_mysql \
        mysqli \
        mbstring \
        zip \
        gd \
        intl \
        opcache \
        exif \
        pcntl \
        sockets

# Instalar Redis extension
RUN apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && apk del .build-deps

# Instalar XDebug para debugging
RUN apk add --no-cache --virtual .build-deps-xdebug $PHPIZE_DEPS \
    && pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && apk del .build-deps-xdebug

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

# Configuración PHP para DESARROLLO
RUN { \
    echo 'display_errors = On'; \
    echo 'display_startup_errors = On'; \
    echo 'error_reporting = E_ALL'; \
    echo 'log_errors = On'; \
    echo 'memory_limit = 512M'; \
    echo 'max_execution_time = 300'; \
    echo 'upload_max_filesize = 100M'; \
    echo 'post_max_size = 100M'; \
    echo 'date.timezone = America/Bogota'; \
    # Habilitar shell_exec para conversión de audio
    echo 'disable_functions = '; \
    # Desactivar OPcache para hot-reload
    echo 'opcache.enable = 0'; \
    echo 'opcache.enable_cli = 0'; \
} > /usr/local/etc/php/conf.d/dev.ini

# Configuración XDebug
RUN { \
    echo 'xdebug.mode=debug,develop,coverage'; \
    echo 'xdebug.start_with_request=trigger'; \
    echo 'xdebug.client_host=host.docker.internal'; \
    echo 'xdebug.client_port=9003'; \
    echo 'xdebug.log=/var/www/html/logs/xdebug.log'; \
    echo 'xdebug.log_level=7'; \
} > /usr/local/etc/php/conf.d/xdebug.ini

# Configuración Nginx
COPY docker/nginx/default.conf /etc/nginx/http.d/default.conf

# Configuración PHP-FPM
RUN { \
    echo '[www]'; \
    echo 'user = www-data'; \
    echo 'group = www-data'; \
    echo 'listen = /run/php/php-fpm.sock'; \
    echo 'listen.owner = www-data'; \
    echo 'listen.group = www-data'; \
    echo 'pm = dynamic'; \
    echo 'pm.max_children = 20'; \
    echo 'pm.start_servers = 4'; \
    echo 'pm.min_spare_servers = 2'; \
    echo 'pm.max_spare_servers = 6'; \
    echo 'clear_env = no'; \
    echo 'catch_workers_output = yes'; \
    echo 'php_admin_value[error_log] = /var/www/html/logs/php-fpm-error.log'; \
} > /usr/local/etc/php-fpm.d/www.conf

# Configuración Supervisor para desarrollo (con logs más verbosos)
RUN { \
    echo '[supervisord]'; \
    echo 'nodaemon=true'; \
    echo 'logfile=/var/www/html/logs/supervisord.log'; \
    echo 'pidfile=/var/run/supervisord.pid'; \
    echo 'loglevel=debug'; \
    echo ''; \
    echo '[program:nginx]'; \
    echo 'command=/usr/sbin/nginx -g "daemon off;"'; \
    echo 'autostart=true'; \
    echo 'autorestart=true'; \
    echo 'stdout_logfile=/var/www/html/logs/nginx-stdout.log'; \
    echo 'stderr_logfile=/var/www/html/logs/nginx-stderr.log'; \
    echo 'priority=10'; \
    echo ''; \
    echo '[program:php-fpm]'; \
    echo 'command=/usr/local/sbin/php-fpm --nodaemonize'; \
    echo 'autostart=true'; \
    echo 'autorestart=true'; \
    echo 'stdout_logfile=/var/www/html/logs/php-fpm-stdout.log'; \
    echo 'stderr_logfile=/var/www/html/logs/php-fpm-stderr.log'; \
    echo 'priority=5'; \
} > /etc/supervisord.conf

# Crear directorios necesarios
RUN mkdir -p /var/www/html/logs /var/www/html/uploads /run/php \
    && chown -R www-data:www-data /var/www/html \
    && chmod -R 755 /var/www/html

WORKDIR /var/www/html

# Exponer puertos
EXPOSE 80 9003

# Script de inicio
COPY docker/entrypoint-dev.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
