# Use official PHP 8.2 FPM image as base (includes CLI)
FROM php:8.2-fpm

# Set working directory for the Laravel application
WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    apt-transport-https \
    lsb-release \
    ca-certificates \
    curl \
    gnupg2 \
    wget \
    nginx \
    supervisor \
    libzip-dev \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libonig-dev \
    libxml2-dev \
    libcurl4-openssl-dev \
    unzip \
    redis-server \
    cron \
    nano \
    procps \
    net-tools \
    git \
    ffmpeg \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Install Node.js and PM2
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g pm2

# Install PHP extensions needed for Laravel
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) \
    zip \
    gd \
    mbstring \
    curl \
    xml \
    bcmath \
    pdo_mysql

# Install Imagick extension
RUN apt-get update && apt-get install -y \
    libmagickwand-dev \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
    && apt-get clean

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

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

# Set PHP configurations
RUN echo "upload_max_filesize=500M\n" \
    "post_max_size=500M\n" \
    "memory_limit=512M\n" \
    "max_execution_time=300\n" \
    "max_input_time=300\n" \
    > /usr/local/etc/php/conf.d/custom.ini

# Configure Nginx directly in Dockerfile
RUN echo "server {\n" \
    "    listen 8000;\n" \
    "    server_name localhost;\n" \
    "    root /app/public;\n" \
    "    index index.php index.html;\n" \
    "    client_max_body_size 100M;\n" \
    "    location / {\n" \
    "        try_files \$uri \$uri/ /index.php?\$query_string;\n" \
    "    }\n" \
    "    location ~ \.php\$ {\n" \
    "        fastcgi_pass 127.0.0.1:9000;\n" \
    "        fastcgi_index index.php;\n" \
    "        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n" \
    "        include fastcgi_params;\n" \
    "    }\n" \
    "    location ~ /\.ht {\n" \
    "        deny all;\n" \
    "    }\n" \
    "}" > /etc/nginx/sites-available/default

# Replace worker_processes and events block in nginx.conf
RUN sed -i 's/^worker_processes .*/worker_processes 1;/' /etc/nginx/nginx.conf && \
    sed -i '/^events {/,/}/d' /etc/nginx/nginx.conf && \
    printf "events {\n    worker_connections 1024;\n    use epoll;\n}" > /tmp/events.conf && \
    sed -i '1r /tmp/events.conf' /etc/nginx/nginx.conf && \
    rm /tmp/events.conf

# Force PHP-FPM to listen on 127.0.0.1:9000
RUN echo "[www]\nlisten = 127.0.0.1:9000" > /usr/local/etc/php-fpm.d/zz-docker.conf
# Copy the Laravel project files into the container
COPY . /app/

RUN chown -R www-data:www-data storage bootstrap/cache

# Install Laravel dependencies using Composer
RUN composer install --optimize-autoloader --no-dev --no-interaction

# Copy .env.test to .env and generate application key
COPY .env.test .env
RUN php artisan key:generate --ansi

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Configure cron job
RUN echo "* * * * * cd /app && /usr/local/bin/php artisan schedule:run >> /var/log/cron.log 2>&1" > /etc/cron.d/laravel-scheduler \
    && crontab /etc/cron.d/laravel-scheduler

# Expose port 8000 for Nginx
EXPOSE 8000

# Start services via entrypoint
CMD ["/entrypoint.sh"]
