if not(CBW_Battle) then return end
local ScriptLoaded = ScriptLoaded or false
if ScriptLoaded then return end
ScriptLoaded = true

local B = CBW_Battle
local cooldown = TICRATE*3/2

B.Action.Shielder = function(mo, doaction)
    local player = mo.player
    -- Properties
    player.actiontext = "Pseudo Shield"
	player.actionrings = 50

    local shield_cost = 50
    local shield_sound = sfx_lvldu 

    -- Initialize cooldown tracker if not already
    player.shield_cooldown = player.shield_cooldown or 0

    -- Ensure player is Miguel
    if player and player.mo and player.mo.skin == "miguel" then
        if doaction then
            if player.rings >= shield_cost and player.shield_cooldown <= 0 then

                -- Apply Pity shield to player
                player.powers[pw_shield] = SH_PITY

                -- Add ghost trail effect
                player.has_ghost_trail = true
                player.ghost_trail_interval = 2

                player.rings = player.rings - shield_cost
                S_StartSound(mo, shield_sound)
                player.shield_cooldown = cooldown
            end
        end
    end
end

addHook("ThinkFrame", function()
    for player in players.iterate do
        if player.mo and player.mo.valid then
            -- Initialize custom variables properly
            if player.shield_triggered == nil then player.shield_triggered = false end
            if player.has_ghost_trail == nil then player.has_ghost_trail = false end
            if player.ghost_trail_interval == nil then player.ghost_trail_interval = 0 end

            if player.spectator then continue end

            -- Ensure player is Miguel
            if player.mo.skin == "miguel" then
                if (player.cmd.buttons & BT_ATTACK) ~= 0 and not player.shield_triggered then
                    B.Action.Shielder(player.mo, true)
                end

                -- Update shield triggered state
                player.shield_triggered = (player.cmd.buttons & BT_ATTACK) ~= 0

                -- Decrement cooldown if any
                if player.shield_cooldown and player.shield_cooldown > 0 then
                    player.shield_cooldown = player.shield_cooldown - 1
                end

                -- Create ghost trail if player has Pity shield
                if player.powers[pw_shield] == SH_PITY and player.has_ghost_trail then
                    player.ghost_trail_interval = player.ghost_trail_interval - 1
                    if player.ghost_trail_interval <= 0 then
                        P_SpawnGhostMobj(player.mo)
                        player.ghost_trail_interval = 2 -- reset interval
                    end
                end

                -- Remove ghost trail if player loses the shield
                if player.powers[pw_shield] ~= SH_PITY and player.has_ghost_trail then
                    player.has_ghost_trail = false
                end
            end
        end
    end
end)

addHook("PlayerQuit", function(player)
    player.shield_triggered = false
    player.shield_cooldown = 0
    player.has_ghost_trail = false
end)
