FROM node:20-alpine AS builder

WORKDIR /app

# Copy root configs
COPY package*.json ./
COPY tsconfig.json ./
COPY tsconfig.base.json ./

# Copy packages
COPY apps/web/package*.json ./apps/web/
COPY shared/package*.json ./shared/

# Install dependencies
RUN npm ci

# Copy source code
COPY apps/web ./apps/web
COPY shared ./shared

# Build the app
WORKDIR /app/apps/web
# We need VITE_API_BASE_URL at build time for SvelteKit static replacements if any,
# but usually it's better to use dynamic env vars if possible.
# For now, let's assume it can be provided during build or we use the default.
RUN npm run build

# ---
FROM node:20-alpine AS runner

WORKDIR /app

# Copy build artifacts and necessary files
COPY --from=builder /app/apps/web/build ./apps/web/build
COPY --from=builder /app/apps/web/package.json ./apps/web/package.json
# Node adapter needs dependencies to run
COPY --from=builder /app/node_modules ./node_modules

EXPOSE 5173

ENV PORT=5173
ENV NODE_ENV=production

WORKDIR /app/apps/web
CMD ["node", "build"]
