FROM php:8.2-alpine

RUN apk add --no-cache \
    postgresql-dev \
    nginx \
    supervisor \
    curl \
    libpng-dev \
    libjpeg-turbo-dev \
    freetype-dev \
    oniguruma-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install pdo_pgsql pgsql mbstring gd

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

WORKDIR /app

COPY . .

RUN composer install --no-dev --optimize-autoloader --no-interaction \
    && php artisan storage:link \
    && chmod -R 775 storage bootstrap/cache \
    && chown -R www-data:www-data storage bootstrap/cache

RUN apk add --no-cache nodejs npm \
    && npm ci && npm run build \
    && rm -rf node_modules \
    && apk del nodejs npm

COPY docker/nginx.conf /etc/nginx/http.d/default.conf
COPY docker/supervisord.conf /etc/supervisor.conf

RUN echo "* * * * * php /app/artisan schedule:run >> /dev/null 2>&1" > /etc/crontabs/www-data

EXPOSE 80

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