FROM php:8.2-apache

# 1. Dependências (Inclui netcat-openbsd e redis)
RUN apt-get update && apt-get install -y \
    git curl unzip libpng-dev libonig-dev libxml2-dev libzip-dev libicu-dev zip vim nano autoconf pkg-config netcat-openbsd \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && docker-php-ext-configure intl \
    && docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip calendar intl \
    && a2enmod rewrite \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
COPY . .

# 2. Copia as correções manuais
# COPY AppServiceProvider.php app/Providers/AppServiceProvider.php
# COPY TrustProxies.php app/Http/Middleware/TrustProxies.php
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf

# 3. Pastas e Permissões
RUN mkdir -p storage/framework/sessions \
    && mkdir -p storage/framework/views \
    && mkdir -p storage/framework/cache \
    && mkdir -p bootstrap/cache \
    && chown -R www-data:www-data /var/www/html/storage \
    && chown -R www-data:www-data /var/www/html/bootstrap/cache \
    && chmod -R 775 /var/www/html/storage

# 4. Instalação do PHP
RUN composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader

# 5. Script de Entrada
COPY docker-entrypoint.sh /usr/local/bin/
# Garante quebras de linha Linux
RUN sed -i 's/\r$//' /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

EXPOSE 80
ENTRYPOINT ["docker-entrypoint.sh"]
