FROM composer:2.4 as build

FROM php:8.1-apache-buster as dev

ENV APP_ENV=dev
ENV APP_DEBUG=true
ENV COMPOSER_ALLOW_SUPERUSER=1

RUN apt-get update && apt-get install -y zip
RUN apt-get install -y libpq-dev
RUN apt install -y zlib1g-dev libpng-dev && rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-install pdo pdo_pgsql

COPY . /var/www/html/
RUN docker-php-ext-install gd
COPY --from=build /usr/bin/composer /usr/bin/composer
RUN composer update
RUN composer install --prefer-dist --no-dev --optimize-autoloader --no-interaction

COPY ./docker/apache/000-default.conf /etc/apache2/sites-available/000-default.conf
COPY ./.env.docker /var/www/html/.env

RUN php artisan config:clear && \
    php artisan route:clear && \
    php artisan cache:clear && \
    php artisan optimize && \
    chmod 777 -R /var/www/html/storage/ && \
    chown -R www-data:www-data /var/www/ && \
    a2enmod rewrite
    
RUN service apache2 restart
