# syntax=docker/dockerfile:1
##################
# BUILDER        #
##################

FROM python:3.11.4-slim-buster AS builder

# set work directory
WORKDIR /app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# install system dependencies
RUN apt-get update && \
    apt-get install -y --no-install-recommends gcc

# lint
RUN pip install --upgrade pip
RUN pip install flake8==6.0.0
COPY . /app/
RUN flake8 --ignore=E501,F401,E126,W391 .

COPY ./requirements.txt .


RUN pip wheel --no-cache-dir --no-deps --wheel-dir /app/wheels -r requirements.txt

##################
# FINAL          #
##################

FROM python:3.11.4-slim-buster


# create directory for the app user
ENV APP_HOME=/app
RUN mkdir -p $APP_HOME/gestion_reservas/staticfiles

# create the app user
RUN addgroup --system app && adduser --system --group app

# create the appropriate directories
WORKDIR $APP_HOME

# install system dependencies
RUN apt-get update && apt-get install -y sqlite3 netcat vim
COPY --from=builder /app/wheels /wheels
COPY --from=builder /app/requirements.txt .

RUN pip install --upgrade pip
RUN pip install --no-cache /wheels/*

# copy entrypoint.sh
COPY ./entrypoint.sh .

# copy project
COPY . $APP_HOME

# chown all the files to the app user
RUN chown -R app:app $APP_HOME

# change to the app user
USER app
WORKDIR $APP_HOME/gestion_reservas

# run entrypoint.sh
ENTRYPOINT ["/app/entrypoint.sh"]
