// form/nc/nc-rag/nc-rag.js
function changeTab() {
	loadData()
}

function loadData() {
	if (rec.tab === 'product_list') {
		loadProductList()
	} else {
		loadRagResult()
	}
}

function openPath() {
	const path = rec.document_path
	if (!path || path === '') {
		message.setWarning(state, 'Path is empty')
		return
	}
	const param = {path: path}
	nc.callServer(state, 'path', param, ret => {
		if (ret.error) {
			message.setError(state, `Failed to open path: ${ret.error}`)
		}
	})
}

function loadRagResult() {
	const param = {
		name: 'form/nc/nc-rag/grid.json',
		parameter: {},
		return: {
			grid: [
				{
					name: 'rag_result',
					tab: 'product_match'
				}
			]
		}
	}
	nc.callServer(state, 'query', param, ret => {
		if (ret.error) {
			rec.status = 'Error loading data: ' + ret.error
			return
		}
		if (ret.grid) {
			for (let gridName in ret.grid) {
				nc.setGrid(state, gridName, ret.grid[gridName])
			}
		}
	})
	loadDirTreeInfo()
}

function loadDirTreeInfo() {
	const param = {
		action: 'dir_tree_info',
		document_path: rec.document_path,
		max_depth: rec.max_depth
	}
	nc.callServer(state, 'rag', param, ret => {
		if (ret.error) {
			util.printRed(ret.error)
			return
		}
		const data = []
		data.push({metric: 'Max depth searched', value: rec.max_depth})
		data.push({metric: 'Max depth found', value: ret.max_depth})
		data.push({metric: 'Total folders', value: ret.total_folder_count})
		data.push({metric: 'Symlinks found', value: ret.symlink_count})
		data.push({metric: 'Time used', value: (ret.used_time * 1000).toFixed(1) + ' ms'})
		if (ret.deepest_folder_path) {
			data.push({metric: 'Deepest folder', value: ret.deepest_folder_path})
		}
		if (ret.folder_count_by_level) {
			for (let level = 1; level <= ret.max_depth; level++) {
				const count = ret.folder_count_by_level[level] || 0
				data.push({metric: 'Folders at level ' + level, value: count})
			}
		}
		nc.setGrid(state, 'dir_tree_info', {data: data})
	})
}

function loadProductList() {
	const param = {
		name: 'form/nc/nc-rag/grid.json',
		parameter: {},
		return: {
			grid: [
				{
					name: 'product_list',
					tab: 'product_list'
				}
			]
		}
	}
	nc.callServer(state, 'query', param, ret => {
		if (ret.error) {
			rec.status = 'Error loading products: ' + ret.error
			return
		}
		if (ret.grid) {
			for (let gridName in ret.grid) {
				nc.setGrid(state, gridName, ret.grid[gridName])
			}
		}
	})
}

function startMatch() {
	rec.status = 'Starting product matching...'
	const param = {
		action: 'start_match',
		document_path: rec.document_path,
		max_depth: rec.max_depth,
		file_type: rec.file_type
	}
	nc.callServer(state, 'rag', param, ret => {
		if (ret.error) {
			rec.status = 'Error: ' + ret.error
			util.printRed(ret.error)
			return
		}
		rec.status = 'Match complete: ' + ret.file_count + ' files processed, ' + ret.match_count + ' matched'
		loadRagResult()
	})
}

function queryRag() {
	if (!rec.chat_question || rec.chat_question === '') {
		message.setWarning(state, 'Please enter a question')
		return
	}
	rec.chat_answer = 'Querying...'
	const param = {
		action: 'query',
		question: rec.chat_question
	}
	nc.callServer(state, 'rag', param, ret => {
		if (ret.error) {
			rec.chat_answer = 'Error: ' + ret.error
			util.printRed(ret.error)
			return
		}
		rec.chat_answer = ret.answer || 'No answer received'
	})
}

function init() {
	loadData()
}

init()
