# Use the official Debian 12.11 (Bookworm) image as the base
FROM debian:12.11

# Set environment variables to prevent interactive prompts during apt operations
ENV DEBIAN_FRONTEND=noninteractive

# Update the package list and install necessary dependencies for adding Node.js and PHP repositories
# curl is needed to download the NodeSource setup script, Bun install script, and Composer installer
# gnupg is needed to handle GPG keys for apt repositories
# ca-certificates is needed for secure connections
# apt-transport-https is needed for apt to fetch packages over HTTPS
# unzip and tar are often required for Bun's installation process
# lsb-release is needed for add-apt-repository (which is not used directly, but useful for detecting distro)
# dirmngr is needed for adding GPG keys
# wget is needed to download the PHP repository GPG key
RUN apt-get update && \
    apt-get install -y curl gnupg ca-certificates apt-transport-https unzip tar lsb-release dirmngr wget && \
    rm -rf /var/lib/apt/lists/*

# Add NodeSource GPG key for Node.js 23.x repository
# The NodeSource setup script adds the repository and imports the GPG key.
# We're specifically targeting Node.js 23.x.
RUN curl -fsSL https://deb.nodesource.com/setup_23.x | bash -

# Install Node.js and npm from the NodeSource repository
# nodejs package includes both Node.js runtime and npm (Node Package Manager)
RUN apt-get update && \
    apt-get install -y nodejs && \
    rm -rf /var/lib/apt/lists/*

# Install Bun
# This command downloads and executes the official Bun installation script.
# It installs Bun globally.
RUN curl -fsSL https://bun.sh/install | bash

# Add Bun to the PATH for non-interactive shells and subsequent commands
# The Bun installer typically adds it to ~/.bashrc or similar, but for Docker,
# we need to ensure it's in the system-wide PATH or explicitly sourced.
# This line appends the Bun binary directory to the PATH environment variable.
ENV PATH="/root/.bun/bin:$PATH"

# Add Ondrej's PHP repository for Debian 12 (Bookworm)
# This repository provides up-to-date PHP versions.
RUN echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/sury-php.list && \
    wget -qO - https://packages.sury.org/php/apt.gpg | apt-key add -

# Install PHP 8.3 and common extensions
# php8.3-cli: Command Line Interface
# php8.3-fpm: FastCGI Process Manager (for web servers like Nginx/Apache)
# php8.3-mysql: MySQL database extension
# php8.3-curl: cURL extension for making HTTP requests
# php8.3-mbstring: Multibyte string functions
# php8.3-xml: XML extension
# php8.3-zip: Zip archive extension (already present, but good to ensure)
# php8.3-gd: GD extension (for JPEG, WebP, etc.)
# php8.3-pdo: PDO (PHP Data Objects) extension
# php8.3-pgsql: PostgreSQL PDO driver
# php8.3-gmp: GNU Multiple Precision arithmetic functions
# php8.3-bcmath: Arbitrary precision mathematics
# php8.3-intl: Internationalization extension
# php8.3-redis: Redis extension
# php8.3-excimer: Excimer extension (for profiling)
# php8.3-xdebug: Xdebug extension (for debugging and profiling)
RUN apt-get update && \
    apt-get install -y php8.3 php8.3-cli php8.3-fpm php8.3-mysql php8.3-curl php8.3-mbstring php8.3-xml php8.3-zip php8.3-gd php8.3-pdo php8.3-pgsql php8.3-gmp php8.3-bcmath php8.3-intl php8.3-redis php8.3-excimer php8.3-xdebug && \
    rm -rf /var/lib/apt/lists/*

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

# Configure Xdebug
# Create a new INI file for Xdebug configuration.
# Set xdebug.mode to 'develop,debug' for development and debugging features.
# Set xdebug.start_with_request to 'yes' to always start Xdebug on every request.
# Set xdebug.client_host to 'host.docker.internal' for Docker Desktop compatibility
# This allows Xdebug to connect back to the host machine's IDE.
RUN echo "zend_extension=xdebug" > /etc/php/8.3/mods-available/xdebug.ini && \
    echo "xdebug.mode=develop,debug" >> /etc/php/8.3/mods-available/xdebug.ini && \
    echo "xdebug.start_with_request=yes" >> /etc/php/8.3/mods-available/xdebug.ini && \
    echo "xdebug.client_host=host.docker.internal" >> /etc/php/8.3/mods-available/xdebug.ini

# --- Install Docker into the image ---
# Add Docker's official GPG key
RUN install -m 0755 -d /etc/apt/keyrings && \
    curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \
    chmod a+r /etc/apt/keyrings/docker.gpg

# Add the Docker repository to Apt sources
RUN echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  tee /etc/apt/sources.list.d/docker.list > /dev/null

# Update apt package index and install Docker Engine, CLI, containerd, and Docker Compose plugin
RUN apt-get update && \
    apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin && \
    rm -rf /var/lib/apt/lists/*

# Verify Node.js, npm, Bun, PHP, Composer, and Docker installations
RUN node -v
RUN npm -v
RUN bun -v
RUN php -v
RUN composer -v

# Set a working directory (optional, but good practice for applications)
WORKDIR /app

# You can add your application code here, for example:
# COPY . /app

# Install dependencies using Bun
# This command assumes you have a package.json or bun.lockb file in your /app directory.
# If you don't have one, this command will likely fail or do nothing.
# RUN bun install

# EXPOSE 3000
# CMD ["node", "your-app.js"]

# Default command if no other command is specified when running the container
# This will keep the container running and allow you to exec into it.
CMD ["node"]
