FROM node:22-alpine AS admin-build
WORKDIR /admin
COPY admin/package*.json ./
RUN npm ci --no-audit --no-fund
COPY admin ./
RUN npm run build

FROM python:3.12-slim AS python-wheels

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /build

COPY requirements.txt requirements-yt-dlp.txt ./
RUN python -m pip install --no-cache-dir --upgrade pip wheel \
    && python -m pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt -r requirements-yt-dlp.txt

FROM python:3.12-slim AS runtime

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /app

RUN apt-get update \
    && apt-get install -y --no-install-recommends ffmpeg ca-certificates libgomp1 nodejs \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt requirements-yt-dlp.txt ./
COPY --from=python-wheels /wheels /wheels
RUN python -m pip install --no-cache-dir --no-index --find-links=/wheels -r requirements.txt -r requirements-yt-dlp.txt \
    && rm -rf /wheels

COPY app ./app
COPY --from=admin-build /admin/dist ./admin_dist

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