# Laravel Backend - CPanel-Compatible Testing Environment
# Mirrors shared hosting PHP environment for accurate testing

FROM php:8.3-apache

# Install PHP extensions common on CPanel shared hosting
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    libzip-dev \
    zip \
    unzip \
    sqlite3 \
    libsqlite3-dev \
    && docker-php-ext-install pdo_mysql pdo_sqlite mbstring exif pcntl bcmath gd zip \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Install Composer (available on most CPanel hosts)
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Configure Apache (CPanel-style configuration)
RUN a2enmod rewrite
COPY docker/apache-cpanel.conf /etc/apache2/sites-available/000-default.conf

# Set working directory
WORKDIR /app

# Copy PHP application
COPY . /app

# Set proper permissions (CPanel-style)
RUN chown -R www-data:www-data /app/storage /app/bootstrap/cache \
    && chmod -R 775 /app/storage /app/bootstrap/cache

# Install dependencies
RUN composer install --no-dev --optimize-autoloader

# Create testing database
RUN mkdir -p /app/storage/database \
    && touch /app/storage/testing.sqlite \
    && chown www-data:www-data /app/storage/testing.sqlite

# Configure PHP for shared hosting limits
COPY docker/php-cpanel.ini /usr/local/etc/php/conf.d/cpanel.ini

# Laravel optimization (production-like)
RUN php artisan config:cache || true \
    && php artisan route:cache || true \
    && php artisan view:cache || true

# Health check endpoint
COPY docker/healthcheck.php /app/public/healthcheck.php

EXPOSE 8000

# Use Apache (CPanel standard) instead of artisan serve
CMD ["apache2-foreground"]