# Multi-stage build for React Awards Map
FROM harbor.scottyah.com/dockerhub-cache/library/node:20-alpine AS frontend-builder

WORKDIR /app/frontend

# Copy frontend package files
COPY frontend/package*.json ./

# Install frontend dependencies
RUN npm ci

# Copy frontend source
COPY frontend/ ./

# Build frontend for production
ENV VITE_API_URL=""
RUN npm run build

# Production stage
FROM harbor.scottyah.com/dockerhub-cache/library/node:20-alpine

WORKDIR /app

# Copy backend package files
COPY backend/package*.json ./

# Install production dependencies only
RUN npm ci --only=production

# Copy backend source
COPY backend/ ./

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

# Create directory for database
RUN mkdir -p /app/data

# Expose port
EXPOSE 4000

# Set production environment
ENV NODE_ENV=production
ENV DATABASE_PATH=/app/data/awards.db

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD node -e "require('http').get('http://localhost:4000/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"

# Start the application
CMD ["node", "index.js"]
