# Stage 1: Get composer from the official image
FROM composer:2 AS composer

# Stage 2: PHP with composer and extensions
FROM php:8.1.0-fpm

# Set environment variables
ARG user=appuser
ARG uid=1000

# Install required packages and PHP extensions
RUN apt-get update && apt-get install -y \
    unzip \
    curl \
    git \
    zip \
    && rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN docker-php-ext-enable mysqli

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

# Copy composer binary from the composer stage
COPY --from=composer /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /var/www/html

# Copy your app code (make sure Docker context includes ./code)
COPY . /var/www/html/

# Install PHP dependencies using Composer
RUN composer install --no-interaction --no-scripts --prefer-dist

# Optionally, change permissions if needed
RUN chown -R $user:$user /var/www/html
