FROM hhvm/hhvm:latest

# Set working directory
WORKDIR /var/www/html

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    supervisor \
    && rm -rf /var/lib/apt/lists/*

# Install and configure PHP extensions (for compatibility with PHP libraries)
RUN docker-php-ext-install \
    pdo \
    pdo_mysql \
    gd \
    mbstring \
    xml \
    zip

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

# Copy application files
COPY . /var/www/html

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

# Make sure scripts are executable
RUN chmod +x /var/www/html/docker-start.sh

# Expose port 18000 as specified in AGENTS.md for qwen/hack
EXPOSE 18000

# Use dumb-init to handle signals properly for k8s
RUN set -eux; \
    wget -O /usr/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.5/dumb-init_1.2.5_x86_64; \
    chmod +x /usr/bin/dumb-init

# Start the application
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["hhvm", "-m", "server", "-p", "18000", "--document-root", "/var/www/html/public"]

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD curl -f http://localhost:18000/ || exit 1