# ============================================
# Base stage - shared configuration
# ============================================
FROM oven/bun:latest AS base
WORKDIR /app

# Install system dependencies with cleanup in same layer
RUN apt-get update && \
    apt-get install -y --no-install-recommends git && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# ============================================
# Dependencies stage - installs all deps
# ============================================
FROM base AS deps

# Copy only package files first (better layer caching)
COPY package.json bun.lock ./
COPY web/package.json web/bun.lock ./web/

# Install all dependencies in one layer
RUN bun install --frozen-lockfile && \
    cd web && bun install --frozen-lockfile

# ============================================
# Development stage - for local dev with volume mounts
# ============================================
FROM base AS development

# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/web/node_modules ./web/node_modules

# Expose ports
EXPOSE 3000

# Default command
CMD ["bun", "run", "dev"]

# ============================================
# Production stage - full app with source code
# ============================================
FROM base AS production

# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/web/node_modules ./web/node_modules

# Copy source code
COPY . .

# Expose ports
EXPOSE 3000

# Default command
CMD ["bun", "run", "dev"]
