---@diagnostic disable: undefined-field, undefined-global
---@class Private
local Private = select(2, ...)

-- EXPORT-01 group export: client-side, comms-free dump of the CURRENT LIVE raid/party
-- into paste-ready "name, realm, class, spec, role" lines (invitelist authoring format).
-- Ships in every packaged build (see WildThings.toc, before the do-not-package boundary)
-- and reads NOTHING from the dev-only Roster tier (the battletag-bearing roster-plan
-- files excluded from the packaged release ZIP) -- this file must stay independent
-- of every file in that tier.
--
-- Private.* deps: Private:IterateGroupMembers (Utility.lua, raid+party unified iterator --
-- deliberately used instead of the raid-only roster API, which would silently
-- return nothing in a 5-man party), Private:CreatePopupWindowWithButton() /
-- Private:CreateEditBox() (shared vararg-table widget constructors, UI/Widgets/PopupWindows,
-- UI/Widgets/EditBox.lua).
--
-- Other members' spec is intentionally left blank: the group inspect API is async/unreliable
-- for anyone but the player, so only the player's own row gets a spec value (best-effort,
-- per 26-CONTEXT.md).

-- Builds the export line array. Module-local (not exposed on Private) since
-- Private:BuildGroupExportString() is the sole consumer (IN-01, 26-REVIEW.md:
-- ShowGroupExportPopup now routes through BuildGroupExportString instead of
-- calling this directly).
local function BuildExportLines()
    local lines = {}
    local ownRealm = GetNormalizedRealmName() or ""
    local ownSpecName = select(2, GetSpecializationInfo(GetSpecialization()))

    for unit in Private:IterateGroupMembers() do
        -- UnitName returns TWO values: name, realm. realm is nil on the player's own
        -- realm (edge 6), so fall back to ownRealm to keep the realm column populated.
        local name, realm = UnitName(unit)

        if name then
            local _, classFileName = UnitClass(unit)
            local role = UnitGroupRolesAssigned(unit)
            -- Own-spec only (best-effort) -- other members' spec is left blank rather
            -- than guessed, since cross-realm/other-unit inspect data is async-only.
            -- unit == "player" only matches the party/solo iteration branch; use
            -- UnitIsUnit so this also matches the player's own raidN token while
            -- actually in a raid (WR-01, 26-REVIEW.md).
            local spec = UnitIsUnit(unit, "player") and (ownSpecName or "") or ""

            table.insert(lines, table.concat(
                { name, realm or ownRealm, classFileName or "", spec, role or "" }, ", "))
        end
    end

    return lines
end

-- Public: returns the full export as a single newline-joined string.
function Private:BuildGroupExportString()
    return table.concat(BuildExportLines(), "\n")
end

-- Build-once popup (mirrors the roster-screen import popup's reuse idiom), reused on
-- every subsequent /wt export call.
local exportPopup

-- Public: opens a copyable popup listing the current live raid/party roster, or
-- prints a friendly message and opens nothing when solo/not grouped (edge 5).
function Private:ShowGroupExportPopup()
    -- Guard on actual group membership, not derived line count -- #lines == 0 is
    -- unreachable because IterateGroupMembers always yields "player" first when
    -- not in a raid, even when fully ungrouped (WR-02, 26-REVIEW.md).
    if not IsInGroup() then
        print("|cff99ff00Wild Things:|r Not in a raid or group.")
        return
    end

    if not exportPopup then
        exportPopup = Private:CreatePopupWindowWithButton()
        exportPopup:SetWidth(420)
        exportPopup.text:SetWidth(420 - 32)
        exportPopup:SetText("Current raid roster (name, realm, class, spec, role):")
        exportPopup:SetButtonText("Close")

        exportPopup.editBox = Private:CreateEditBox(exportPopup, "", function() end)
        exportPopup.editBox:SetMultiLine(true)
        exportPopup.editBox:SetWidth(420 - 32)
        exportPopup.editBox:SetHeight(200)
        exportPopup.editBox:SetPoint("TOP", exportPopup.text, "BOTTOM", 0, -16)

        exportPopup:SetAdditionalHeight(200 + 16 + exportPopup.button:GetHeight() + 10)
        exportPopup:SetButtonOnClick(function() end)
    end

    -- IN-01 (26-REVIEW.md): route through the public BuildGroupExportString()
    -- rather than duplicating a second table.concat(lines, "\n") here, so the
    -- popup and the public API share one source of truth.
    exportPopup.editBox:SetText(self:BuildGroupExportString())
    exportPopup:Pop()

    -- One-click copy: focus + select-all so the user can immediately Ctrl+C.
    exportPopup.editBox:SetFocus()
    exportPopup.editBox:HighlightText()
end
