FROM php:8.2-fpm-alpine

RUN apk add --no-cache nginx nodejs npm supervisor icu-dev \
&& docker-php-ext-install pdo pdo_mysql mysqli bcmath pcntl intl

# Configurar PHP-FPM con límites de upload
RUN echo "upload_max_filesize=50M" > /usr/local/etc/php/conf.d/uploads.ini \
    && echo "post_max_size=55M" >> /usr/local/etc/php/conf.d/uploads.ini \
    && echo "memory_limit=256M" >> /usr/local/etc/php/conf.d/uploads.ini

WORKDIR /app
COPY . .

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install --optimize-autoloader

RUN npm ci && \
    VITE_REVERB_HOST=reverb.muronegro.cloud \
    VITE_REVERB_PORT=443 \
    VITE_REVERB_SCHEME=https \
    VITE_REVERB_APP_KEY=fmoknd2swp3clsqfbdpg \
    npm run build

RUN mkdir -p storage/framework/sessions storage/framework/views storage/framework/cache storage/app/livewire-tmp storage/logs \
    && chmod -R 775 storage bootstrap/cache \
    && chown -R nobody:nginx storage bootstrap/cache

RUN rm -rf public/storage && php artisan storage:link

COPY nginx.conf /etc/nginx/nginx.conf
COPY supervisord.conf /etc/supervisord.conf
COPY php-fpm-pool.conf /usr/local/etc/php-fpm.d/www.conf

RUN mkdir -p /var/run && chmod 755 /var/run

EXPOSE 8080

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
