# Dockerfile for extension compatibility testing.
#
# Builds PHP with --enable-embed and installs ALL available extensions.
# Then runs the compatibility test suite.
#
# Usage:
#   docker build -f crates/folk-runtime-embed/compat/Dockerfile \
#     -t folk-embed-compat .
#   docker run --rm folk-embed-compat

# ── Stage 1: Build PHP with embed SAPI + all extensions ──────────
FROM debian:bookworm-slim AS php-builder

RUN apt-get update && apt-get install -y \
    build-essential autoconf bison re2c pkg-config \
    libxml2-dev libsqlite3-dev libonig-dev libcurl4-openssl-dev \
    libssl-dev zlib1g-dev libreadline-dev libzip-dev libpng-dev \
    libjpeg-dev libfreetype-dev libwebp-dev libavif-dev \
    libgmp-dev libldap2-dev libldb-dev libsasl2-dev \
    libpq-dev default-libmysqlclient-dev libsodium-dev \
    libtidy-dev libxslt1-dev libffi-dev libbz2-dev \
    libsnmp-dev libgd-dev libenchant-2-dev libargon2-dev \
    unixodbc-dev firebird-dev libpspell-dev \
    curl git \
    && rm -rf /var/lib/apt/lists/*

ARG PHP_VERSION=8.3.15
RUN curl -fSL "https://www.php.net/distributions/php-${PHP_VERSION}.tar.xz" -o /tmp/php.tar.xz \
    && mkdir -p /usr/src/php \
    && tar xf /tmp/php.tar.xz -C /usr/src/php --strip-components=1 \
    && rm /tmp/php.tar.xz

WORKDIR /usr/src/php

# Build with maximum extensions enabled
RUN ./configure \
    --enable-embed=shared \
    --enable-opcache \
    --enable-mbstring \
    --enable-pcntl \
    --enable-sockets \
    --enable-bcmath \
    --enable-calendar \
    --enable-ctype \
    --enable-dom \
    --enable-exif \
    --enable-ftp \
    --enable-gd --with-jpeg --with-freetype --with-webp \
    --enable-intl \
    --enable-mysqlnd \
    --enable-soap \
    --enable-shmop \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-sysvmsg \
    --with-bz2 \
    --with-curl \
    --with-ffi \
    --with-gettext \
    --with-gmp \
    --with-ldap \
    --with-openssl \
    --with-pdo-mysql \
    --with-pdo-pgsql \
    --with-pgsql \
    --with-readline \
    --with-sodium \
    --with-tidy \
    --with-xsl \
    --with-zip \
    --with-zlib \
    --with-pdo-sqlite \
    --with-config-file-path=/etc/php \
    --with-config-file-scan-dir=/etc/php/conf.d \
    --disable-cgi \
    --disable-phpdbg \
    --prefix=/usr/local \
    && make -j"$(nproc)" \
    && make install \
    && mkdir -p /etc/php/conf.d

# Install PECL extensions
RUN pecl install redis msgpack igbinary apcu mongodb yaml uuid xhprof \
    && for ext in redis msgpack igbinary apcu mongodb yaml uuid xhprof; do \
        echo "extension=$ext.so" > /etc/php/conf.d/50-$ext.ini; \
    done

# Verify embed SAPI
RUN test -f /usr/local/lib/libphp.so
RUN php-config --php-sapis | grep -q embed

# ── Stage 2: Test runner ─────────────────────────────────────────
FROM debian:bookworm-slim

RUN apt-get update && apt-get install -y \
    build-essential pkg-config curl \
    libxml2 libsqlite3-0 libonig5 libcurl4 libssl3 zlib1g \
    libreadline8 libzip4 libpng16-16 libjpeg62-turbo libfreetype6 \
    libwebp7 libgmp10 libldap-2.5-0 libpq5 default-mysql-client \
    libsodium23 libtidy5deb1 libxslt1.1 libffi8 libbz2-1.0 \
    libsnmp40 libgd3 libargon2-1 libenchant-2-2 \
    unixodbc firebird3.0-utils libpspell-dev \
    && rm -rf /var/lib/apt/lists/*

COPY --from=php-builder /usr/local /usr/local
COPY --from=php-builder /etc/php /etc/php
RUN ldconfig

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain 1.85.0
ENV PATH="/root/.cargo/bin:${PATH}"

# Copy workspace
WORKDIR /src
COPY . .

# Copy test script
COPY crates/folk-runtime-embed/compat/test_extensions.php /src/test_extensions.php

# Build embed crate
RUN cargo build -p folk-runtime-embed 2>&1

# Run compatibility tests
CMD ["cargo", "test", "-p", "folk-runtime-embed", "--test", "extension_compat", "--", "--test-threads=1", "--nocapture"]
