FROM php:8.2-apache

# Enable Apache modules
RUN a2enmod rewrite

# Set working directory
WORKDIR /var/www/html

# Install dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    libzip-dev

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip

# Configure Apache
COPY docker/php/apache.conf /etc/apache2/sites-available/000-default.conf
COPY docker/php/apache-vhost.conf /etc/apache2/sites-available/multi_institution.conf
RUN a2ensite multi_institution

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

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

# Grant permissions for Apache
RUN chown -R www-data:www-data /var/www/html
RUN chmod -R 755 /var/www/html

# Expose port 80
EXPOSE 80

# Start Apache
CMD ["apache2-foreground"]