# Stage 1: Build TypeScript source code
FROM node:22-bookworm-slim AS builder

WORKDIR /app

# Install dependencies
COPY package*.json ./
RUN npm ci

# Copy TypeScript configuration and source
COPY tsconfig.json ./
COPY index.ts ./

# Compile to JavaScript (dist/)
RUN npm run build

# Stage 2: Production runtime image
FROM node:22-bookworm-slim AS runner

WORKDIR /app

ENV NODE_ENV=production

# Install only production dependencies
COPY package*.json ./
RUN npm ci --omit=dev && npm cache clean --force

# Copy compiled code from builder
COPY --from=builder /app/dist ./dist

# Create auth folder for persistent WhatsApp credentials
RUN mkdir -p /app/auth

VOLUME ["/app/auth"]

CMD ["npm", "start"]
