local ADDON_NAME = ...
local PvPScout = CreateFrame("Frame")
PvPScout:SetScript("OnEvent", function(self, event, ...)
  if self[event] then
    self[event](self, ...)
  end
end)

PvPScout:RegisterEvent("ADDON_LOADED")
PvPScout:RegisterEvent("PLAYER_LOGIN")

local function InitSavedVariables()
  if not PvPScoutDB then
    PvPScoutDB = {
      enabled = true,
      region = nil, -- auto-detect from client
      brackets = { "2v2", "3v3", "shuffle", "rbg", "blitz" },
      bracketOrder = { "2v2", "3v3", "shuffle", "rbg", "blitz" },
      showRank = true,
      color = { r = 1, g = 0.82, b = 0 }, -- gold
        -- UI/tooltip options
        showHeader = true,
        showUpdatedAt = true,
        colorByRating = true,
        staleHours = 24,
        -- minimap
        showMinimap = true,
        minimapPos = 45,
    }
  else
    -- Migrations: ensure new fields exist
    PvPScoutDB.bracketOrder = PvPScoutDB.bracketOrder or { "2v2", "3v3", "shuffle", "rbg", "blitz" }
    if PvPScoutDB.showHeader == nil then PvPScoutDB.showHeader = true end
    if PvPScoutDB.showUpdatedAt == nil then PvPScoutDB.showUpdatedAt = true end
    if PvPScoutDB.colorByRating == nil then PvPScoutDB.colorByRating = true end
    if not PvPScoutDB.staleHours then PvPScoutDB.staleHours = 24 end
    if PvPScoutDB.showMinimap == nil then PvPScoutDB.showMinimap = true end
    if not PvPScoutDB.minimapPos then PvPScoutDB.minimapPos = 45 end
  end
end

function PvPScout:ADDON_LOADED(name)
  local _ = self -- silence luacheck: unused self
  if name ~= ADDON_NAME then return end
  InitSavedVariables()
end

function PvPScout:PLAYER_LOGIN()
  local _ = self -- silence luacheck: unused self
  -- Determine region by realm list
  local realmList = GetCVar("portal")
  -- Possible values: US, EU, KR, TW, CN (CN unsupported for BNet API)
  if realmList and realmList ~= "" then
    PvPScoutDB.region = PvPScoutDB.region or string.lower(realmList)
  end

  -- Apply minimap visibility if button exists
  if PvPScout_MinimapButton then
    if PvPScoutDB.showMinimap == false then PvPScout_MinimapButton:Hide() else PvPScout_MinimapButton:Show() end
  end

  -- Startup message (English)
  local ver = tostring((GetAddOnMetadata and GetAddOnMetadata(ADDON_NAME, "Version")) or "")
  local brackets = PvPScoutDB.brackets or {"2v2","3v3"}
  local bracketStr = table.concat(brackets, ", ")
  local mini = (PvPScoutDB.showMinimap ~= false) and "shown" or "hidden"
  print(string.format("PvPScout %s loaded — settings via /pvpscout — minimap: %s — brackets: %s", ver, mini, bracketStr))

  -- Make sure slash to open AddOns pane exists as alternative navigation
  _G.SLASH_PVPSCOUTADDONS1 = "/pvpscoutaddons"
  SlashCmdList["PVPSCOUTADDONS"] = function()
    if Settings and SettingsPanel and SettingsPanel.OpenToCategory then
      SettingsPanel.OpenToCategory("AddOns")
    elseif InterfaceOptionsFrame_OpenToCategory then
      InterfaceOptionsFrame_OpenToCategory("InterfaceOptionsFrameAddOnsList")
    end
  end
end

-- Public API
_G.PvPScout = PvPScout
