-- vim: ft=terra
local pstr = lib.mem.ptr(int8)
local pref = lib.mem.ref(int8)

local terra 
render_conf_sec(co: &lib.srv.convo, uid: uint64): pstr
	var time: lib.store.timepoint = co.who.source:auth_sigtime_user_fetch(uid)
	var tstr: int8[26]
	lib.osclock.ctime_r(&time, &tstr[0])
	var body = data.view.conf_sec {
		lastreset = pstr {
			ptr = &tstr[0], ct = lib.str.sz(&tstr[0])
		}
	}
	
	var a = co:stra(768) -- defer a:free()

	if co.srv.cfg.credmgd then
		var new = co:pgetv('new')
		if not new then
			body:append(&a)
			var credmgr = data.view.conf_sec_credmg {
				credlist = pstr{'',0};
			}
			var creds = co.srv:auth_enum_uid(uid)
			if creds.ct > 0 then defer creds:free()
				var cl = co:stra(256)
				for i=0, creds.ct do var c = creds(i).ptr
					if not c.blacklist then
						cl:lpush('<option value="'):shpush(c.aid):lpush('"> ['):push(c.kind,0):lpush('] '):push(c.comment,0)
						if c.netmask.pv ~= 0 then
							-- push string rep
						end
						cl:lpush('</option>')
					end
				end
				credmgr.credlist = cl:finalize()
			end
			credmgr:append(&a)
			--if credmgr.credlist.ct > 0 then credmgr.credlist:free() end
		else
			if new:cmp('pw') then
				var d: data.view.conf_sec_pwnew
				var time = lib.osclock.time(nil)
				var timestr: int8[26] lib.osclock.ctime_r(&time, &timestr[0])
				var cmt = co:stra(48)
				cmt:lpush('enrolled over http on '):push(&timestr[0],0)
				d.comment = cmt:finalize()

				var st = d:poolstr(&co.srv.pool)
				--d.comment:free()
				return st
			elseif new:cmp('challenge') then
			-- we're going to break the rules a bit and do database munging from
			-- the rendering code, because doing otherwise in this case would be
			-- genuinely nightmarish
			elseif new:cmp('otp') then
			elseif new:cmp('api') then
			else return pstr.null() end
		end
	else body:append(&a) end

	return a:finalize()
end

terra lib.render.conf.sec_overlay
(co: &lib.srv.convo, path: lib.mem.ptr(pref)): pstr
 -- render the credential panel for the current user, allowing
 -- it to be reused in the administration UI
	return render_conf_sec(co,co.who.id)
end

return render_conf_sec
