PLUGIN.name = "Подразделение: модель и параметры"
PLUGIN.author = "Automated"
PLUGIN.description = "Устанавливает модель игрока, skin и bodygroups согласно его подразделению (Podr) из таблицы фракции."

if SERVER then
    local function applyPodrModel(client, character)
        if not IsValid(client) then return end
        if not character then character = client:GetCharacter() end
        if not character then return end

        local faction = client:Team()
        local factionTable = ix.faction.Get(faction)
        if not factionTable then return end

        local podrID = character:GetPodr()
        local specID = character:GetSpec()
        local podrTable = factionTable.Podr and factionTable.Podr[podrID]
        local specTable = factionTable.Spec and factionTable.Spec[specID]

        -- Determine source for model: prefer spec.model, fallback to podr.model
        local sourceModelTable = nil
        if specTable and specTable.model and type(specTable.model) == "string" and specTable.model ~= "" then
            sourceModelTable = specTable
        elseif podrTable and podrTable.model and type(podrTable.model) == "string" and podrTable.model ~= "" then
            sourceModelTable = podrTable
        end

        if sourceModelTable then
            pcall(function() client:SetModel(sourceModelTable.model) end)
        end

        -- Skin: prefer spec.skin, then podr.skin
        local skinSource = nil
        if specTable and specTable.skin then
            skinSource = specTable.skin
        elseif podrTable and podrTable.skin then
            skinSource = podrTable.skin
        end

        if skinSource then
            if isnumber(skinSource) then
                pcall(function() client:SetSkin(skinSource) end)
            elseif istable(skinSource) then
                local skinVal = nil
                if skinSource[client:Team()] then
                    skinVal = skinSource[client:Team()]
                else
                    for _, v in ipairs(skinSource) do
                        skinVal = v
                        break
                    end
                end
                if isnumber(skinVal) then
                    pcall(function() client:SetSkin(skinVal) end)
                end
            end
        end

        -- Bodygroups: merge spec.bodygroups over podr.bodygroups
        local mergedBodygroups = {}
        if podrTable and istable(podrTable.bodygroups) then
            for k, v in pairs(podrTable.bodygroups) do mergedBodygroups[k] = v end
        end
        if specTable and istable(specTable.bodygroups) then
            for k, v in pairs(specTable.bodygroups) do mergedBodygroups[k] = v end
        end

        timer.Simple(0.1, function()
            if not IsValid(client) then return end

            if mergedBodygroups and next(mergedBodygroups) then
                for k, v in pairs(mergedBodygroups) do
                    if isnumber(k) and isnumber(v) then
                        pcall(function() client:SetBodygroup(k, v) end)
                    elseif istable(v) and v.id and v.value then
                        pcall(function() client:SetBodygroup(v.id, v.value) end)
                    end
                end
            end
        end)
    end

    function PLUGIN:CharacterLoaded(character)
        local client = character:GetPlayer()
        if IsValid(client) then
            applyPodrModel(client, character)
        end
    end

    -- Apply on spawn too (in case Helix or gamemode overwrote model earlier)
    function PLUGIN:PlayerSpawn(client)
        local character = client:GetCharacter()
        if character then
            -- delay a tick to let other PlayerSpawn logic run
            timer.Simple(0, function()
                if IsValid(client) then
                    applyPodrModel(client, character)
                end
            end)
        end
    end
end

if CLIENT then
    -- nothing client-side needed for now, but keep file shared for simplicity
end

-- Документация/пример для FACTION.Podr:
-- FACTION.Podr = {
--   [1] = { name = "Новоприбывшие", model = "models/player/group01/male_04.mdl", skin = 0, bodygroups = { [1] = 1, {id = 2, value = 0} } },
-- }
