local function BuildAWCEvents()
    return {
        {
            day = 1,
            start = time({ year = 2026, month = 4, day = 10, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 4, day = 10, hour = 23, min = 59, sec = 59 }),
            preText = "Next AWC on ",
            todayText = "AWC Cup 1 Today at ",
            display = "AWC Cup 1 - Apr 10-12, 2026 | 19:00 CEST",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 2,
            start = time({ year = 2026, month = 4, day = 11, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 4, day = 11, hour = 23, min = 59, sec = 59 }),
            todayText = "Day 2 of AWC Cup 1 Today at ",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 3,
            start = time({ year = 2026, month = 4, day = 12, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 4, day = 12, hour = 23, min = 59, sec = 59 }),
            todayText = "Final Day of AWC Cup 1 Today at ",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 4,
            start = time({ year = 2026, month = 4, day = 17, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 4, day = 17, hour = 23, min = 59, sec = 59 }),
            todayText = "AWC Cup 2 Today at ",
            display = "AWC Cup 2 - Apr 17-19, 2026 | 19:00 CEST",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 5,
            start = time({ year = 2026, month = 4, day = 18, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 4, day = 18, hour = 23, min = 59, sec = 59 }),
            todayText = "Day 2 of AWC Cup 2 Today at ",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 6,
            start = time({ year = 2026, month = 4, day = 19, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 4, day = 19, hour = 23, min = 59, sec = 59 }),
            todayText = "Final Day of AWC Cup 2 Today at ",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 7,
            start = time({ year = 2026, month = 5, day = 1, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 5, day = 1, hour = 23, min = 59, sec = 59 }),
            todayText = "AWC Cup 3 Today at ",
            display = "AWC Cup 3 - May 1-3, 2026 | 19:00 CEST",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 8,
            start = time({ year = 2026, month = 5, day = 2, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 5, day = 2, hour = 23, min = 59, sec = 59 }),
            todayText = "Day 2 of AWC Cup 3 Today at ",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 9,
            start = time({ year = 2026, month = 5, day = 3, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 5, day = 3, hour = 23, min = 59, sec = 59 }),
            todayText = "Final Day of AWC Cup 3 Today at ",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 10,
            start = time({ year = 2026, month = 6, day = 5, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 6, day = 5, hour = 23, min = 59, sec = 59 }),
            todayText = "AWC Gauntlet Today at ",
            display = "AWC Gauntlet - Jun 5, 2026 | 19:00 CEST",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 11,
            start = time({ year = 2026, month = 6, day = 6, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 6, day = 6, hour = 23, min = 59, sec = 59 }),
            todayText = "AWC Season 1 Finals Today at ",
            display = "AWC Season 1 Finals - Jun 6-7, 2026 | 19:00 CEST",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 12,
            start = time({ year = 2026, month = 6, day = 7, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 6, day = 7, hour = 23, min = 59, sec = 59 }),
            todayText = "Day 2 of AWC Season 1 Finals Today at ",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 13,
            start = time({ year = 2026, month = 9, day = 12, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 9, day = 12, hour = 23, min = 59, sec = 59 }),
            todayText = "AWC Grand Finals Today at ",
            display = "BlizzCon Grand Finals - Sep 12-13, 2026 | 19:00 CEST",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
        {
            day = 14,
            start = time({ year = 2026, month = 9, day = 13, hour = 0, min = 0, sec = 0 }),
            finish = time({ year = 2026, month = 9, day = 13, hour = 23, min = 59, sec = 59 }),
            todayText = "Day 2 of AWC Grand Finals Today at ",
            timeByRegion = {
                [1] = "10:00 PDT",
                [2] = "02:00 KST",
                [3] = "19:00 CEST",
                [4] = "01:00 CST",
            },
        },
    }
end

RSTATS.AWCEvents = BuildAWCEvents()

function RSTATS:GetAWCAnnouncement()
    local now = time()
    local region = (GetCurrentRegion and GetCurrentRegion()) or 3

    for i, event in ipairs(self.AWCEvents or {}) do
        local timeText = (event.timeByRegion and event.timeByRegion[region]) or "TBC"

        if now < event.start then
            if i == 1 and event.preText then
                return event.preText .. date("%d %b %Y", event.start) .. " at " .. timeText
            end
            return "Next AWC on " .. date("%d %b %Y", event.start) .. " at " .. timeText
        end

        if now >= event.start and now <= event.finish then
            return (event.todayText or "AWC Today at ") .. timeText
        end
    end

    return nil
end

function RSTATS:GetAWCBannerText()
    local now = time()
    local region = (GetCurrentRegion and GetCurrentRegion()) or 3

    for _, event in ipairs(self.AWCEvents or {}) do
        local timeText = (event.timeByRegion and event.timeByRegion[region]) or "TBC"

        if now < event.start then
            return {
                left = "Next AWC on",
                right = " " .. date("%d %b %Y", event.start) .. " | " .. timeText,
            }
        end

        if now >= event.start and now <= event.finish then
            return {
                left = (event.todayText or "AWC Today at "):gsub("%s+$", ""),
                right = " " .. timeText,
            }
        end
    end

    return {
        left = "Next AWC on",
        right = " TBC",
    }
end

function RSTATS:GetNextAWCDisplay()
    local now = time()
    local region = (GetCurrentRegion and GetCurrentRegion()) or 3

    for _, event in ipairs(self.AWCEvents or {}) do
        local timeText = (event.timeByRegion and event.timeByRegion[region]) or "TBC"

        if now <= event.finish then
            if event.display then
                return event.display
            end

            return date("%d %b %Y", event.start) .. " | " .. timeText
        end
    end

    return "TBC"
end

local function GetAWCDateKey(ts)
    local t = date("*t", ts or time())
    return string.format("%04d-%02d-%02d", t.year, t.month, t.day)
end

function RSTATS:MaybePrintDailyAWCMessage()
    if not Database or type(Database) ~= "table" then
        return
    end

    local msg = self:GetAWCAnnouncement()
    if not msg or msg == "" then
        return
    end

    local todayKey = GetAWCDateKey()
    if Database.lastAWCAnnouncementDate == todayKey then
        return
    end

    DEFAULT_CHAT_FRAME:AddMessage(
        self:ColorText("Rated Stats: ") .. "|cffffffff" .. msg .. "|r"
    )

    Database.lastAWCAnnouncementDate = todayKey

    if SaveData then
        SaveData()
    end
end

local awcFrame = CreateFrame("Frame")
awcFrame:RegisterEvent("PLAYER_LOGIN")
awcFrame:SetScript("OnEvent", function()
    C_Timer.After(2, function()
        if RSTATS and RSTATS.MaybePrintDailyAWCMessage then
            RSTATS:MaybePrintDailyAWCMessage()
        end
    end)
end)
