# ── Stage 1: build Vite frontend ─────────────────────────────────────────────
FROM denoland/deno:2.7.11 AS builder

WORKDIR /app

COPY deno.json deno.lock package.json ./
COPY tsconfig*.json vite.config.ts lingui.config.ts ./
RUN deno install

COPY index.html ./
COPY public/ ./public/
COPY scripts/ ./scripts/
COPY src/ ./src/

# In same-origin deployments (API serves the frontend), no build args are needed
# — the frontend uses relative URLs at runtime. Only set VITE_API_HOSTNAME if
# the API lives on a different host than the frontend (e.g. a separate API server).
ARG VITE_API_PROTOCOL
ARG VITE_API_HOSTNAME
ARG VITE_API_PORT
ENV VITE_API_PROTOCOL=$VITE_API_PROTOCOL \
    VITE_API_HOSTNAME=$VITE_API_HOSTNAME \
    VITE_API_PORT=$VITE_API_PORT

RUN deno task build

# ── Stage 2: runtime ──────────────────────────────────────────────────────────
FROM denoland/deno:alpine-2.7.11

RUN apk add --no-cache ffmpeg

WORKDIR /app

COPY deno.json deno.lock ./
RUN deno install

COPY api/ ./api/
COPY --from=builder /app/dist/ ./dist/

# Persistent data: database and uploaded/generated files must be mounted as volumes.
VOLUME ["/app/api/sql", "/app/api/uploads"]

EXPOSE 8000

CMD ["deno", "task", "start"]
