# syntax=docker/dockerfile:1

FROM composer:2 AS vendor
WORKDIR /app
COPY composer.json composer.lock* ./
RUN if [ -f composer.lock ]; then composer install --no-dev --no-scripts --prefer-dist --no-interaction --ignore-platform-reqs; \
    else composer update --no-dev --no-scripts --prefer-dist --no-interaction --ignore-platform-reqs; fi
COPY . .
RUN composer dump-autoload --classmap-authoritative --no-dev \
    && composer run-script --no-dev post-install-cmd || true

FROM php:8.4-fpm-alpine AS runtime

WORKDIR /app

RUN apk add --no-cache \
        tzdata postgresql-dev postgresql-client libpq libzip-dev \
        libjpeg-turbo-dev freetype-dev libwebp-dev libpng-dev icu-dev \
        oniguruma-dev bash autoconf g++ make \
    && docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \
    && docker-php-ext-install -j$(nproc) \
        zip pdo pdo_pgsql pdo_mysql gd intl opcache \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && rm -rf /tmp/pear /var/cache/apk/*

ENV TZ=Europe/Moscow
RUN cp /usr/share/zoneinfo/$TZ /etc/localtime && echo "$TZ" > /etc/timezone

COPY docker/fpm-pool.conf /usr/local/etc/php-fpm.d/zz-docker.conf
COPY --from=vendor /app /app

RUN mkdir -p var/cache var/log public/uploads public/banners \
    && chown -R www-data:www-data var public/uploads public/banners \
    && chmod -R 775 var

ENV APP_ENV=prod APP_DEBUG=0
USER www-data
RUN APP_SECRET=build-placeholder \
    DATABASE_URL="postgresql://build:build@127.0.0.1:5432/build" \
    DATABASE_BITRIX_URL="mysql://build:build@127.0.0.1:3306/build" \
    REDIS_URL="redis://127.0.0.1:6379" \
    TRUSTED_PROXIES="127.0.0.1" \
    CORS_ALLOW_ORIGIN="^https?://.*$" \
    TECH_MAINTENANCE=false \
    php bin/console cache:warmup --env=prod || true

EXPOSE 9000
CMD ["php-fpm", "-F"]
