# Stage 1: Python Build
FROM python:3.14-slim AS python-builder
WORKDIR /app
COPY pyproject.toml README.md LICENSE ./
COPY src/ ./src/

RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get full-upgrade -y && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

RUN pip install --upgrade pip build
ARG APP_VERSION=0.0.0
ENV SETUPTOOLS_SCM_PRETEND_VERSION=${APP_VERSION}
RUN python -m build

# Stage 2: Final Python Application
FROM python:3.14-slim
WORKDIR /app
# Install system dependencies and upgrade to patch vulnerabilities
# gcc and libmariadb-dev-compat are often needed for mysqlclient/mariadb driver compilation if binaries aren't available
RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get full-upgrade -y && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends pkg-config libmariadb-dev-compat gcc libcurl4 && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Copy built wheel from builder stage
COPY --from=python-builder /app/dist/ /app/dist/

# Upgrade pip and install the package (this will pull bsm-frontend from PyPI)
RUN pip install --upgrade pip && \
    pip install --upgrade --force --no-cache-dir $(ls /app/dist/*.whl)[mysql,mariadb,postgresql] && \
    rm -rf /app/dist

# Set default environment variables
ENV HOST=0.0.0.0
ENV PORT=11325

# Expose ports
EXPOSE 11325
EXPOSE 19132/udp
EXPOSE 19133/udp

# Copy entrypoint script
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

# Entrypoint and Command to run the application
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["start"]
