FROM node:20-bookworm-slim AS editor-builder

WORKDIR /build/vendor/oh-my-cv

RUN corepack enable
RUN corepack prepare pnpm@9.4.0 --activate

COPY vendor/oh-my-cv/ ./

RUN pnpm install --frozen-lockfile --registry=https://registry.npmjs.org

ENV NUXT_PUBLIC_SIGNAL_API_BASE=""

RUN pnpm build-fast:pkg
RUN pnpm build


FROM python:3.12-slim AS runtime

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_DEFAULT_TIMEOUT=120 \
    PIP_RETRIES=5 \
    PYTHONPATH=/app/src

WORKDIR /app

COPY pyproject.toml README.md ./
COPY src/ ./src/

RUN pip install --no-cache-dir .

COPY --from=editor-builder /build/vendor/oh-my-cv/site/.output/public/ \
    ./vendor/oh-my-cv/site/.output/public/

RUN groupadd --gid 1000 resume-agent \
    && useradd --uid 1000 --gid resume-agent --create-home resume-agent \
    && mkdir -p /app/.resume-agent /app/output \
    && chown -R resume-agent:resume-agent /app

USER resume-agent

EXPOSE 8000

HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=5 \
    CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/api/status', timeout=5).read()"]

CMD ["uvicorn", "resume_agent.webapp:app", "--host", "0.0.0.0", "--port", "8000"]
