-- Create font string to display the ShardID
local shardIDText = TimerFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal") -- Change "OVERLAY" to "HIGHLIGHT"
shardIDText:SetPoint("TOPLEFT", TimerFrame, "TOPLEFT", 2, -20)
shardIDText:SetText("ShardID: |cffff0000N/A|r") -- Start off red and ashamed

-- Store the current ShardID
local currentShardID = "N/A"

-- Add a mapping of zone names to abbreviations
local zoneAbbreviations = {
    ["Azj-Kahet"] = "AK",
    ["City of Threads"] = "AK", -- Treat as Azj-Kahet
    ["City of Threads - Lower"] = "AK", -- Treat as Azj-Kahet
    ["The Ringing Deeps"] = "RD",
    ["Hallowfall"] = "HF",
    ["Isle of Dorn"] = "ID",
    ["Siren Isle"] = "SI",
    ["Undermine"] = "UN"
}

-- Add a mapping of subzones to their parent zones
local subzoneToZone = {
    ["Slam Central Station"] = "Undermine",
    -- Add other subzones here if needed
}

-- Reset to player's ShardID (but don't set it to N/A)
local function ResetToPlayerShardID()
    local guid = UnitGUID("player")
    if guid then
        local segments = { strsplit("-", guid) }
        local id = segments[5]
        if id and id ~= currentShardID then
            SetShardID(id)
        end
    end
end

-- Modify the SetShardID function to announce the ShardID
local function SetShardID(id)
    local coloredID
    if id == "N/A" then
        coloredID = "|cffff0000" .. id .. "|r" -- Red, the color of regret
    else
        coloredID = "|cff00ff00" .. id .. "|r" -- Green, the color of success
    end

    -- Get the current zone or subzone name
    local zoneName = GetZoneText()
    local parentZone = subzoneToZone[zoneName] or zoneName -- Map subzone to parent zone if applicable
    local zoneAbbreviation = zoneAbbreviations[parentZone] or "" -- Get the abbreviation for the parent zone

    -- Update the text to include the zone abbreviation if available
    local displayText = "ShardID: " .. coloredID
    if zoneAbbreviation ~= "" then
        displayText = displayText .. " (" .. zoneAbbreviation .. ")"
    end

    shardIDText:SetText(displayText)

    -- Announce the ShardID to the party or raid if it has changed and is not "N/A"
    local validZones = { "AK", "RD", "HF", "ID", "SI", "UN" }
    local isValidZone = false
    for _, validZone in ipairs(validZones) do
        if zoneAbbreviation == validZone then
            isValidZone = true
            break
        end
    end

    if id ~= currentShardID and id ~= "N/A" and isValidZone then
        if IsInGroup() or IsInRaid() then
            local chatType = IsInRaid() and "RAID" or "PARTY"
            SendChatMessage("ShardID: " .. id .. (zoneAbbreviation ~= "" and " (" .. zoneAbbreviation .. ")" or ""), chatType)
        end
    end

    currentShardID = id
    BlackBoxDB = BlackBoxDB or {}
    BlackBoxDB.shardID = id
end

-- Reset to player's ShardID (but don't set it to N/A)
local function ResetToPlayerShardID()
    local guid = UnitGUID("player")
    if guid then
        local segments = { strsplit("-", guid) }
        local id = segments[5]
        if id and id ~= currentShardID then
            SetShardID(id)
        end
    end
end

-- Restore last saved ShardID on reload (yes, even your previous shame is preserved)
local frame = CreateFrame("Frame")
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", function(self, event, addonName)
    if addonName == "BlackBox" then
        BlackBoxDB = BlackBoxDB or {}
        local savedID = BlackBoxDB.shardID or "N/A"
        SetShardID(savedID)
    end
end)

-- Zone change = back to your personal ShardID, whatever that is
local zoneFrame = CreateFrame("Frame")
zoneFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
zoneFrame:SetScript("OnEvent", function()
    SetShardID("N/A") -- Reset ShardID to "N/A" in red
end)

-- Update on mob mouseover (the actual useful part of your life)
local mouseoverFrame = CreateFrame("Frame")
mouseoverFrame:SetScript("OnUpdate", function()
    if UnitExists("mouseover") and not UnitIsPlayer("mouseover") then
        local name = UnitName("mouseover")
        if name == "Oathsworn Peacekeeper" then
            return -- Ignore this NPC
        end
        local guid = UnitGUID("mouseover")
        if guid then
            local segments = { strsplit("-", guid) }
            local id = segments[5]
            if id and id ~= currentShardID then
                SetShardID(id)
            end
        end
    end
end)

-- Register an event for when the player's target changes
local targetFrame = CreateFrame("Frame")
targetFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
targetFrame:SetScript("OnEvent", function()
    if UnitExists("target") and not UnitIsPlayer("target") and not (UnitPlayerControlled("target") and not UnitInParty("target")) then
        local name = UnitName("target")
        if name == "Oathsworn Peacekeeper" then
            return -- Ignore this NPC
        end
        local guid = UnitGUID("target")
        if guid then
            local segments = { strsplit("-", guid) }
            local id = segments[5]
            if id and id ~= currentShardID then
                SetShardID(id)
            end
        end
    end
end)

-- First time setup: pretend like the player’s shard matters
ResetToPlayerShardID()
