--[[ Core/Guard.lua - API drift protection.

     Everything we know about C_HousingBlueprint was learned on the 12.1 PTR.
     Blizzard can rename or drop functions between PTR and live. If that happens
     the addon must NOT spam Lua errors - in a launch week that is the fastest
     route to bad reviews.

     At ADDON_LOADED we verify every function we actually call. Anything missing
     disables its feature gracefully, and the player gets ONE line about it.
     Everything unaffected keeps working.

     ns.features = { contents, import, collection, links, catalog, export }
     Use ns.Guard.Has("import") before wiring anything that needs the API. ]]--

local _, ns = ...
local Guard = {}
ns.Guard = Guard

-- feature -> the calls it cannot live without
local REQUIREMENTS = {
  contents   = { { "C_HousingBlueprint", "RequestBlueprintContents" } },
  import     = { { "C_HousingBlueprint", "ImportBlueprint" } },
  collection = { { "C_HousingBlueprint", "RequestBlueprintCollection" } },
  links      = { { "C_HousingBlueprint", "IsShareCodeValid" } },
  catalog    = { { "C_HousingCatalog", "GetCatalogEntryInfoByRecordID" } },
  export     = { { "C_HousingBlueprint", "ExportBlueprint" } },   -- dev/bake only
}

-- nice names for the one-line message
local LABELS = {
  contents   = "buildability scores",
  import     = "one-click import",
  collection = "My Blueprints",
  links      = "chat links",
  catalog    = "item icons",
  export     = "blueprint export",
}

ns.features = {}

local function exists(namespaceName, fnName)
  local t = _G[namespaceName]
  return type(t) == "table" and type(t[fnName]) == "function"
end

--- Run detection. Returns list of missing feature keys.
function Guard.Check()
  local missing = {}
  for feature, calls in pairs(REQUIREMENTS) do
    local ok = true
    for _, call in ipairs(calls) do
      if not exists(call[1], call[2]) then ok = false; break end
    end
    ns.features[feature] = ok
    if not ok then missing[#missing + 1] = feature end
  end
  table.sort(missing)
  return missing
end

--- Has the API for this feature? Unknown feature names default to true so a
--- typo can never silently switch something off.
function Guard.Has(feature)
  local v = ns.features[feature]
  if v == nil then return true end
  return v
end

--- True when the housing API is missing entirely (pre-12.1 client).
function Guard.HousingAvailable()
  return type(C_HousingBlueprint) == "table"
end

------------------------------------------------------------------------
-- Run detection and describe the result. Init.lua owns the single login
-- line (spec 7), so this returns text instead of printing it.
------------------------------------------------------------------------
function Guard.CheckAndDescribe()
  if not Guard.HousingAvailable() then
    for feature in pairs(REQUIREMENTS) do ns.features[feature] = false end
    ns.degraded = "no-housing-api"
    return "Housing Blueprints not found on this client - BlueprintBazaar needs patch 12.1."
  end

  local missing = Guard.Check()
  -- export is a dev-only convenience; never worth warning players about
  for i = #missing, 1, -1 do
    if missing[i] == "export" then table.remove(missing, i) end
  end
  if #missing == 0 then return nil end

  ns.degraded = table.concat(missing, ",")
  local labels = {}
  for _, feature in ipairs(missing) do labels[#labels + 1] = LABELS[feature] or feature end
  return "Blizzard changed part of the housing API in this build, so " ..
    table.concat(labels, " and ") .. " are off for now. Everything else works."
end
