FROM php:8.3-cli

# Install cron and dependencies
RUN apt-get update && apt-get install -y cron libpng-dev libxml2-dev psmisc procps dos2unix nodejs npm xvfb chromium zip unzip \
    && docker-php-ext-install pdo_mysql mysqli sockets \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && ln -s /usr/bin/chromium /usr/bin/chromium-browser || true

WORKDIR /var/www/html_docker

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

# Copy composer files first for better caching
COPY public/composer.* ./public/

# Install dependencies (ignore platform reqs because some extensions might not be in the build env yet)
RUN cd public && composer install --no-dev --optimize-autoloader --no-scripts --ignore-platform-reqs

# Custom PHP configuration to suppress warnings
RUN echo "display_errors=Off" > $PHP_INI_DIR/conf.d/error-logging.ini && \
    echo "log_errors=On" >> $PHP_INI_DIR/conf.d/error-logging.ini && \
    echo "error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT" >> $PHP_INI_DIR/conf.d/error-logging.ini

# Copy the crontab files and entrypoint
COPY docker/cron/crontab-set.txt /var/www/html_docker/crontab-set.txt
COPY docker/cron/crontab-set-live.txt /var/www/html_docker/crontab-set-live.txt
COPY docker/cron/cron-entrypoint.sh /usr/local/bin/cron-entrypoint.sh

# Give execution rights and create log file
RUN chmod +x /usr/local/bin/cron-entrypoint.sh \
    && touch /var/log/cron.log

# Copy the application code
COPY . .

# Set permissions on files that need it (must be after COPY)
RUN chmod 777 /var/www/html_docker/public/api/trading_status.php

ENTRYPOINT ["/usr/local/bin/cron-entrypoint.sh"]
