# ─── Stage 1: Build the Svelte UI ────────────────────────────────────────────
FROM node:20-alpine AS ui-builder

WORKDIR /app/ui
COPY ui/package.json ui/package-lock.json* ./
RUN npm install

COPY ui/ ./
RUN npm run build


# ─── Stage 2: Bun runtime ────────────────────────────────────────────────────
FROM oven/bun:1-alpine AS runtime

WORKDIR /app

# Install production dependencies
COPY package.json bun.lockb* ./
RUN bun install --production --frozen-lockfile || bun install --production

# Copy source
COPY src/ ./src/
COPY tsconfig.json ./

# Copy built UI from stage 1
COPY --from=ui-builder /app/ui/dist ./ui/dist

# Volumes for user data
VOLUME ["/app/torrents", "/app/config"]

EXPOSE 3000

CMD ["bun", "run", "src/index.ts"]
