-- EbonholdHub: modules/ui/MainNav.lua
-- Site-style top navigation bar — floats above the main panel (same as /ebh sitedemo).

EbonholdHub.MainNav = EbonholdHub.MainNav or {}

local MN  = EbonholdHub.MainNav
local ST  = EbonholdHub.SiteTheme
local SW  = EbonholdHub.SiteWidgets
local C   = ST.C
local L   = ST.Layout

local navFrame
local anchorFrame
local navState = { activeId = "myBuild", registry = {} }

local NAV_LAYOUT_V = 4

local ACTION_BTN_H = 28
local ACTION_BTN_TOP = -math.floor((L.NAV_H - ACTION_BTN_H) / 2)

local function DestroyNavFrame()
    if navFrame then
        navFrame:Hide()
        navFrame = nil
    end
    if _G.EbonholdHubMainNav then
        _G.EbonholdHubMainNav:Hide()
        _G.EbonholdHubMainNav = nil
    end
    navState.registry = {}
end

local NAV_ROUTES = {
    catalog  = "publicBuilds",
    echoMap  = "echoMap",
    myBuild  = "buildOverview",
}

local NAV_ITEMS = {
    { id = "myBuild", label = "My Build", icon = "builds" },
    { id = "catalog", label = "Catalog", icon = "catalog" },
    { id = "echoMap", label = "Echo Map", icon = "external" },
}

local ACTION_ITEMS = {
    { id = "newBuild", label = "New Build", icon = "builds", accent = true },
    { id = "quickCreate", label = "Quick Create", icon = "import", accent = true, danger = true },
    { id = "report", label = "Report bug", icon = "report" },
}

local function PositionNav()
    if navFrame and anchorFrame and anchorFrame:IsShown() then
        navFrame:ClearAllPoints()
        navFrame:SetPoint("BOTTOMLEFT", anchorFrame, "TOPLEFT", 0, 6)
    end
end

local function RunAction(item)
    if item.id == "newBuild" then
        EbonholdHub.ViewRouter.Show("buildTabs", { mode = "create" })
    elseif item.id == "quickCreate" then
        if EbonholdHub.BuildList and EbonholdHub.BuildList.CreateFromCharacter then
            EbonholdHub.BuildList.CreateFromCharacter()
        end
    elseif item.id == "report" then
        if EbonholdHub.BugReport and EbonholdHub.BugReport.OpenWebsite then
            EbonholdHub.BugReport.OpenWebsite()
        end
    end
end

local function CreateActionButton(parent, item)
    local variant = "default"
    if item.accent and item.danger then
        variant = "danger"
    elseif item.accent then
        variant = "accent"
    end
    return SW.CreateNavActionButton(parent, {
        label = item.label,
        icon = item.icon,
        variant = variant,
        onClick = function() RunAction(item) end,
    })
end

function MN.SetActiveId(navId)
    navState.activeId = navId or "myBuild"
    if navState.registry then
        for _, link in pairs(navState.registry) do
            if link.ApplyState then link.ApplyState() end
        end
    end
end

function MN.Position()
    PositionNav()
end

function MN.Show()
    if navFrame then
        navFrame:Show()
        PositionNav()
    end
end

function MN.Hide()
    if navFrame then navFrame:Hide() end
end

function MN.Create(panelFrame, opts)
    opts = opts or {}
    anchorFrame = panelFrame

    if navFrame and navFrame._navLayoutV == NAV_LAYOUT_V then
        return navFrame
    end
    DestroyNavFrame()

    navState.onSelect = function(id)
        local route = NAV_ROUTES[id]
        if not route then return end
        if route == "buildOverview" then
            local active = EbonholdHub.Build and EbonholdHub.Build.GetActive()
            if active then
                EbonholdHub.ViewRouter.Show("buildOverview", { build = active })
            else
                EbonholdHub.ViewRouter.Show("welcome")
            end
        else
            EbonholdHub.ViewRouter.Show(route)
        end
        MN.SetActiveId(id)
    end

    navFrame = CreateFrame("Frame", "EbonholdHubMainNav", UIParent)
    navFrame:SetSize(L.WIN_W, L.NAV_H)
    navFrame:SetFrameStrata(panelFrame:GetFrameStrata())
    navFrame:SetFrameLevel(panelFrame:GetFrameLevel() + 2)
    navFrame:Hide()

    SW.Fill(navFrame, C.navBg)
    SW.ThinBorder(navFrame, C.border, 1)

    local accentStripe = navFrame:CreateTexture(nil, "ARTWORK")
    accentStripe:SetTexture(ST.FLAT)
    accentStripe:SetVertexColor(unpack(C.accent))
    accentStripe:SetWidth(4)
    accentStripe:SetPoint("TOPLEFT", 0, 0)
    accentStripe:SetPoint("BOTTOMLEFT", 0, 0)

    local glowParent = CreateFrame("Frame", nil, navFrame)
    glowParent:SetPoint("TOPLEFT", 0, 0)
    glowParent:SetPoint("BOTTOMLEFT", 0, 0)
    glowParent:SetWidth(10)
    SW.AttachAccentGlow(glowParent, 10)

    local logoArea = CreateFrame("Frame", nil, navFrame)
    logoArea:SetPoint("LEFT", accentStripe, "RIGHT", 10, 0)
    logoArea:SetSize(150, L.NAV_H)
    logoArea:EnableMouse(true)
    logoArea:SetScript("OnMouseDown", function(_, button)
        if button == "LeftButton" and anchorFrame then
            anchorFrame:StartMoving()
        end
    end)
    logoArea:SetScript("OnMouseUp", function()
        if anchorFrame then anchorFrame:StopMovingOrSizing() end
    end)

    local logoText = SW.Label(logoArea, "|cffebebf0Ebonhold|r|cff4dbff2Hub|r", 14, C.text, false, "semibold")
    logoText:SetPoint("LEFT", 0, 0)

    local navRow = CreateFrame("Frame", nil, navFrame)
    navRow:SetPoint("LEFT", logoArea, "RIGHT", 12, 0)
    navRow:SetPoint("RIGHT", navFrame, "RIGHT", -360, 0)
    navRow:SetHeight(L.NAV_H)
    navRow:SetFrameLevel(navFrame:GetFrameLevel() + 2)

    navState.registry = {}
    local prev
    for _, item in ipairs(NAV_ITEMS) do
        local link = SW.CreateNavLink(navRow, item, navState)
        link:SetPoint("TOP", navRow, "TOP", 0, 0)
        if prev then
            link:SetPoint("LEFT", prev, "RIGHT", 4, 0)
        else
            link:SetPoint("LEFT", navRow, "LEFT", 0, 0)
        end
        prev = link
        navState.registry[item.id] = link
    end

    local actions = CreateFrame("Frame", nil, navFrame)
    actions:SetPoint("RIGHT", navFrame, "RIGHT", opts.onClose and -44 or -12, 0)
    actions:SetPoint("TOP", navFrame, "TOP", 0, 0)
    actions:SetHeight(L.NAV_H)
    actions:SetFrameLevel(navFrame:GetFrameLevel() + 2)

    prev = nil
    for i = #ACTION_ITEMS, 1, -1 do
        local item = ACTION_ITEMS[i]
        local btn = CreateActionButton(actions, item)
        btn:SetPoint("TOP", actions, "TOP", 0, ACTION_BTN_TOP)
        if prev then
            btn:SetPoint("RIGHT", prev, "LEFT", -6, 0)
        else
            btn:SetPoint("RIGHT", actions, "RIGHT", 0, 0)
        end
        prev = btn
    end

    if opts.onClose then
        local closeBtn = SW.CreateCloseButton(navFrame, opts.onClose, 28)
        closeBtn:SetPoint("RIGHT", navFrame, "RIGHT", -10, 0)
        closeBtn:SetPoint("TOP", navFrame, "TOP", 0, ACTION_BTN_TOP)
        closeBtn:SetFrameLevel(navFrame:GetFrameLevel() + 4)
    end

    panelFrame:HookScript("OnShow", PositionNav)
    panelFrame:HookScript("OnHide", function()
        if navFrame then navFrame:Hide() end
    end)

    navFrame._navLayoutV = NAV_LAYOUT_V
    MN.SetActiveId(navState.activeId)

    return navFrame
end
