# --- Stage 1: Python Backend dev ---

FROM python:3.14-slim AS dev

LABEL org.opencontainers.image.title="The-DisExcel-project-dev"
LABEL org.opencontainers.image.source="https://git.homyk.space/MH.Dmitrii/The_DisExcel_project"

WORKDIR /home/excel-project

COPY pyproject.toml poetry.lock docker/entrypoint.sh alembic.ini ./

RUN chmod +x ./entrypoint.sh

RUN pip install --no-cache-dir --break-system-packages poetry \
    && poetry config virtualenvs.create false \
    && poetry install --no-root --no-interaction


# --- Stage 1: Python Backend builder ---

FROM python:3.14-slim AS builder

LABEL org.opencontainers.image.title="The-DisExcel-project-dev"
LABEL org.opencontainers.image.source="https://git.homyk.space/MH.Dmitrii/The_DisExcel_project"

WORKDIR /home/excel-project

ENV VIRTUAL_ENV=/opt/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

RUN python -m venv "$VIRTUAL_ENV"

COPY pyproject.toml poetry.lock ./

RUN pip install --no-cache-dir poetry \
    && poetry config virtualenvs.create false \
    && poetry install --no-root --no-interaction --only main,web \
    && pip uninstall -y poetry poetry-core poetry-plugin-export 

# --- Stage 2: Python Backend prod ---

FROM python:3.14-slim AS prod

LABEL org.opencontainers.image.title="The-DisExcel-project-prod"
LABEL org.opencontainers.image.source="https://git.homyk.space/MH.Dmitrii/The_DisExcel_project"

WORKDIR /home/excel-project

ENV VIRTUAL_ENV=/opt/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

COPY --from=builder /opt/venv /opt/venv

COPY pyproject.toml poetry.lock main.py docker/entrypoint.sh alembic.ini ./
COPY src/ ./src/

RUN chmod +x ./entrypoint.sh

RUN groupadd --gid 1000 appuser \
    && useradd --uid 1000 --gid appuser --shell /bin/bash --create-home appuser \
    && chown -R appuser:appuser /home/excel-project

USER appuser

ENTRYPOINT ["./entrypoint.sh", "--prod"]

# --- Stage 1: Python daemon builder ---

FROM python:3.14-slim AS worker-builder

LABEL org.opencontainers.image.title="The-DisExcel-project-dev"
LABEL org.opencontainers.image.source="https://git.homyk.space/MH.Dmitrii/The_DisExcel_project"

WORKDIR /home/excel-project

ENV VIRTUAL_ENV=/opt/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

RUN python -m venv "$VIRTUAL_ENV"

COPY pyproject.toml poetry.lock ./

RUN pip install --no-cache-dir poetry \
    && poetry config virtualenvs.create false \
    && poetry install --no-root --no-interaction --only main,daemon \
    && pip uninstall -y poetry poetry-core poetry-plugin-export 

# --- Stage 2: Python daemons ---

FROM python:3.14-slim AS daemon

LABEL org.opencontainers.image.title="The-DisExcel-project-prod"
LABEL org.opencontainers.image.source="https://git.homyk.space/MH.Dmitrii/The_DisExcel_project"

WORKDIR /home/excel-project

ENV VIRTUAL_ENV=/opt/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

COPY --from=worker-builder /opt/venv /opt/venv

COPY pyproject.toml poetry.lock alembic.ini daemon_run.py  ./
COPY src/ ./src/

RUN groupadd --gid 1000 appuser \
    && useradd --uid 1000 --gid appuser --shell /bin/bash --create-home appuser \
    && chown -R appuser:appuser /home/excel-project

USER appuser

ENTRYPOINT ["python","-u", "daemon_run.py", "--all"]
