local API = rawget(_G, "LIDCOUNCIL_API")
if type(API) ~= "table" then
  API = {}
  _G.LIDCOUNCIL_API = API
end

local function PublishApi(name, value, opts)
  API[name] = value
  if type(opts) == "table" and opts.global == false then
    _G[name] = nil
    return
  end
  _G[name] = value
end

local function TrimText(value)
  value = tostring(value or "")
  return (value:gsub("^%s+", ""):gsub("%s+$", ""))
end

local function NormalizeDistributionMode(value)
  local mode = string.upper(TrimText(value))
  if mode == "MANUAL_VISIBLE" then
    return "MANUAL_VISIBLE"
  end
  return "COUNCIL"
end

local function DetectCollectibleKind(itemID, link)
  local resolvedItemID = tonumber(itemID)
  local resolvedLink = TrimText(link)

  if type(C_HousingCatalog) == "table" and type(C_HousingCatalog.GetCatalogEntryInfoByItem) == "function" then
    local ok, info = pcall(C_HousingCatalog.GetCatalogEntryInfoByItem, resolvedItemID or resolvedLink)
    if ok and type(info) == "table" then
      return "HOUSING_DECOR"
    end
  end

  if type(C_ToyBox) == "table" and type(C_ToyBox.GetToyInfo) == "function" then
    local ok, toyName = pcall(C_ToyBox.GetToyInfo, resolvedItemID or resolvedLink)
    if ok and toyName then
      return "TOY"
    end
  end

  if resolvedItemID and type(C_MountJournal) == "table" and type(C_MountJournal.GetMountFromItem) == "function" then
    local ok, mountID = pcall(C_MountJournal.GetMountFromItem, resolvedItemID)
    if ok and tonumber(mountID) and tonumber(mountID) > 0 then
      return "MOUNT"
    end
  end

  if resolvedItemID and type(C_PetJournal) == "table" and type(C_PetJournal.GetPetInfoByItemID) == "function" then
    local ok, speciesID = pcall(C_PetJournal.GetPetInfoByItemID, resolvedItemID)
    if ok and tonumber(speciesID) and tonumber(speciesID) > 0 then
      return "PET"
    end
  end

  return nil
end

local function ClassifyLootRollItem(payload)
  payload = type(payload) == "table" and payload or {}

  local distributionMode = NormalizeDistributionMode(payload.distributionMode)
  local collectibleKind = payload.collectibleKind
  if distributionMode ~= "MANUAL_VISIBLE" then
    collectibleKind = DetectCollectibleKind(payload.itemID, payload.link)
    if collectibleKind then
      distributionMode = "MANUAL_VISIBLE"
    end
  end

  local statusLabel = distributionMode == "MANUAL_VISIBLE" and "Non-Council Item" or ""
  local detailText = ""
  if distributionMode == "MANUAL_VISIBLE" then
    detailText =
      "This item was not auto-passed or distributed by LidCouncil.\n"
      .. "It is shown for visibility only and is not added to the loot list unless you add manual note points."
  end

  return {
    distributionMode = distributionMode,
    collectibleKind = collectibleKind,
    councilManaged = distributionMode == "COUNCIL",
    autoPassAllowed = distributionMode == "COUNCIL",
    showOnLiveBoard = true,
    statusLabel = statusLabel,
    detailText = detailText,
  }
end

local function IsManualVisibleLootItem(entry)
  return NormalizeDistributionMode(type(entry) == "table" and entry.distributionMode or nil) == "MANUAL_VISIBLE"
end

PublishApi("LIDCOUNCIL_ClassifyLootRollItem", ClassifyLootRollItem)
PublishApi("LIDCOUNCIL_IsManualVisibleLootItem", IsManualVisibleLootItem)
