import re

path = r"C:\Users\Norme\Documents\verdent-projects\new-project\GuildDeathLog\Modules\UI.lua"

with open(path, "r", encoding="utf-8") as f:
    lines = f.readlines()

# Find CreateOverlay start and end
start_line = None
end_line = None
for i, line in enumerate(lines):
    if "function UI:CreateOverlay()" in line and start_line is None:
        start_line = i
    if start_line is not None and i > start_line + 5:
        if line.strip() == "end" and end_line is None:
            # Check next non-empty line is a function or comment
            for j in range(i+1, min(i+5, len(lines))):
                stripped = lines[j].strip()
                if stripped:
                    if stripped.startswith("function") or stripped.startswith("--") or stripped.startswith("local"):
                        end_line = i
                    break
        if end_line is not None:
            break

print(f"CreateOverlay: lines {start_line+1} to {end_line+1}")

NEW_CODE = r"""function UI:CreateOverlay()
    local W, H = 660, 460
    local CX   = 60
    local CTY  = -130
    local CBY  = 55
    local CW   = W - CX * 2

    local COL1_X = CX + 5
    local COL2_X = CX + 180
    local COL3_X = CX + 363
    local COL4_X = CX + 455
    local COL1_W = 172
    local COL2_W = 178
    local COL3_W = 88
    local COL4_W = 85

    local o = CreateFrame("Frame", "GDLDeathOverlay", UIParent)
    o:SetSize(W, H)
    o:SetFrameStrata("FULLSCREEN_DIALOG")
    o:SetFrameLevel(500)
    o:Hide()

    local scale = math.max(0.85, GuildDeathLogDB.settings.overlayScale or 1.0)
    o:SetScale(scale)
    o.currentScale = scale
    GuildDeathLogDB.settings.overlayScale = scale
    local pos = GuildDeathLogDB.settings.overlayPosition
    if pos and pos.point then
        o:SetPoint(pos.point, UIParent, pos.relPoint, pos.x, pos.y)
    else
        o:SetPoint("TOP", 0, -80)
    end

    local bgBase = o:CreateTexture(nil, "BACKGROUND", nil, -8)
    bgBase:SetAllPoints()
    bgBase:SetTexture("Interface\\AddOns\\GuildDeathLog\\Textures\\GDLDeathFrame_New.png")

    local classGrad = o:CreateTexture(nil, "BACKGROUND", nil, -6)
    classGrad:SetPoint("TOPLEFT",  CX,  CTY)
    classGrad:SetPoint("TOPRIGHT", -CX, CTY)
    classGrad:SetHeight(90)
    classGrad:SetTexture("Interface\\Buttons\\WHITE8x8")
    classGrad:SetBlendMode("ADD")
    classGrad:SetAlpha(0)
    o.classGrad = classGrad

    local closeBtn = CreateFrame("Button", nil, o)
    closeBtn:SetSize(22, 22)
    closeBtn:SetPoint("TOPRIGHT", -62, -66)
    closeBtn:SetAlpha(0.55)
    local closeTex = closeBtn:CreateTexture(nil, "OVERLAY")
    closeTex:SetAllPoints()
    closeTex:SetTexture("Interface\\Buttons\\UI-StopButton")
    closeTex:SetVertexColor(0.95, 0.35, 0.35)
    closeBtn:SetScript("OnEnter", function(self) self:SetAlpha(1.0) end)
    closeBtn:SetScript("OnLeave", function(self) self:SetAlpha(0.55) end)
    closeBtn:SetScript("OnClick", function()
        if o.hideTimer then o.hideTimer:Cancel() end
        if o.outroAnim then o.outroAnim:Stop(); o.outroAnim:Play()
        else o:Hide(); C_Timer.After(0.1, function() UI:ShowNextOverlay() end) end
    end)

    local function makeSep(yOff, alpha)
        local s = o:CreateTexture(nil, "ARTWORK")
        s:SetPoint("TOPLEFT",  o, "TOPLEFT",  CX + 10, yOff)
        s:SetPoint("TOPRIGHT", o, "TOPRIGHT", -CX - 10, yOff)
        s:SetHeight(1)
        s:SetTexture("Interface\\Buttons\\WHITE8x8")
        s:SetVertexColor(0.72, 0.55, 0.12, alpha or 0.50)
        return s
    end
    makeSep(CTY - 4)

    local HDR_Y = CTY - 18
    local function makeHdr(txt, xL, w, j)
        local fs = o:CreateFontString(nil, "ARTWORK")
        fs:SetFont("Fonts\\FRIZQT__.TTF", 11, "OUTLINE")
        fs:SetPoint("TOPLEFT", o, "TOPLEFT", xL, HDR_Y)
        fs:SetWidth(w)
        fs:SetJustifyH(j or "LEFT")
        fs:SetText(txt)
        fs:SetTextColor(0.88, 0.72, 0.22)
        fs:SetShadowOffset(1, -1)
        fs:SetShadowColor(0, 0, 0, 1)
        return fs
    end
    makeHdr("Name",               COL1_X, COL1_W, "LEFT")
    makeHdr("Ursache des Todes",  COL2_X, COL2_W, "LEFT")
    makeHdr("Uhrzeit",            COL3_X, COL3_W, "LEFT")
    makeHdr("Zone",               COL4_X, COL4_W, "LEFT")

    local SEP2_Y = CTY - 34
    local sep2 = makeSep(SEP2_Y)
    o.sep2 = sep2

    local ROW_Y = CTY - 36
    local ROW_H = 56

    local rowBg = o:CreateTexture(nil, "BACKGROUND", nil, -4)
    rowBg:SetPoint("TOPLEFT",  o, "TOPLEFT",  CX + 5, ROW_Y)
    rowBg:SetPoint("TOPRIGHT", o, "TOPRIGHT", -CX - 5, ROW_Y)
    rowBg:SetHeight(ROW_H)
    rowBg:SetTexture("Interface\\Buttons\\WHITE8x8")
    rowBg:SetVertexColor(0.04, 0.02, 0.005, 0.50)
    o.rowBg = rowBg

    local iconGlow = o:CreateTexture(nil, "ARTWORK")
    iconGlow:SetSize(60, 60)
    iconGlow:SetPoint("TOPLEFT", o, "TOPLEFT", COL1_X - 2, ROW_Y - 4)
    iconGlow:SetTexture("Interface\\Cooldown\\star4")
    iconGlow:SetBlendMode("ADD")
    iconGlow:SetVertexColor(0.90, 0.70, 0.18, 0.40)
    o.iconGlow = iconGlow

    local iconBacking = o:CreateTexture(nil, "ARTWORK")
    iconBacking:SetSize(44, 44)
    iconBacking:SetPoint("TOPLEFT", o, "TOPLEFT", COL1_X, ROW_Y - 7)
    iconBacking:SetTexture("Interface\\Buttons\\WHITE8x8")
    iconBacking:SetVertexColor(0.05, 0.02, 0.01, 0.82)
    o.iconBacking = iconBacking

    local iconBorder = o:CreateTexture(nil, "OVERLAY")
    iconBorder:SetSize(48, 48)
    iconBorder:SetPoint("TOPLEFT", o, "TOPLEFT", COL1_X - 2, ROW_Y - 5)
    iconBorder:SetTexture("Interface\\Buttons\\UI-Quickslot2")
    iconBorder:SetVertexColor(0.90, 0.72, 0.20, 0.85)
    o.iconBorder = iconBorder

    o.classIcon = o:CreateTexture(nil, "OVERLAY")
    o.classIcon:SetSize(40, 40)
    o.classIcon:SetPoint("TOPLEFT", o, "TOPLEFT", COL1_X + 2, ROW_Y - 9)

    do
        local pf = CreateFrame("Frame", nil, o)
        local t  = 0
        pf:SetScript("OnUpdate", function(_, dt)
            if not o:IsShown() then return end
            t = t + dt
            if o.iconGlow then o.iconGlow:SetAlpha(0.24 + 0.22 * math.sin(t * 2.0)) end
        end)
    end

    o.nameText = o:CreateFontString(nil, "OVERLAY")
    o.nameText:SetFont("Fonts\\MORPHEUS.TTF", 18, "OUTLINE")
    o.nameText:SetPoint("TOPLEFT", o, "TOPLEFT", COL1_X + 48, ROW_Y - 5)
    o.nameText:SetWidth(COL1_W - 52)
    o.nameText:SetJustifyH("LEFT")
    o.nameText:SetShadowOffset(2, -2)
    o.nameText:SetShadowColor(0, 0, 0, 1)

    o.classText = o:CreateFontString(nil, "OVERLAY")
    o.classText:SetFont("Fonts\\FRIZQT__.TTF", 9, "OUTLINE")
    o.classText:SetPoint("TOPLEFT", o, "TOPLEFT", COL1_X + 49, ROW_Y - 28)
    o.classText:SetWidth(COL1_W - 52)
    o.classText:SetJustifyH("LEFT")
    o.classText:SetTextColor(0.88, 0.80, 0.55)
    o.classText:SetShadowOffset(1, -1)
    o.classText:SetShadowColor(0, 0, 0, 1)

    o.killerText = o:CreateFontString(nil, "OVERLAY")
    o.killerText:SetFont("Fonts\\FRIZQT__.TTF", 13, "OUTLINE")
    o.killerText:SetPoint("TOPLEFT", o, "TOPLEFT", COL2_X, ROW_Y - 7)
    o.killerText:SetWidth(COL2_W)
    o.killerText:SetJustifyH("LEFT")
    o.killerText:SetTextColor(1.0, 0.40, 0.22)
    o.killerText:SetShadowOffset(1, -1)
    o.killerText:SetShadowColor(0, 0, 0, 1)

    o.spellText = o:CreateFontString(nil, "OVERLAY")
    o.spellText:SetFont("Fonts\\FRIZQT__.TTF", 9, "OUTLINE")
    o.spellText:SetPoint("TOPLEFT", o, "TOPLEFT", COL2_X, ROW_Y - 26)
    o.spellText:SetWidth(COL2_W)
    o.spellText:SetJustifyH("LEFT")
    o.spellText:SetTextColor(0.82, 0.68, 0.42)
    o.spellText:SetShadowOffset(1, -1)
    o.spellText:SetShadowColor(0, 0, 0, 1)

    o.timestampText = o:CreateFontString(nil, "OVERLAY")
    o.timestampText:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
    o.timestampText:SetPoint("TOPLEFT", o, "TOPLEFT", COL3_X, ROW_Y - 14)
    o.timestampText:SetWidth(COL3_W)
    o.timestampText:SetJustifyH("LEFT")
    o.timestampText:SetTextColor(0.82, 0.76, 0.55)
    o.timestampText:SetShadowOffset(1, -1)
    o.timestampText:SetShadowColor(0, 0, 0, 1)

    o.zoneText = o:CreateFontString(nil, "OVERLAY")
    o.zoneText:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
    o.zoneText:SetPoint("TOPLEFT", o, "TOPLEFT", COL4_X, ROW_Y - 14)
    o.zoneText:SetWidth(COL4_W)
    o.zoneText:SetJustifyH("LEFT")
    o.zoneText:SetTextColor(0.55, 0.88, 1.00)
    o.zoneText:SetShadowOffset(1, -1)
    o.zoneText:SetShadowColor(0, 0, 0, 1)

    local sep3 = makeSep(ROW_Y - ROW_H)
    o.divider = sep3

    local LW_Y = ROW_Y - ROW_H - 14

    local lwLabel = o:CreateFontString(nil, "ARTWORK")
    lwLabel:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE")
    lwLabel:SetPoint("TOPLEFT", o, "TOPLEFT", CX, LW_Y)
    lwLabel:SetWidth(CW)
    lwLabel:SetJustifyH("CENTER")
    lwLabel:SetText("~ Letzte Worte ~")
    lwLabel:SetTextColor(0.65, 0.50, 0.18)
    lwLabel:SetShadowOffset(1, -1)
    lwLabel:SetShadowColor(0, 0, 0, 1)
    o.lwLabel = lwLabel

    o.lastWordsText = o:CreateFontString(nil, "OVERLAY")
    o.lastWordsText:SetFont("Fonts\\FRIZQT__.TTF", 13, "OUTLINE")
    o.lastWordsText:SetPoint("TOPLEFT", o, "TOPLEFT", CX + 10, LW_Y - 18)
    o.lastWordsText:SetWidth(CW - 20)
    o.lastWordsText:SetJustifyH("CENTER")
    o.lastWordsText:SetTextColor(0.95, 0.88, 0.65)
    o.lastWordsText:SetShadowOffset(1, -1)
    o.lastWordsText:SetShadowColor(0, 0, 0, 1)

    local alliIcon = o:CreateTexture(nil, "ARTWORK")
    alliIcon:SetSize(40, 40)
    alliIcon:SetPoint("BOTTOMLEFT", o, "BOTTOMLEFT", CX + 2, CBY + 2)
    alliIcon:SetTexture("Interface\\FactionFrame\\PVP-Currency-Alliance")
    alliIcon:SetVertexColor(0.80, 0.85, 1.0, 0.65)

    local hordeIcon = o:CreateTexture(nil, "ARTWORK")
    hordeIcon:SetSize(40, 40)
    hordeIcon:SetPoint("BOTTOMRIGHT", o, "BOTTOMRIGHT", -CX - 2, CBY + 2)
    hordeIcon:SetTexture("Interface\\FactionFrame\\PVP-Currency-Horde")
    hordeIcon:SetVertexColor(1.0, 0.82, 0.75, 0.65)

    o.levelText = o:CreateFontString(nil, "OVERLAY")
    o.levelText:SetFont("Fonts\\FRIZQT__.TTF", 11, "OUTLINE")
    o.levelText:SetPoint("BOTTOM", o, "BOTTOM", 0, CBY + 14)
    o.levelText:SetWidth(CW - 110)
    o.levelText:SetJustifyH("CENTER")
    o.levelText:SetTextColor(0.85, 0.72, 0.35)
    o.levelText:SetShadowOffset(1, -1)
    o.levelText:SetShadowColor(0, 0, 0, 1)

    local scaleText = o:CreateFontString(nil, "OVERLAY")
    scaleText:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
    scaleText:SetPoint("BOTTOM", 0, -24)
    scaleText:SetTextColor(1, 0.8, 0.3)
    scaleText:Hide()
    o.scaleText = scaleText

    o:SetMovable(true)
    o:EnableMouse(true)
    o:RegisterForDrag("LeftButton")

    local isDragging = false
    local isScaling  = false
    local startX, startY, startScale

    o:SetScript("OnMouseDown", function(self, button)
        if button == "RightButton" then
            if self.hideTimer then self.hideTimer:Cancel() end
            if self.outroAnim then self.outroAnim:Stop(); self.outroAnim:Play()
            else self:Hide(); C_Timer.After(0.2, function() UI:ShowNextOverlay() end) end
        elseif button == "LeftButton" then
            if IsShiftKeyDown() then
                isScaling  = true
                isDragging = false
                startX, startY = GetCursorPosition()
                startScale = self.currentScale or 1.0
                self:SetScript("OnUpdate", function(self)
                    if not IsMouseButtonDown("LeftButton") then
                        isScaling = false
                        self:SetScript("OnUpdate", nil)
                        GuildDeathLogDB.settings.overlayScale = self.currentScale
                        if self.scaleText then C_Timer.After(1.2, function() if self.scaleText then self.scaleText:Hide() end end) end
                        return
                    end
                    if not isScaling then self:SetScript("OnUpdate", nil); return end
                    local curX, curY = GetCursorPosition()
                    local newScale = math.max(0.85, math.min(2.5, startScale + (curY - startY) / 100))
                    self:SetScale(newScale)
                    self.currentScale = newScale
                    if self.scaleText then
                        self.scaleText:SetText(math.floor(newScale * 100) .. "%")
                        self.scaleText:Show()
                    end
                end)
            else
                isDragging = true
                isScaling  = false
                self:StartMoving()
            end
        end
    end)

    o:SetScript("OnMouseUp", function(self, button)
        if button == "LeftButton" then
            if isScaling then
                isScaling = false
                self:SetScript("OnUpdate", nil)
                GuildDeathLogDB.settings.overlayScale = self.currentScale
                if self.scaleText then C_Timer.After(1, function() if self.scaleText then self.scaleText:Hide() end end) end
            end
            if isDragging then
                isDragging = false
                self:StopMovingOrSizing()
                local point, _, relPoint, x, y = self:GetPoint()
                GuildDeathLogDB.settings.overlayPosition = { point=point, relPoint=relPoint, x=x, y=y }
            end
        end
    end)

    self.overlay = o
end
"""

new_lines = lines[:start_line] + [NEW_CODE] + lines[end_line+1:]

with open(path, "w", encoding="utf-8") as f:
    f.writelines(new_lines)

print(f"Replaced CreateOverlay (lines {start_line+1}-{end_line+1}) with new table-style design.")
print(f"New file: {len(new_lines)} lines")
