# Stage 1: Build Stage
FROM ubuntu:22.04 AS build

# Set non-interactive mode
ENV DEBIAN_FRONTEND=noninteractive

# Install build dependencies and tools
RUN apt-get update && \
    apt-get -y install software-properties-common && \
    add-apt-repository ppa:ondrej/php && \
    apt-get update && \
    apt-get install -y apache2 mysql-client php8.1 php8.1-curl php8.1-mysql php8.1-mbstring php8.1-xml php8.1-mysqli php8.1-memcached php8.1-xmlrpc php8.1-pgsql php8.1-zip php8.1-gd php8.1-intl && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf /var/lib/apt/lists/* && \
    apt-get update && \
    apt-get install -y ssmtp mailutils && \
    apt-get install -y net-tools
# Set up the working directory
WORKDIR /var/www/html

# Remove the default index.html file
RUN rm -rf /var/www/html/index.html

# Copy the application source code
COPY ./app /var/www/html/

# Set ownership and permissions
RUN chown -R www-data:www-data /var/www/html && \
    chmod -Rf 755 /var/www/html/

# Stage 2: Runtime Stage
FROM ubuntu:22.04

# Set non-interactive mode
ENV DEBIAN_FRONTEND=noninteractive

# Install necessary runtime packages
RUN apt-get update && \
    apt-get install -y apache2 php8.1 libapache2-mod-php8.1 && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* && \
    a2enmod rewrite

# Copy necessary files from the build stage
COPY --from=build /var/www/html /var/www/html

# Copy configuration files
COPY apache2.conf /etc/apache2/apache2.conf
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf

# Configure Apache
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

# Expose ports
EXPOSE 80

# Start Apache service
CMD ["apache2ctl", "-D", "FOREGROUND"]

