# Build stage
FROM node:23.9-alpine AS build

# Install build dependencies
RUN apk add --no-cache git python3 make g++

# Set working directory
WORKDIR /app

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

# Copy source code
COPY repo/ .

# Verify files are copied
RUN ls -la

# Install node modules (skip optional to reduce size)
RUN npm install --production=false --no-optional

# Build GUI
RUN cd src/gui && npm run build && cd -

# Production stage
FROM node:23.9-alpine

# Install runtime dependencies
RUN apk add --no-cache git

# Create app directories
RUN mkdir -p /opt/puter/app

# Set working directory
WORKDIR /opt/puter/app

# Copy built artifacts from build stage
COPY --from=build /app/src/gui/dist ./dist
COPY --from=build /app/package*.json ./
COPY --from=build /app/src ./src

# Expose port
EXPOSE 4100

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

# Environment
ENV NO_VAR_RUNCUME=1

# Start Puter
CMD ["npm", "start"]
