# Build stage
FROM node:22-slim AS builder

WORKDIR /app

# Copy package files
COPY package.json package-lock.json ./

# Install all dependencies
RUN npm ci

# Copy the rest of the source
COPY . .

# Build the frontend (creates the 'dist' folder)
RUN npm run build

# Runtime stage
FROM node:22-slim

# Install sqlite3 runtime dependencies
RUN apt-get update && apt-get install -y python3 make g++ && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy package files
COPY package.json package-lock.json ./

# Install only production dependencies (better-sqlite3 needs compilation)
RUN npm ci --only=production && npm install -g tsx

# Copy built frontend from builder stage
COPY --from=builder /app/dist ./dist

# Copy the server file and other necessary source
COPY server.ts ./
# (If there are other source files needed by server.ts, they should be copied too)
# Looking at server.ts, it doesn't seem to import other local files besides 'vite'
# but wait, it uses 'path' and 'url' (built-in).

# Expose the port the app runs on
EXPOSE 3000

# Set environment to production
ENV NODE_ENV=production

# Command to run the server
CMD ["tsx", "server.ts"]
