# ======================
# Etapa 1: Compilacion
# ======================
FROM php:8.1-cli AS builder

# Instalar herramientas
RUN apt-get update && apt-get install -y \
    git curl unzip zip nodejs npm libzip-dev libpng-dev libjpeg-dev \
    libfreetype6-dev libonig-dev libxml2-dev

RUN docker-php-ext-install pdo pdo_mysql mbstring zip gd mysqli

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

# Copiar el codigo fuente
WORKDIR /app
COPY . .

# Instalar dependencias PHP y JS + compilar
RUN composer install --no-dev --optimize-autoloader
RUN npm install && npm run build

# ======================
# Etapa 2: Imagen final
# ======================
FROM php:8.1-apache

RUN apt-get update && apt-get install -y libzip-dev libpng-dev libjpeg-dev \
    libfreetype6-dev libonig-dev libxml2-dev && \
    docker-php-ext-install pdo pdo_mysql mbstring zip gd mysqli && \
    a2enmod rewrite

WORKDIR /var/www/html

# Copiar solo los archivos ya listos desde la etapa anterior
COPY --from=builder /app /var/www/html

# Permisos adecuados
RUN chown -R www-data:www-data /var/www/html && chmod -R 755 /var/www/html
