-- WarBoard_AntiCheat.lua
-- Always-on World PvP integrity guards — active for the whole session, independent of any
-- objective, supply run, or whether the board is open.
--
-- Current guard: eject the player from the "Magical Snow Sled" the instant it's detected — whether
-- the game exposes it as the player's VEHICLE or as a PET. Event-driven for an instant response,
-- with a light periodic backstop so it can't be slipped past on a missed event.

local addonName, WB = ...
WB.AntiCheat = WB.AntiCheat or {}
local AC = WB.AntiCheat

local function TAG() return WB.TAG or "|cff33ff99WarBoard|r" end

-- Unit names ejected on sight. UnitName is LOCALIZED, so these match the client's language
-- (English here, matching the reported exploits). Extend as new ones are found.
local EJECT_ON_SIGHT = {
    ["Magical Snow Sled"] = true,
    ["Bench"]             = true,   -- Lorewalking bench
}

local lastWarnAt = 0
-- Check the player's vehicle/pet slots; eject + warn if either is a banned unit. Returns true on a hit.
function AC.CheckBannedVehicle()
    local v = UnitName("vehicle")
    local p = UnitName("pet")
    local hit = (v and EJECT_ON_SIGHT[v] and v) or (p and EJECT_ON_SIGHT[p] and p)
    if not hit then return false end
    if VehicleExit then pcall(VehicleExit) end
    local now = GetTime()
    if (now - lastWarnAt) > 2 then   -- one message per burst (the eject can take a beat to apply)
        lastWarnAt = now
        print((TAG() .. " %s detected \226\128\148 exiting the vehicle."):format(hit))
    end
    return true
end

-- ── Wiring ─────────────────────────────────────────────────────────────────────
local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
if f.RegisterUnitEvent then
    f:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", "player")   -- sled applied as a vehicle
    f:RegisterUnitEvent("UNIT_PET", "player")               -- sled applied as a pet
else
    f:RegisterEvent("UNIT_ENTERED_VEHICLE")
    f:RegisterEvent("UNIT_PET")
end
f:SetScript("OnEvent", function() AC.CheckBannedVehicle() end)

-- Light backstop for anything the events miss (e.g. an aura-applied sled that fires neither). The
-- check is two UnitName calls + a table lookup — negligible, so a 1s cadence is fine.
if C_Timer and C_Timer.NewTicker then
    C_Timer.NewTicker(1, AC.CheckBannedVehicle)
end
