#!/bin/bash
# DYNR Module: Module Forge v2.3.6

run_create_module() {
    BLUE='\033[38;5;45m'; PURPLE='\033[38;5;127m'; GOLD='\033[38;5;178m'; RED='\033[38;5;196m'; NC='\033[0m'
    
    clear
    echo -e "${PURPLE}=================================================${NC}"
    echo -e "${BLUE}    🐉  DYNASTY REVOLUTION: MODULE FORGE  🐉     ${NC}"
    echo -e "${PURPLE}=================================================${NC}"

    echo -e "${GOLD}Enter the name of your new module:${NC}"
    read -p "Name (e.g. System Audit): " RAW_NAME < /dev/tty

    if [[ -z "$RAW_NAME" ]]; then
        echo -e "${RED}Forge extinguished: No name provided.${NC}"
        sleep 2; dynr; return
    fi

    # Formatting: lowercase and replace spaces with hyphens for the filename
    FILE_NAME=$(echo "$RAW_NAME" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
    # Formatting: replace hyphens with underscores for the function name
    FUNC_NAME=$(echo "$FILE_NAME" | tr '-' '_')
    TARGET="/opt/dynr/modules/$FILE_NAME"

    echo -e "${BLUE}🔨 Hammering out the '$FILE_NAME' scroll...${NC}"

    # Create the template
    cat <<EOF > "$TARGET"
#!/bin/bash
# DYNR Module: $RAW_NAME v2.3.6

run_$FUNC_NAME() {
    BLUE='\033[38;5;45m'; PURPLE='\033[38;5;127m'; GOLD='\033[38;5;178m'; NC='\033[0m'
    
    clear
    echo -e "\${PURPLE}=================================================\${NC}"
    echo -e "\${BLUE}    🐉  DYNASTY REVOLUTION: $RAW_NAME  🐉    \${NC}"
    echo -e "\${PURPLE}=================================================\${NC}"

    # --- Your Logic Here ---
    echo "The scroll is blank... write your legend."

    # --- Return to Dynasty ---
    echo -e "\${PURPLE}=================================================\${NC}"
    read -p "Press Enter to return to the Dynasty..." < /dev/tty
    dynr
}
EOF

    chmod +x "$TARGET"
    echo -e "${GOLD}✨ Module created at: $TARGET${NC}"
    echo -e "You can now edit it or run it using: ${BLUE}dynr $FILE_NAME${NC}"
    echo -e "${PURPLE}-------------------------------------------------${NC}"
    
    read -p "Press Enter to return..." < /dev/tty
    dynr
}