FROM php:8.3-fpm

# Installa le dipendenze necessarie
RUN apt-get update && apt-get install -y \
    git \
    unzip \
    libzip-dev \
    npm \
    libcurl4-openssl-dev \
    libssl-dev \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    nginx \
    libpq-dev

# Installa Node.js 20.x
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
RUN apt-get install -y nodejs

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN docker-php-ext-install zip curl fileinfo
RUN docker-php-ext-install pgsql pdo_pgsql
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd
RUN docker-php-ext-install opcache
RUN pecl install apcu && docker-php-ext-enable apcu


RUN npm install -g yarn

WORKDIR /var/www/httpdocs

# Copia selettivamente i file e le directory necessarie
COPY assets assets/
COPY bin bin/
COPY config config/
COPY public/index.php  public/index.php
COPY src src/
COPY templates templates/
#COPY translations translations/
COPY .env .env
COPY webpack.config.js webpack.config.js
COPY composer.json composer.json
COPY composer.lock composer.lock
COPY compress_logs.bat compress_logs.bat
COPY symfony.lock symfony.lock
COPY package.json package.json
COPY yarn.lock yarn.lock
COPY opcache.ini /usr/local/etc/php/conf.d/opcache.ini
COPY custom-php.ini /usr/local/etc/php/conf.d/custom-php.ini
COPY apcu.ini /usr/local/etc/php/conf.d/apcu.ini

# Esegue composer come root per Docker è necessario
ENV COMPOSER_ALLOW_SUPERUSER=1
ENV APP_ENV=prod
ENV DATABASE_URL="postgresql://app:!ChangeMe!@database:5432/syncher?serverVersion=16&charset=utf8"

#ENV APP_ENV=dev

RUN composer install --no-interaction

RUN yarn install --force

# Esegui la build di Yarn
RUN yarn build

# Copia la configurazione di Nginx
COPY nginx.conf /etc/nginx/sites-available/default

# Imposta i permessi corretti
RUN chown -R www-data:www-data /var/www/httpdocs

# Script di avvio
COPY start.sh /start.sh
RUN chmod +x /start.sh

CMD ["/start.sh"]