-- Portal Authority — core/bootstrap.lua
-- Core bootstrap, gate evaluation, and global table initialization.
--
-- Depends on: none
-- Exposes:    PortalAuthority buildConfig/gate methods; PortalAuthority.CoreInternal gate helpers
-- DB keys:    none
-- Events:     none
-- Search key: none

-- Portal Authority
-- Core event handling, settings migration, and chat announcement logic.

local ADDON_NAME = ...

PortalAuthority = PortalAuthority or {}
PortalAuthorityDiagDump = nil

local PA_SETTINGS_WINDOW_DEV_GATE_ENABLED = false
local PA_SETTINGS_WINDOW_PUBLIC_GATE_ENABLED = true
local PA_DIAG_GATE_ENABLED = false

local PA_IsSecretValue
local PA_NormalizeSafeString
local PA_SafeStringsEqual
local PA_SafeUnitGUID

PortalAuthority.CoreInternal = PortalAuthority.CoreInternal or {}
local CoreInternal = PortalAuthority.CoreInternal

PortalAuthority._buildConfig = {
    gates = {
        uiSurface = false,
        staticBaseline = false,
        settingsBaseline = false,
        settingsEagerBuild = false,
        settingsDirectBuild = false,
        settingsWindowDev = PA_SETTINGS_WINDOW_DEV_GATE_ENABLED,
        settingsWindowPublic = PA_SETTINGS_WINDOW_PUBLIC_GATE_ENABLED,
        diag = PA_DIAG_GATE_ENABLED,
        onboardingD1Dev = false, -- Internal onboarding branch only; keep false in public builds.
        onboardingD3Dev = false, -- Internal onboarding branch only; keep false in public builds.
        onboardingD4Dev = false, -- Internal onboarding branch only; keep false in public builds.
        onboardingD3Public = true,
        onboardingD4Public = true,
        onboardingQualifyingUpgradePublic = true,
    },
    release = {
        premiumOnboardingResetEpoch = "PA_2_0_0_BASELINE",
        premiumOnboardingVersion = "PA_2_0_0_PREMIUM_V1",
        qualifyingUpgradeOnboardingVersion = "PA_2_1_1_QUALIFYING_UPGRADE_V1",
        settingsWindowAnnouncementVersion = "2.1.2",
        premiumAuthoredFontPath = "Fonts\\FRIZQT__.TTF",
        premiumAuthoredFontName = "Overclock: Friz",
        overclockWarcraftLogsUrl = "https://classic.warcraftlogs.com/guild/us/faerlina/overclock",
    },
    settings = {
        keystoneBootstrapStates = {
            scheduled = true,
            ran = true,
            suppressed = true,
            unknown = true,
        },
        bootstrapActiveStates = {
            none = true,
            root = true,
            announcements = true,
            dock = true,
            timers = true,
            interrupt = true,
            combat = true,
            profiles = true,
        },
    },
}

function PA_IsUiSurfaceGateEnabled()
    return PortalAuthority._buildConfig.gates.uiSurface == true
end

function PortalAuthority:IsUiSurfaceGateEnabled()
    return PA_IsUiSurfaceGateEnabled()
end

function PA_IsStaticBaselineGateEnabled()
    return PortalAuthority._buildConfig.gates.staticBaseline == true
end

function PortalAuthority:IsStaticBaselineGateEnabled()
    return PA_IsStaticBaselineGateEnabled()
end

function PA_IsSettingsBaselineGateEnabled()
    return PortalAuthority._buildConfig.gates.settingsBaseline == true
end

function PortalAuthority:IsSettingsBaselineGateEnabled()
    return PA_IsSettingsBaselineGateEnabled()
end

function PA_IsSettingsEagerBuildGateEnabled()
    if PortalAuthority._buildConfig.gates.settingsEagerBuild == true then
        return true
    end
    return PortalAuthority
        and PortalAuthority.IsSettingsWindowHostEnabled
        and PortalAuthority:IsSettingsWindowHostEnabled()
        or false
end

function PortalAuthority:IsSettingsEagerBuildGateEnabled()
    return PA_IsSettingsEagerBuildGateEnabled()
end

function PA_IsSettingsDirectBuildGateEnabled()
    return PortalAuthority._buildConfig.gates.settingsDirectBuild == true
end

function PortalAuthority:IsSettingsDirectBuildGateEnabled()
    return PA_IsSettingsDirectBuildGateEnabled()
end

function PA_IsSettingsWindowDevGateEnabled()
    return PortalAuthority._buildConfig.gates.settingsWindowDev == true
end

function PortalAuthority:IsSettingsWindowDevGateEnabled()
    return PA_IsSettingsWindowDevGateEnabled()
end

function PA_IsSettingsWindowPublicGateEnabled()
    return PortalAuthority._buildConfig.gates.settingsWindowPublic == true
end

function PortalAuthority:IsSettingsWindowPublicGateEnabled()
    return PA_IsSettingsWindowPublicGateEnabled()
end

function PA_IsSettingsWindowHostEnabled()
    return PA_IsSettingsWindowDevGateEnabled() or PA_IsSettingsWindowPublicGateEnabled()
end

function PortalAuthority:IsSettingsWindowHostEnabled()
    return PA_IsSettingsWindowHostEnabled()
end

BINDING_HEADER_PORTAL_AUTHORITY = "Portal Authority"
BINDING_NAME_PORTAL_AUTHORITY_OPEN_SETTINGS = "Open Settings"

function PA_Binding_ToggleSettingsWindow()
    if not PortalAuthority then
        return
    end

    if PortalAuthority.IsSettingsWindowOpen and PortalAuthority:IsSettingsWindowOpen() then
        if PortalAuthority.CloseSettingsWindow then
            PortalAuthority:CloseSettingsWindow()
        end
        return
    end

    if PortalAuthority.HandleSlashRootEntry then
        PortalAuthority:HandleSlashRootEntry()
    end
end

function PA_IsDiagGateEnabled()
    return PortalAuthority._buildConfig.gates.diag == true
end

function PortalAuthority:IsDiagDumpEnabled()
    return PA_IsDiagGateEnabled()
end

function PA_IsOnboardingD1DevGateEnabled()
    return PortalAuthority._buildConfig.gates.onboardingD1Dev == true
end

function PortalAuthority:IsOnboardingD1DevGateEnabled()
    return PA_IsOnboardingD1DevGateEnabled()
end

function PA_IsOnboardingD3DevGateEnabled()
    return PortalAuthority._buildConfig.gates.onboardingD3Dev == true
end

function PortalAuthority:IsOnboardingD3DevGateEnabled()
    return PA_IsOnboardingD3DevGateEnabled()
end

function PA_IsOnboardingD4DevGateEnabled()
    return PA_IsOnboardingD3DevGateEnabled() and PortalAuthority._buildConfig.gates.onboardingD4Dev == true
end

function PortalAuthority:IsOnboardingD4DevGateEnabled()
    return PA_IsOnboardingD4DevGateEnabled()
end

function PA_IsOnboardingD3PublicGateEnabled()
    return PortalAuthority._buildConfig.gates.onboardingD3Public == true
end

function PortalAuthority:IsOnboardingD3PublicGateEnabled()
    return PA_IsOnboardingD3PublicGateEnabled()
end

function PA_IsOnboardingD4PublicGateEnabled()
    return PA_IsOnboardingD3PublicGateEnabled() and PortalAuthority._buildConfig.gates.onboardingD4Public == true
end

function PortalAuthority:IsOnboardingD4PublicGateEnabled()
    return PA_IsOnboardingD4PublicGateEnabled()
end

function PA_IsOnboardingQualifyingUpgradePublicGateEnabled()
    return PortalAuthority._buildConfig.gates.onboardingQualifyingUpgradePublic == true
end

function PortalAuthority:IsOnboardingQualifyingUpgradePublicGateEnabled()
    return PA_IsOnboardingQualifyingUpgradePublicGateEnabled()
end

function PA_IsOnboardingD3Enabled()
    return PA_IsOnboardingD3DevGateEnabled() or PA_IsOnboardingD3PublicGateEnabled()
end

function PortalAuthority:IsOnboardingD3Enabled()
    return PA_IsOnboardingD3Enabled()
end

function PA_IsOnboardingD4Enabled()
    return PA_IsOnboardingD4DevGateEnabled() or PA_IsOnboardingD4PublicGateEnabled()
end

function PortalAuthority:IsOnboardingD4Enabled()
    return PA_IsOnboardingD4Enabled()
end

function PortalAuthority:IsCombatAlertsEnabled()
    local db = PortalAuthorityDB or self.defaults or {}
    return db.combatAlertsEnabled ~= false
end

function PortalAuthority:SetSettingsKeystoneBootstrapState(state)
    local states = PortalAuthority._buildConfig.settings.keystoneBootstrapStates
    local normalized = type(state) == "string" and states[state] and state or "unknown"
    local currentState = self and self._settingsKeystoneBootstrapState or nil
    local current = type(currentState) == "string" and states[currentState] and currentState or "unknown"

    if current == "ran" then
        return current
    end
    if current == "scheduled" and (normalized == "unknown" or normalized == "scheduled" or normalized == "suppressed") then
        return current
    end
    if current == "suppressed" and normalized ~= "ran" then
        return current
    end
    if normalized == "unknown" and self and self._settingsKeystoneBootstrapState ~= nil then
        return current
    end

    self._settingsKeystoneBootstrapState = normalized
    return normalized
end

function PortalAuthority:GetSettingsKeystoneBootstrapState()
    local state = self and self._settingsKeystoneBootstrapState or nil
    local states = PortalAuthority._buildConfig.settings.keystoneBootstrapStates
    if type(state) == "string" and states[state] then
        return state
    end
    return "unknown"
end

function PortalAuthority:SetSettingsBootstrapActive(state)
    local states = PortalAuthority._buildConfig.settings.bootstrapActiveStates
    local normalized = type(state) == "string" and states[state] and state or "none"
    self._settingsBootstrapActive = normalized
    return normalized
end

function PortalAuthority:GetSettingsBootstrapActive()
    local state = self and self._settingsBootstrapActive or nil
    local states = PortalAuthority._buildConfig.settings.bootstrapActiveStates
    if type(state) == "string" and states[state] then
        return state
    end
    return "none"
end

CoreInternal.PA_IsUiSurfaceGateEnabled = PA_IsUiSurfaceGateEnabled
CoreInternal.PA_IsStaticBaselineGateEnabled = PA_IsStaticBaselineGateEnabled
CoreInternal.PA_IsSettingsBaselineGateEnabled = PA_IsSettingsBaselineGateEnabled
CoreInternal.PA_IsSettingsEagerBuildGateEnabled = PA_IsSettingsEagerBuildGateEnabled
CoreInternal.PA_IsSettingsDirectBuildGateEnabled = PA_IsSettingsDirectBuildGateEnabled
CoreInternal.PA_IsSettingsWindowHostEnabled = PA_IsSettingsWindowHostEnabled
CoreInternal.PA_IsSettingsWindowDevGateEnabled = PA_IsSettingsWindowDevGateEnabled
CoreInternal.PA_IsSettingsWindowPublicGateEnabled = PA_IsSettingsWindowPublicGateEnabled
CoreInternal.PA_IsOnboardingD1DevGateEnabled = PA_IsOnboardingD1DevGateEnabled
CoreInternal.PA_IsOnboardingD3DevGateEnabled = PA_IsOnboardingD3DevGateEnabled
CoreInternal.PA_IsOnboardingD4DevGateEnabled = PA_IsOnboardingD4DevGateEnabled
CoreInternal.PA_IsOnboardingD3PublicGateEnabled = PA_IsOnboardingD3PublicGateEnabled
CoreInternal.PA_IsOnboardingD4PublicGateEnabled = PA_IsOnboardingD4PublicGateEnabled
CoreInternal.PA_IsOnboardingQualifyingUpgradePublicGateEnabled = PA_IsOnboardingQualifyingUpgradePublicGateEnabled
CoreInternal.PA_IsOnboardingD3Enabled = PA_IsOnboardingD3Enabled
CoreInternal.PA_IsOnboardingD4Enabled = PA_IsOnboardingD4Enabled
