FROM python:3.9-slim-buster as base

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=off \
    PIP_DISABLE_PIP_VERSION_CHECK=on \
    PIP_DEFAULT_TIMEOUT=100 \
    POETRY_HOME="/opt/poetry" \
    POETRY_VIRTUALENVS_IN_PROJECT=true \
    POETRY_NO_INTERACTION=1 \
    PYSETUP_PATH="/opt/pysetup" \
    VENV_PATH="/opt/pysetup/.venv"

ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"

FROM base as builder-base
ARG POETRY_OPTS

RUN apt-get update && apt-get upgrade -y
RUN apt-get install --no-install-recommends gcc build-essential wget curl nano groff less zsh jq unzip -y

RUN python -m pip install -U pip setuptools wheel poetry

# use poetry to install python requirements
WORKDIR $PYSETUP_PATH
COPY ./pyproject.toml ./

# install packages
RUN poetry install $POETRY_OPTS

# export constraints
RUN poetry export --format requirements.txt --output constraints.txt --without-hashes

# 'production' stage uses the clean 'python-base' stage and copies
# in only our runtime deps that were installed in the 'builder-base'
FROM base as production

ENV VENV_PATH="/opt/pysetup/.venv"

# create container output folders
RUN mkdir /sradeveloper &&  \
    mkdir /sradeveloper/tmp &&  \
    mkdir /sradeveloper/tmp/advisorscodebase &&  \
    mkdir /sradeveloper/logs &&  \
    mkdir /sradeveloper/logs/advisorscodebase &&  \
    mkdir /home/root &&  \
    mkdir /home/root/.aws

# copy necessary runtime files
COPY --from=builder-base $VENV_PATH $VENV_PATH
COPY --from=builder-base $PYSETUP_PATH/constraints.txt /sradeveloper/constraints.txt

# set prefect cloud defaults
RUN . $VENV_PATH/bin/activate

# set env variables
ENV JUPYTER_TOKEN=sra
ENV TZ=America/Chicago

# set env variable to use prefect cloud secrets
ENV PREFECT__CLOUD__USE_LOCAL_SECRETS=false

RUN apt-get update && apt-get upgrade -y

# install zsh
RUN apt-get install --no-install-recommends wget curl nano unzip zsh jq git -y
RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# cleanup
RUN apt-get check &&  \
    apt-get clean &&  \
    apt-get autoclean

RUN . $VENV_PATH/bin/activate
WORKDIR /sradeveloper/
