FROM php:8.2-fpm

ARG user=laravel
ARG uid=${uid:-1000}

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libfreetype6-dev \
    libjpeg-dev \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    supervisor \
    nginx libpq-dev \
    build-essential \
    openssl \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install gd pdo pdo_mysql pdo_pgsql bcmath mbstring xml exif pcntl

RUN pecl install xdebug

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user
WORKDIR /var/www

COPY ./php.ini /usr/local/etc/php/php.ini

COPY composer.json composer.lock ./

RUN composer install --no-dev --optimize-autoloader --no-scripts


COPY . .

RUN chown -R $uid:$uid /var/www && \
    chown -R $uid:www-data /var/www/storage /var/www/bootstrap/cache && \
    chmod -R 775 /var/www/storage /var/www/bootstrap/cache

COPY ./supervisord.conf /etc/supervisord.conf
COPY ./nginx.conf /etc/nginx/sites-enabled/default

#RUN php artisan optimize
RUN php artisan storage:link

EXPOSE 80

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