-- womb_utils.lua
-- Digestive-Alchemy-style helper functions for the womb MaterialInventoryComponent.
-- dofile_once() this from any script that needs to read/write the womb.
--
-- The womb is a child entity of the player tagged "ln_womb" with a
-- MaterialInventoryComponent, created once in init.lua OnPlayerSpawned.

-----------------------------------------------------------------------------------------
-- FIND THE WOMB ENTITY
-----------------------------------------------------------------------------------------
--- Returns the womb entity ID (child of player tagged "ln_womb"), or nil.
function getWombEntity(player_id)
    if player_id == nil or player_id == 0 then return nil end
    for _, child in ipairs(EntityGetAllChildren(player_id) or {}) do
        if EntityHasTag(child, "ln_womb") then
            return child
        end
    end
    return nil
end

-----------------------------------------------------------------------------------------
-- GET MATERIAL COUNT  (matches DA's getMaterialCount signature)
-----------------------------------------------------------------------------------------
--- Returns the amount of `material` stored in `entity`'s MaterialInventoryComponent.
---
--- `material` can be:
---   • a material name string   → count of that single material
---   • "all"                    → sum of every material
---
--- `entity` defaults to the caller (GetUpdatedEntityID) when omitted,
--- but you will typically pass the womb entity explicitly.
function getWombMaterialCount(material, entity)
    local entity = entity or GetUpdatedEntityID()
    local mic = EntityGetFirstComponentIncludingDisabled(entity, "MaterialInventoryComponent")
    if mic == nil then return 0 end
    local counts = ComponentGetValue2(mic, "count_per_material_type")
    if counts == nil then return 0 end

    if material ~= "all" then
        local idx = CellFactory_GetType(material)
        if idx == nil or idx < 0 then return 0 end
        return tonumber(counts[idx + 1]) or 0
    end

    -- "all": sum everything
    local total = 0
    for _, v in pairs(counts) do
        if v ~= 0 then total = total + v end
    end
    return total
end

-----------------------------------------------------------------------------------------
-- LIST MATERIALS  (matches DA's IngestedMaterialsList)
-----------------------------------------------------------------------------------------
--- Returns { {name, amount}, {name, amount}, ... } for every material
--- with amount > 0 in the entity's MaterialInventoryComponent.
function getWombMaterialsList(entity)
    local entity = entity or GetUpdatedEntityID()
    local mic = EntityGetFirstComponentIncludingDisabled(entity, "MaterialInventoryComponent")
    if mic == nil then return {} end
    local counts = ComponentGetValue2(mic, "count_per_material_type")
    if counts == nil then return {} end

    local list = {}
    for k, v in pairs(counts) do
        if v ~= nil and v > 0 then
            local name = CellFactory_GetName(k - 1)
            table.insert(list, { name, v })
        end
    end
    return list
end

-----------------------------------------------------------------------------------------
-- ADD / SET MATERIAL  (thin wrapper over the engine call)
-----------------------------------------------------------------------------------------
--- Sets the amount of `material` in `entity` to exactly `amount`.
--- Use current + delta to add:
---   addWombMaterial(womb, "ln_cum", getWombMaterialCount("ln_cum", womb) + 120)
function setWombMaterial(entity, material, amount)
    if entity == nil then return end
    if amount < 0 then amount = 0 end
    AddMaterialInventoryMaterial(entity, material, amount)
end

--- Convenience: adds `delta` to the current count of `material`.
function addWombMaterial(entity, material, delta)
    if entity == nil or delta == 0 then return end
    local current = getWombMaterialCount(material, entity)
    setWombMaterial(entity, material, current + delta)
end

-----------------------------------------------------------------------------------------
-- FULLNESS STAGE HELPERS
-- Mirrors the stage thresholds used in leak_cum.lua so womb.lua and any
-- other script can derive stage/overfilled from a total without duplicating
-- the threshold constants.
-----------------------------------------------------------------------------------------

local _STAGE4  = 900
local _STAGE5  = 1500
local _STAGE6  = 1900
local _CUM_MAX = 2000

--- Returns the fullness stage (0–6) for a given total.
--- Stage meanings match leak_cum.lua:
---   0 = empty, 1–3 = filling up, 4 = 50% speed penalty,
---   5 = 75% speed penalty + damage chance, 6 = 95% speed penalty + guaranteed damage
function getWombStage(total)
    if     total > _STAGE6 then return 6
    elseif total > _STAGE5 then return 5
    elseif total > _STAGE4 then return 4
    elseif total > 500     then return 3
    elseif total > 250     then return 2
    elseif total > 0       then return 1
    else                        return 0 end
end

--- Returns true when total exceeds the overfilled threshold (>800).
function isWombOverfilled(total)
    return total > _CUM_MAX
end

--- Returns the overfilled threshold constant.
function getWombMax()
    return _CUM_MAX
end

--- Returns a short flavor description string for the current fullness level.
--- Matches the descriptions shown in the leak_cum status icon tooltip.
function getWombFullnessDescription(total)
    local stage      = getWombStage(total)
    local overfilled = isWombOverfilled(total)
    if     overfilled  then return "You most certainly don't feel like having sex. You're feeling so full you might pop."
    elseif stage >= 6  then return "You're feeling sickly full and dripping. Any more creampies will hurt you."
    elseif stage >= 5  then return "You're feeling sickly full. Any more creampies might hurt you."
    elseif stage >= 4  then return "You're really full of cum. Movement is difficult."
    elseif stage >= 3  then return "You're full of cum. Any more will make movement difficult."
    elseif stage >= 2  then return "You can still fit more cum inside of you."
    elseif stage >= 1  then return "You're almost empty."
    else                    return "" end
end
