# -------- Stage 1: Builder (Composer Dependencies) --------
  FROM composer:2 AS vendor

  WORKDIR /app
  
  COPY composer.json composer.lock ./
  
  RUN composer install --no-dev --optimize-autoloader --no-scripts --no-progress
  
  # -------- Stage 2: Runtime (Alpine + PHP-FPM + Nginx + Supervisor) --------
  FROM alpine:3.22
  
  COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
  
  # Install system and PHP packages, inkl. SQLite & PostgreSQL
  RUN apk add --no-cache \
      nginx supervisor gettext bash curl \
      php82 php82-fpm php82-opcache php82-mbstring php82-xml php82-curl php82-zip \
      php82-ctype php82-session php82-pdo php82-pdo_mysql php82-tokenizer php82-dom \
      php82-fileinfo php82-gd php82-intl php82-simplexml php82-phar php82-bcmath \
      php82-pdo_sqlite php82-sqlite3 php82-pgsql php82-pdo_pgsql \
      nodejs npm
  
  # Provide php alias
  RUN ln -sf /usr/bin/php82 /usr/bin/php
  
  # Add custom PHP logging configuration
  COPY deploy/php-fpm/zz-logging.ini /etc/php82/conf.d/

  # Create user, dirs, and sockets
  RUN mkdir -p /run/nginx /run/php /var/www/html /var/log/supervisor
  
  # Configure PHP-FPM socket and environment variables
  RUN sed -i 's|^listen = 127\\.0\\.0\\.1:9000|listen = /run/php/php-fpm.sock|' /etc/php82/php-fpm.d/www.conf \
      && sed -i 's|^;listen.owner = nobody|listen.owner = nginx|' /etc/php82/php-fpm.d/www.conf \
      && sed -i 's|^;listen.group = nobody|listen.group = nginx|' /etc/php82/php-fpm.d/www.conf \
      && sed -i 's|^;listen.mode = 0660|listen.mode = 0660|' /etc/php82/php-fpm.d/www.conf \
      && sed -i 's|^user = nobody|user = nginx|' /etc/php82/php-fpm.d/www.conf \
      && sed -i 's|^group = nobody|group = nginx|' /etc/php82/php-fpm.d/www.conf \
      && sed -i 's|^;clear_env = no|clear_env = no|' /etc/php82/php-fpm.d/www.conf
  
  WORKDIR /var/www/html
  
  COPY --from=vendor /app/vendor ./vendor
  COPY . ./
  
  COPY deploy/nginx/nginx.conf /etc/nginx/nginx.conf
  COPY deploy/supervisord.conf /etc/supervisor/supervisord.conf
  COPY deploy/nginx/nginx.default.conf.template /etc/nginx/templates/default.conf.template
  COPY deploy/nginx/.htpasswd /etc/nginx/.htpasswd
  COPY deploy/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
  
  RUN chmod +x /usr/local/bin/docker-entrypoint.sh
  
  # Permissions for Laravel writable directories
  RUN mkdir -p storage bootstrap/cache \
      && chown -R nginx:nginx storage bootstrap/cache \
      && chmod -R ug+rwX storage bootstrap/cache 

# Create Nginx error log file with correct permissions
RUN mkdir -p /var/log/nginx \
    && touch /var/log/nginx/error.log \
    && chown -R nginx:nginx /var/log/nginx \
    && chown -R nginx:nginx /var/log/php82 \
    && chmod -R 664 /var/log/nginx/error.log      
  # Build frontend assets
  RUN npm ci && npm run build

  # Optimize Laravel configuration (config:cache removed - will be done at runtime with correct env vars)
  RUN php artisan route:cache && php artisan view:cache && php artisan event:cache
  
  # Entferne .env Datei, um sensible Daten nicht im finalen Image zu behalten
    RUN rm -f /var/www/html/.env
    RUN rm -f /var/www/html/env-config.env

  EXPOSE 80
  ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
  