-- @author DoomTero 2026

ZR_SoundProfiles = ZR_SoundProfiles or {}

local DEBUG = true
local function log(fmt, ...)
	if DEBUG then print(string.format("[ZR SoundProfiles] " .. fmt, ...)) end
end

ZR_SoundProfiles._registry = ZR_SoundProfiles._registry or {}

function ZR_SoundProfiles.register(name, profile)
	if type(name) ~= "string" or name == "" then
		print("[ZR SoundProfiles] register: bad name")
		return false
	end
	if type(profile) ~= "table" then
		print("[ZR SoundProfiles] register: profile must be a table for " .. name)
		return false
	end
	if type(profile.stages) ~= "table" then
		print("[ZR SoundProfiles] register: missing stages table for " .. name)
		return false
	end
	for stageName, stage in pairs(profile.stages) do
		if type(stage) ~= "table" then
			print(string.format(
				"[ZR SoundProfiles] register: stage %q is not a table in %s",
				stageName, name))
			return false
		end
		local hasPool   = type(stage.pool) == "table" and #stage.pool > 0
		local hasPhases = type(stage.phases) == "table" and #stage.phases > 0
		if not hasPool and not hasPhases then
			print(string.format(
				"[ZR SoundProfiles] register: stage %q in %s has neither pool nor phases",
				stageName, name))
			return false
		end
	end
	ZR_SoundProfiles._registry[name] = profile
	log("registered profile: %s", name)
	return true
end

function ZR_SoundProfiles.get(name)
	return name and ZR_SoundProfiles._registry[name] or nil
end

function ZR_SoundProfiles.resolve(name)
	local p = ZR_SoundProfiles.get(name)
	if p then return p, name end
	if name and name ~= "default" then
		log("profile %q not found, falling back to 'default'", name)
	end
	return ZR_SoundProfiles.get("default"), "default"
end

function ZR_SoundProfiles.list()
	local names = {}
	for k in pairs(ZR_SoundProfiles._registry) do names[#names + 1] = k end
	table.sort(names)
	return names
end

ZR_SoundProfiles.register("default", {
	pulseTicks = 90,
	stages = {
		intro = {
			pool = {
				"PainFromBite",
				"PainFromLacerate",
				"PainFromScratch",
				"PainFromHitBlunt",
			},
		},
		main = {
			pool = {
				"PainMoodle",
				"PainFromGlassCut",
				"PainFromScratch",
			},
		},
		outro = {
			pool = {
				"SighReliefed",
				"SighSad",
				"PainMoodle",
				"CorpseLowEffort",
			},
			pulseTicks = 60,
		},
	},
})

ZR_SoundProfiles.register("defeatpunch_default", {
	pulseTicks = 90,
	stages = {
		intro = {
			pool = {
				"PainFromBite",
				"PainFromLacerate",
				"PainFromScratch",
				"PainFromHitBlunt",
			},
		},
		main = {
			pool = {
				"PainMoodle",
				"PainFromGlassCut",
				"PainFromScratch",
			},
		},
		outro = {
			pool = {
				"SighReliefed",
				"SighSad",
				"PainMoodle",
				"CorpseLowEffort",
			},
			pulseTicks = 60,
		},
	},
})

ZR_SoundProfiles.register("soft_moan", {
	pulseTicks = 120,
	stages = {
		intro = {
			pool = { "PainMoodle", "Exercise" },
		},
		main = {
			pool = { "PainMoodle", "CorpseLowEffort", "Exercise" },
		},
		outro = {
			pool = { "SighReliefed", "SighSad" },
			pulseTicks = 90,
		},
	},
})

ZR_SoundProfiles.register("rough", {
	pulseTicks = 120,
	stages = {
		intro = {
			pool = { "PainFromBite", "PainFromLacerate" },
			pulseTicks = 90,
		},
		main = {
			pool = {
				"PainFromBite",
				"PainFromLacerate",
				"PainFromScratch",
				"PainFromHitBlunt",
			},
		},
		outro = {
			pool = { "PainMoodle", "SighSad" },
		},
	},
})

return ZR_SoundProfiles
