#!/bin/bash

# Path: scripts/node.sh

set -e

BLUE='\033[1;34m'
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
NC='\033[0m'

export NVM_DIR="$HOME/.programming/node"
export COREPACK_ENABLE_DOWNLOAD_PROMPT=0

echo -e "${BLUE}    LOG:${YELLOW} Setting up Node.js (NVM) in ${NVM_DIR}...${NC}"

if [ ! -d "$NVM_DIR" ]; then
    mkdir -p "$NVM_DIR"
    echo -e "${BLUE}    LOG:${YELLOW} Installing NVM to custom path...${NC}"
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | PROFILE=/dev/null bash
fi

[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

if command -v nvm &> /dev/null; then
    echo -e "${BLUE}    LOG:${YELLOW} Installing Node LTS...${NC}"
    nvm install --lts --no-progress
    nvm use --lts
    
    echo -e "${BLUE}    LOG:${YELLOW} Enabling Corepack (pnpm/yarn)...${NC}"
    
    corepack enable
    
    echo -e "${GREEN}    LOG: Node setup complete. $(node -v)${NC}"
    echo -e "${GREEN}    LOG: Package Managers: pnpm $(pnpm -v), yarn $(yarn -v)${NC}"
else
    echo -e "${RED}    ERROR: NVM failed to load from $NVM_DIR${NC}"
    exit 1
fi
