FROM debian:bookworm-slim

# Evitar interacciones durante instalación
ENV DEBIAN_FRONTEND=noninteractive

# Instalar dependencias
RUN apt-get update && apt-get install -y \
    apache2 \
    php \
    php-mysql \
    php-mbstring \
    php-gd \
    php-zip \
    php-curl \
    composer \
    supervisor \
    cron \
    curl \
    unzip \
    git \
    && rm -rf /var/lib/apt/lists/*

# Configurar Apache
RUN a2enmod rewrite
COPY apache-config.conf /etc/apache2/sites-available/000-default.conf

# Copiar proyecto
WORKDIR /var/www/html
COPY . .

# Instalar dependencias de Composer
# Ajustar permisos temporalmente para composer
RUN mkdir -p /var/www/html/vendor
RUN composer install --no-dev --optimize-autoloader

# Configurar permisos finales
RUN chown -R www-data:www-data /var/www/html \
    && chmod -R 755 /var/www/html \
    && mkdir -p /var/www/html/logs \
    && chown -R www-data:www-data /var/www/html/logs

# Configurar Supervisor
COPY supervisor.conf /etc/supervisor/conf.d/contenedor.conf

# Exponer puerto
EXPOSE 80

# Configurar Entrypoint
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

# Iniciar Supervisor
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/contenedor.conf"]
