local h = require("@lib/html")
local fs = require("@lune/fs")

local function b64encode(data: string): string
	local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
	local bytes = { string.byte(data, 1, #data) }
	local out = {}
	local i = 1
	while i <= #bytes do
		local b1 = bytes[i]
		local b2 = bytes[i + 1]
		local b3 = bytes[i + 2]
		local n = b1 * 65536 + (b2 or 0) * 256 + (b3 or 0)
		local c1 = math.floor(n / 262144) % 64 + 1
		local c2 = math.floor(n / 4096) % 64 + 1
		local c3 = math.floor(n / 64) % 64 + 1
		local c4 = (n % 64) + 1
		if not b2 then
			table.insert(out, string.sub(b, c1, c1))
			table.insert(out, string.sub(b, c2, c2))
			table.insert(out, "==")
		elseif not b3 then
			table.insert(out, string.sub(b, c1, c1))
			table.insert(out, string.sub(b, c2, c2))
			table.insert(out, string.sub(b, c3, c3))
			table.insert(out, "=")
		else
			table.insert(out, string.sub(b, c1, c1))
			table.insert(out, string.sub(b, c2, c2))
			table.insert(out, string.sub(b, c3, c3))
			table.insert(out, string.sub(b, c4, c4))
		end
		i += 3
	end
	return table.concat(out)
end

local resultsMarkdown = ""
do
	local ok, content = pcall(fs.readFile, "results.md")
	if ok and typeof(content) == "string" then
		resultsMarkdown = content
	else
		resultsMarkdown = "(results.md not found or could not be read)"
	end
end

local resultsMarkdownB64 = b64encode(resultsMarkdown)
local renderScript = [[
  (function(){
    var el = document.getElementById('results-rendered');
    if (!el || !window.marked) { return; }
    var b64 = el.getAttribute('data-md-b64') || '';
    try {
      var md = atob(b64);
      el.innerHTML = window.marked.parse(md, { gfm: true, breaks: false });
    } catch (e) {
      el.textContent = 'Failed to render markdown: ' + e;
    }
  })();
]]
local renderScriptSrc = "data:text/javascript;base64," .. b64encode(renderScript)

return h.html({ lang = "en" })({
	h.head()({
		h.meta({ charset = "utf-8" }),
		h.meta({ name = "viewport", content = "width=device-width, initial-scale=1" }),
		h.meta({ name = "color-scheme", content = "dark light" }),
		h.title()({
			"lsf bench",
		}),
		h.link({
			rel = "icon",
			href = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAGFBMVEUAov4Aov4Aof4Aov7l8/6l0v5ctP4Fof7s4QukAAAABHRSTlMBRY/MU7dEywAAALZJREFUeNqNk0EOwyAMBGvs3f3/j1sXWkKDK+biQ0YwisXjFPdm/76bJL0cq4VBdVBTp5RcSS2ZbqxJTQVe3QBwEfRLJJRakcCPYEUCIqkTugCpVQkEqoQJJdn5XwB1Y7khtoJ9BYR2zEUAkkBxmzBuYOQkuEnIbwJyAgFxSWgpAATSec8QMRM8ZYIIBIGUIewW0c9g9JkJA7N2lSKTx6on00nrsupC4txkLdWPzNxrYR7kp8/+CUlvDvtDdoJIAAAAAElFTkSuQmCC",
		}),
		h.link({
			rel = "stylesheet",
			href = "https://cdn.jsdelivr.net/npm/@picocss/pico@2.1.1/css/pico.classless.min.css",
		}),
		h.script({ src = "https://cdn.jsdelivr.net/npm/marked/marked.min.js", defer = "defer" })({}),
		h.script({ src = renderScriptSrc, defer = "defer" })({}),
	}),
	h.body()({
		h.header()({
			h.h1()({
				"lsf bench (luau.software bench)",
			}),
			h.p()({
				"a benchmark for testing local LLMs capability with Luau programming - written in Luau",
			}),
			h.p()({
				"ollama was used for inference, all model names are the exact model names you'd ollama run",
			}),
		}),
		h.main()({
			h.section()({
				h.h2()({ "Benchmark Results" }),
				h.div({ id = "results-rendered", ["data-md-b64"] = resultsMarkdownB64 })({}),
				h.noscript()({
					"JavaScript is required to render Markdown. Showing raw contents:",
					h.pre({ style = "white-space: pre; overflow-x: auto;" })({
						h.code({ class = "language-markdown" })({ resultsMarkdown }),
					}),
				}),
				h.hr()({}),
			}),
			h.header()({
				h.h1()({ "info for this benchmark:" }),
			}),
			h.div({ style = "display: flex; flex-direction: row; flex-wrap: wrap; column-gap: 10px;" })({
				h.article()({
					h.header()({
						"my email (contact for questions about the benchmark)",
					}),
					h.p()({
						"cyclic@luau.software",
					}),
				}),
				h.article()({
					h.header()({
						"the git repo for the source and partial technical details",
					}),
					h.a({ href = "https://git.luau.software/cyclic/lsfbench" })({
						"lsfbench",
					}),
				}),
				h.img({
					src = "https://git.luau.software/luau.software/luau.software/raw/branch/main/public/luauchan.png",
					style = "position: fixed; bottom: 0px; right: 0px; width: 300px; height: auto; z-index: 9999;",
				})({})
			}),
		}),
	}),
})
