local fes = require("fes")
local std = fes.std
local u = fes.util

local site = fes.fes()

fes.app.global.start(fes, site, "fes")

site:h2("Fes")
site:muted("fes is a lightweight, static, and optionated microframework.")

site:h2("Development")
site:p("You can " ..
std.external("https://git.fsdproject.org/fes/log.html", "browse") ..
" its source code or get a copy using the following command:")
site:code("git clone https://git.vxserver.dev/fSD/fes")

site:h2("Docker")
site:p("You can deploy websites using the " ..
std.external("https://git.vxserver.dev/fSD/-/packages/container/fes/latest", "docker") .. " image:")
site:code("docker pull git.vxserver.dev/fsd/fes:latest")

local tars = u.ls("archive/fes")

local function parseVersion(pkg)
	local version = pkg:match("-(.+)$")
	local parts = {}
	for n in version:gmatch("%d+") do
		parts[#parts + 1] = tonumber(n)
	end
	return parts
end

local function isNewer(a, b)
	local va, vb = parseVersion(a), parseVersion(b)
	local len = math.max(#va, #vb)

	for i = 1, len do
		local na, nb = va[i] or 0, vb[i] or 0
		if na ~= nb then
			return na > nb
		end
	end
	return false
end

local function latestRelease(packages, name)
	local latest = nil
	local prefix = "^" .. name .. "%-"

	for _, pkg in ipairs(packages) do
		if pkg:match(prefix) then
			if not latest or isNewer(pkg, latest) then
				latest = pkg
			end
		end
	end

	return latest
end

local latest = latestRelease(tars, "fes")

site:h2("Download")
site:ul {
	std.p(std.a("/archive/fes/" .. latest, latest) .. " (2026-31-1)"),
	std.a("/archive/fes", "Looking for specific version?")
}


fes.app.global.finish(fes, site)

return site
