# Define the base image dynamically using build argument
ARG DISTRO=ubuntu:latest
FROM ${DISTRO} AS base

# Install dependencies based on the distribution
RUN \
    # For Debian/Ubuntu
    if [ -f /etc/debian_version ]; then \
        apt-get update && \
        apt-get install -y python3 python3-pip python3-dev build-essential clang libclang-rt-dev git flake8 pylint python3-pytest python3-pexpect python3-setuptools python3-pyparsing vim procps sudo && \
        apt-get clean; \
        groupadd -f testuser; \
        useradd -m -d /home/testuser -s /bin/bash -g testuser testuser; \
        echo 'root:password' | chpasswd; \
        echo 'testuser:password' | chpasswd; \
        echo 'testuser ALL=(ALL:ALL) ALL' > /etc/sudoers.d/testuser && chmod 0440 /etc/sudoers.d/testuser; \
    # For Fedora
    elif [ -f /etc/fedora-release ]; then \
        dnf install -y python3 python3-pip python3-pytest git flake8 pylint python3-pexpect python3-setuptools python3-pyparsing vim sudo; \
        groupadd -f testuser; \
        useradd -m -d /home/testuser -s /bin/bash -g testuser testuser; \
        echo 'root:password' | chpasswd; \
        echo 'testuser:password' | chpasswd; \
        echo 'testuser ALL=(ALL:ALL) ALL' > /etc/sudoers.d/testuser && chmod 0440 /etc/sudoers.d/testuser; \
    # For CentOS
    elif [ -f /etc/centos-release ]; then \
        # Update CentOS repository to use vault.centos.org
        sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-* && \
        sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-* && \
        yum install -y python3 python3-pip python3-pytest git vim sudo && \
        yum install -y python3-devel gcc && \
        python3 -m pip install flake8 pylint pexpect setuptools pyparsing; \
        yum clean all; \
        groupadd -f testuser; \
        useradd -m -d /home/testuser -s /bin/bash -g testuser testuser; \
        echo 'root:password' | chpasswd; \
        echo 'testuser:password' | chpasswd; \
        echo 'testuser ALL=(ALL:ALL) ALL' > /etc/sudoers.d/testuser && chmod 0440 /etc/sudoers.d/testuser; \
    fi

# Set permissions for the user to access /app
RUN mkdir /home/testuser/lshell && chown -R testuser:testuser /home/testuser/lshell

# Set working directory to /home/testuser
WORKDIR /home/testuser/lshell

# Set PYTHONPATH to the current working directory
ENV PYTHONPATH=/home/testuser/lshell

# Copy the code and requirements
COPY . /home/testuser/lshell

# Install test/runtime Python dependencies from the repository requirements.
# Debian/Ubuntu images may require --break-system-packages (PEP 668).
RUN python3 -m pip install --no-cache-dir -r /home/testuser/lshell/requirements.txt \
    || python3 -m pip install --break-system-packages --no-cache-dir -r /home/testuser/lshell/requirements.txt

# Install lshell from source via PEP 517/pyproject.toml.
# Debian/Ubuntu images may require --break-system-packages (PEP 668).
RUN python3 -m pip install --no-cache-dir --no-deps --no-build-isolation /home/testuser/lshell \
    || python3 -m pip install --break-system-packages --no-cache-dir --no-deps --no-build-isolation /home/testuser/lshell

# Switch to `testuser`
USER testuser

# Entry point for interactive lshell (overridden in Docker Compose for tests)
CMD ["lshell"]
