# CloneWeb - Web Interface + Crawler Dockerfile
FROM node:20-alpine

WORKDIR /app

# Install wget for crawler + bash for scripts
RUN apk add --no-cache wget bash && ln -s /usr/bin/wget /bin/wget

# --- Crawler service (port 3002) ---
COPY simple-crawler/package*.json /app/simple-crawler/
WORKDIR /app/simple-crawler
RUN npm install --production

COPY simple-crawler/ /app/simple-crawler/

# --- Web interface service (port 3000) ---
WORKDIR /app
COPY web-interface/package*.json /app/web-interface/
WORKDIR /app/web-interface
RUN npm install --production

COPY web-interface/ /app/web-interface/

# --- Storage dir for clones ---
RUN mkdir -p /app/cloned-sites

# Expose port
EXPOSE 3000

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

# Start crawler in background (port 3002), then web server (port 3000)
CMD bash -c "node /app/simple-crawler/wget-cloner.js > /tmp/crawler.log 2>&1 & node server.js"