-- apply_parasite.lua
-- dofile_once this from sex_scene.lua.
-- Creates a persistent parasite entity as a child of the player, or
-- advances the stage of an existing one if re-infected by the same creature.
--
-- Returns:
--   "new"       – first infection, parasite created at stage 1
--   "advanced"  – already infected, stage advanced by 1
--   "maxed"     – already at max stage, no change
dofile_once("mods/lewd_noita/files/status_effects/parasite_utils.lua")

local PARASITE_MAX_STAGES = {
	parasite_firebug        = 5,
	parasite_fly            = 5,
	parasite_spider         = 5,
	parasite_rat            = 5,
	parasite_ant            = 5,
	parasite_fungus         = 4,
	parasite_hornygasflower = 4,
	parasite_longleg        = 5,
}

local PARASITE_STAGE_GLOBALS = {
	parasite_firebug        = "LN_STAGE_ANN_FIREBUG",
	parasite_fly            = "LN_STAGE_ANN_FLY",
	parasite_spider         = "LN_STAGE_ANN_SPIDER",
	parasite_rat            = "LN_STAGE_ANN_RAT",
	parasite_ant            = "LN_STAGE_ANN_ANT",
	parasite_fungus         = "LN_STAGE_ANN_FUNGUS",
	parasite_hornygasflower = "LN_STAGE_ANN_FLOWER",
	parasite_longleg        = "LN_STAGE_ANN_LONGLEG",
}

function apply_parasite(player_id, entity_name, icon_file, display_name, description, script_file, execute_every)
	local max_stage = PARASITE_MAX_STAGES[entity_name] or 4

	-- Check if this parasite is already active
	for _, child in ipairs(EntityGetAllChildren(player_id) or {}) do
		if EntityGetName(child) == entity_name then
			-- Already infected – advance stage via sex trigger
			local stage, _, vsc = get_parasite_stage(child)
			if vsc ~= nil and stage < max_stage then
				set_parasite_stage(child, stage + 1)
				local uic = EntityGetFirstComponent(child, "UIIconComponent")
				if uic then
					ComponentSetValue2(uic, "name", display_name .. " " .. get_stage_label(stage + 1))
				end
				local overlay_name = entity_name:match("^parasite_(.+)$")
				if overlay_name then
					update_parasite_overlay(player_id, overlay_name, stage + 1)
				end
				return "advanced"
			end
			return "maxed"
		end
	end

	-- First infection: create at stage 1
	local gkey = PARASITE_STAGE_GLOBALS[entity_name]
	if gkey then GlobalsSetValue(gkey, "1") end

	local parasite = EntityCreateNew(entity_name)
	EntityAddComponent2(parasite, "InheritTransformComponent", {})
	EntityAddComponent2(parasite, "UIIconComponent", {
		icon_sprite_file = icon_file,
		name = display_name,
		description = description,
	})
	EntityAddComponent2(parasite, "VariableStorageComponent", {
		name="parasite_timer",
		value_string="0",
		value_int=0,
	})
	EntityAddComponent2(parasite, "VariableStorageComponent", {
		name="parasite_stage",
		value_string=tostring(GameGetFrameNum()),
		value_int=1,
	})
	EntityAddComponent2(parasite, "LuaComponent", {
		script_source_file=script_file,
		execute_every_n_frame=execute_every,
	})
	EntityAddChild(player_id, parasite)
	local overlay_name = entity_name:match("^parasite_(.+)$")
	if overlay_name then
		update_parasite_overlay(player_id, overlay_name, 1)
	end
	return "new"
end
