# ============================================
# BraceIQMed API - Node.js Express Server
# ============================================

FROM node:20-alpine

# Install build dependencies for better-sqlite3
RUN apk add --no-cache python3 make g++ sqlite

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies (rebuild native modules for Linux)
RUN npm ci --only=production && \
    npm rebuild better-sqlite3

# Copy application code (excluding node_modules via .dockerignore)
COPY . .

# Create data directories
RUN mkdir -p /app/data/uploads /app/data/outputs

# Environment variables
ENV NODE_ENV=production
ENV PORT=3002
ENV DATA_DIR=/app/data
ENV DB_PATH=/app/data/braceflow.db

EXPOSE 3002

# Health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost:3002/api/health || exit 1

CMD ["node", "server.js"]
