FROM ubuntu:24.04

ENV TZ=Asia/Seoul
ENV PYTHONUNBUFFERED=1
ENV DEBIAN_FRONTEND=noninteractive

# ========================================
# 1. Base packages & timezone setup
# ========================================
RUN apt-get update && \
    apt-get install -y --no-install-recommends apt-utils && \
    apt-get install -yq tzdata && \
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
    apt-get install -y \
        curl wget git tar gnupg2 lsb-release lz4 zstd vim \
        build-essential zlib1g-dev libncurses5-dev libgdbm-dev \
        libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev \
        python3-dev pkg-config ca-certificates

# ========================================
# 2. Python 3.13 build & install
# ========================================
RUN cd /usr/src && \
    wget https://www.python.org/ftp/python/3.13.11/Python-3.13.11.tar.xz && \
    tar -xf Python-3.13.11.tar.xz && \
    cd Python-3.13.11 && \
    ./configure --enable-optimizations && \
    make altinstall && \
    rm -rf /usr/src/Python-3.13.11 /usr/src/Python-3.13.11.tar.xz

# ========================================
# 3. Python symlinks
# ========================================
RUN rm -f /usr/bin/python /usr/bin/python3 && \
    ln -s /usr/local/bin/python3.13 /usr/bin/python && \
    ln -s /usr/local/bin/python3.13 /usr/bin/python3 && \
    ln -s /usr/local/bin/python3.13 /usr/local/bin/python && \
    ln -s /usr/local/bin/python3.13 /usr/local/bin/python3 && \
    ln -s /usr/local/bin/pip3.13 /usr/bin/pip && \
    ln -s /usr/local/bin/pip3.13 /usr/bin/pip3 && \
    ln -s /usr/local/bin/pip3.13 /usr/local/bin/pip && \
    ln -s /usr/local/bin/pip3.13 /usr/local/bin/pip3

# ========================================
# 4. Python packages
# ========================================
RUN pip install --upgrade pip && \
    pip install wheel && \
    pip install sqlalchemy alembic pydantic && \
    pip install gunicorn uvicorn[standard] && \
    pip install fastapi uv poetry

# ========================================
# 5. Cleanup
# ========================================
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
    update-ca-certificates
