ARG        AWS_ACCOUNT=086679231553

#####
# target: backend
FROM       ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python311 AS backend

USER       root
RUN        apt update && \
           apt install build-essential -y && \
           apt clean && \
           rm -rf /var/lib/apt/lists/*

USER       worker
# Install pipenv
ENV        PATH="/home/worker/.local/bin:${PATH}"
RUN        python -m pip install pipenv --user

# Set working directory
WORKDIR    /var/app

# Create and install virtualenv
ADD        Pipfile ./
ADD        Pipfile.lock ./
# Tell pipenv to create venv in the current directory
ENV        PIPENV_VENV_IN_PROJECT=1
ARG        IS_DEV=false
RUN        if [ "$IS_DEV" = "true" ] ; then \
             pipenv  sync --dev ; \
           else \
             pipenv  sync ; \
           fi

# add src files
COPY       ./*.py /var/app/
COPY       ./feed_status/ /var/app/feed_status/

#####
# target: backend-dev
FROM       backend AS backend-dev

# add dev-related files
COPY       ./.flake8 /var/app/.flake8
COPY       ./tests/ /var/app/tests/
COPY       ./Makefile /var/app/

CMD        pipenv run python dev.py
EXPOSE     5000

#####
# target: frontend
# TODO: upgrade node
FROM       node:12 AS frontend

WORKDIR    /var/app

# Copy application to the container
ADD        ./frontend/ ./frontend/

# Build Monty frontend and update favicon
RUN        mkdir -p feed_status/{static,templates}
WORKDIR    /var/app/frontend

RUN        yarn install
RUN        yarn build --no-clean
RUN        sed -i 's/favicon\.ico/favicon\.png/' /var/app/feed_status/templates/index.html
RUN        sed -i 's/favicon\.ico/favicon\.png/' /var/app/feed_status/templates/delphi.html

EXPOSE     8080

#####
# target: frontend-streamlit
# Production-ready Streamlit UI source bundle. Streamlit itself is installed in
# the backend stage's virtualenv via Pipfile; this stage just stages the source
# and the production Streamlit config so the runtime stage can COPY them in.
FROM       ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python311 AS frontend-streamlit

WORKDIR    /var/app
COPY       ./feed_status/ui/ /var/app/feed_status/ui/
COPY       ./conf/streamlit.toml /var/app/.streamlit/config.toml

EXPOSE     8501

#####
# target: runtime
# Single container that fronts all traffic with nginx on :8080 and runs both
# the Flask backend (uwsgi on :8000) and the Streamlit UI (on :8501) under
# supervisord. See conf/{supervisord,nginx,streamlit}.* for the routing details.
FROM        ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python311 AS runtime

USER       root
RUN        apt update && \
           apt install uwsgi-plugin-python3 nginx -y && \
           apt clean && \
           rm -rf /var/lib/apt/lists/*

RUN        useradd uwsgi -s /bin/false && \
           mkdir -p /var/log/uwsgi /var/log/nginx /var/lib/nginx && \
           chown -R uwsgi:uwsgi /var/log/uwsgi

WORKDIR    /var/app

# Backend (Flask) artifacts + virtualenv (includes streamlit, supervisor)
COPY       --from=backend --chown=uwsgi /var/app/ /var/app/
ENV        PATH="/var/app/.venv/bin:${PATH}"

# Vue frontend artifacts
COPY       --from=frontend --chown=uwsgi /var/app/feed_status/static/ /var/app/feed_status/static/
COPY       --from=frontend --chown=uwsgi /var/app/feed_status/templates/ /var/app/feed_status/templates/

# Streamlit UI source + production config (placed by frontend-streamlit stage)
COPY       --from=frontend-streamlit --chown=uwsgi /var/app/feed_status/ui/ /var/app/feed_status/ui/
COPY       --from=frontend-streamlit --chown=uwsgi /var/app/.streamlit/ /var/app/.streamlit/

# Process supervisor + nginx template/entrypoint (entrypoint renders the
# template into /etc/nginx/nginx.conf at container start based on MONTY_UI).
COPY       ./conf/supervisord.conf /etc/supervisord.conf
COPY       ./conf/nginx.conf.template /etc/nginx/nginx.conf.template
COPY       ./conf/nginx-entrypoint.py /etc/nginx-entrypoint.py

# Backend starter (kept for parity with standalone dev / debug runs)
COPY       uwsgi-start.sh /var/app/

# supervisord runs as root and drops to per-program users.
CMD        ["/var/app/.venv/bin/supervisord", "-c", "/etc/supervisord.conf"]

EXPOSE     8080
