# syntax=docker/dockerfile:1

FROM node:22-alpine AS frontend

WORKDIR /build

RUN corepack enable && corepack prepare pnpm@11.1.2 --activate

COPY pyproject.toml README.md ./
COPY frontend/package.json frontend/pnpm-lock.yaml frontend/pnpm-workspace.yaml ./frontend/

WORKDIR /build/frontend
RUN pnpm install --frozen-lockfile

COPY frontend/ ./
RUN pnpm build

FROM python:3.12-slim AS base

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1

WORKDIR /app

RUN python -m pip install --upgrade pip

FROM base AS pypi-runtime

ARG PACKAGE_VERSION

COPY bot.py ./

RUN python -m pip install "nonebot-plugin-webui==${PACKAGE_VERSION#v}" \
    && mkdir -p /app/.nonebot-plugin-webui

EXPOSE 8080

CMD ["python", "bot.py"]

FROM base AS source-runtime

COPY pyproject.toml README.md bot.py ./
COPY nonebot_plugin_webui ./nonebot_plugin_webui
COPY --from=frontend /build/nonebot_plugin_webui/static ./nonebot_plugin_webui/static

RUN python -m pip install . \
    && mkdir -p /app/.nonebot-plugin-webui

EXPOSE 8080

CMD ["python", "bot.py"]
