FROM php:8.2-fpm-alpine

# Install system dependencies
RUN apk add --no-cache \
    git \
    curl \
    libpng-dev \
    libxml2-dev \
    zip \
    unzip \
    mariadb-client \
    redis \
    ffmpeg \
    imagemagick \
    imagemagick-dev

# Install PHP extensions
RUN docker-php-ext-install \
    pdo_mysql \
    mysqli \
    gd \
    xml \
    mbstring \
    intl \
    opcache

# Install Redis extension
RUN pecl install redis && docker-php-ext-enable redis

# Install ImageMagick extension
RUN pecl install imagick && docker-php-ext-enable imagick

# Install Xdebug for code coverage
RUN pecl install xdebug && docker-php-ext-enable xdebug

# Configure Xdebug for coverage
RUN echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

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

# Set working directory
WORKDIR /srv/easystream

# Copy composer files
COPY composer.json composer.lock* ./

# Install PHP dependencies
RUN composer install --no-scripts --no-autoloader --prefer-dist

# Copy application code
COPY . .

# Generate autoloader
RUN composer dump-autoload --optimize

# Set permissions
RUN chown -R www-data:www-data /srv/easystream \
    && chmod -R 755 /srv/easystream \
    && chmod -R 777 /srv/easystream/f_data

# Create test directories
RUN mkdir -p /srv/easystream/tests/temp \
    && mkdir -p /srv/easystream/tests/fixtures \
    && mkdir -p /srv/easystream/tests/coverage \
    && mkdir -p /srv/easystream/tests/results \
    && chown -R www-data:www-data /srv/easystream/tests

USER www-data

EXPOSE 9000

CMD ["php-fpm"]