# HCFS Node.js Development Environment
FROM bzzz-hcfs-base:latest

LABEL maintainer="anthony@deepblack.cloud"
LABEL description="HCFS Node.js development environment with modern JS/TS tools"
LABEL language="javascript"
LABEL version="1.0.0"

# Install Node.js and npm
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get install -y nodejs

# Install Yarn package manager
RUN npm install -g yarn

# Install global development tools
RUN npm install -g \
    # TypeScript ecosystem
    typescript \
    ts-node \
    @types/node \
    # Build tools
    webpack \
    webpack-cli \
    rollup \
    vite \
    # Testing frameworks
    jest \
    mocha \
    cypress \
    # Code quality
    eslint \
    prettier \
    @typescript-eslint/parser \
    @typescript-eslint/eslint-plugin \
    # Development servers
    nodemon \
    concurrently \
    # Package management
    npm-check-updates \
    # Documentation
    jsdoc \
    typedoc \
    # CLI tools
    commander \
    inquirer \
    chalk \
    # Process management
    pm2 \
    forever

# Create Node.js workspace structure
RUN sudo -u agent mkdir -p /home/agent/work/{src,tests,docs,public,build,dist}

# Set up Node.js environment
ENV NODE_ENV=development
ENV NPM_CONFIG_PREFIX=/home/agent/.npm-global
ENV PATH=/home/agent/.npm-global/bin:$PATH

# Create npm configuration
RUN sudo -u agent mkdir -p /home/agent/.npm-global && \
    sudo -u agent npm config set prefix '/home/agent/.npm-global'

# Install HCFS Node.js SDK
COPY hcfs-nodejs-sdk /opt/hcfs/nodejs-sdk
RUN cd /opt/hcfs/nodejs-sdk && npm install && npm link

# Create package.json template for new projects
RUN sudo -u agent bash -c 'cat > /home/agent/work/package.json.template << EOF
{
  "name": "hcfs-agent-project",
  "version": "1.0.0",
  "description": "HCFS-enabled Node.js project",
  "main": "src/index.js",
  "scripts": {
    "start": "node src/index.js",
    "dev": "nodemon src/index.js",
    "test": "jest",
    "build": "webpack --mode production",
    "lint": "eslint src/",
    "format": "prettier --write src/"
  },
  "dependencies": {
    "@hcfs/sdk": "file:/opt/hcfs/nodejs-sdk",
    "express": "^4.18.0",
    "axios": "^1.0.0"
  },
  "devDependencies": {
    "nodemon": "^3.0.0",
    "jest": "^29.0.0",
    "eslint": "^8.0.0",
    "prettier": "^3.0.0"
  },
  "engines": {
    "node": ">=18.0.0"
  }
}
EOF'

# Node.js-specific HCFS integration script
COPY scripts/nodejs-hcfs-init.js /opt/hcfs/scripts/
RUN chmod +x /opt/hcfs/scripts/nodejs-hcfs-init.js

# Expose common Node.js development ports
EXPOSE 3000 8080 8000 9229

# Add Node.js-specific entrypoint
COPY scripts/nodejs-entrypoint.sh /opt/hcfs/
RUN chmod +x /opt/hcfs/nodejs-entrypoint.sh

ENTRYPOINT ["/opt/hcfs/nodejs-entrypoint.sh"]
CMD ["node"]