---@class AddonPrivate
local Private = select(2, ...)

---@class Settings
local settings = {}
Private.Settings = settings

-- Default saved settings. These are merged into the SavedVariables DB on load
-- WITHOUT overwriting any value the user already has, so existing profiles keep
-- their minimap position etc. and just gain any newly-added keys.
local defaults = {
    -- auto show/hide the planner + Nearby HUD when entering/leaving a meta-expansion
    -- CONTINENT (only closes when the whole continent has nothing tracked).
    autoContinent = false,
    minimap = { hide = false, minimapPos = 220 },
    hud = {
        enabled = false,        -- is the "Nearby Now" HUD shown
        locked = false,         -- prevent dragging the HUD
        point = "RIGHT",
        relPoint = "RIGHT",
        x = -20,
        y = 120,
        maxRows = 8,            -- how many nearby objectives to list
        showTravelHint = true, -- show the "tracking / travel to" line
    },
    route = {
        enabled = false,        -- is the "Do-it-now Route" window shown
        locked = false,
        point = "LEFT",
        relPoint = "LEFT",
        x = 20,
        y = 120,
        maxRows = 12,             -- how many route steps to display
        dungeonsCollapsed = false, -- Dungeons & Raids section collapsed
        otherZonesCollapsed = false, -- Other zones on this continent section collapsed
        wqCollapsed = false,       -- World Quests section collapsed
        wqRelevantOnly = true,     -- show only meta-relevant World Quests (right-click header to show all)
        delvesCollapsed = false,   -- Delves section collapsed
        questsCollapsed = false,   -- Storylines & Quests section collapsed
        filters = {},              -- per-section show/hide toggles (nil/true = shown); see the Filter button
        scale = 1.0,               -- planner window scale (accessibility)
        boldText = false,          -- bold/outlined text for readability
        autoMode = false,          -- auto-track the nearest objective, hands-free (everywhere)
        autoModeInstance = false,  -- like autoMode but ONLY while inside an instance (dungeon/raid/delve/scenario)

    },
    preview = {
        enabled = true,         -- auto-show the reward model panel when a reward-bearing achievement is selected
    },
    rares = {
        enabled = true,         -- pop an alert when a rare (map vignette) appears nearby
        onlyRelevant = true,    -- default ON: only alert for rares tied to an incomplete tracked meta objective
        sound = true,           -- play a sound on a new rare
        locked = false,
        point = "TOP", relPoint = "TOP", x = 0, y = -200,
    },
    -- Silent realm-wide peer detection (see Comm.lua). enabled = participate in the
    -- hidden channel (needed for detection to work at all); verbose = live prints
    -- (author only, via /mtwho live). Normal users never see anything.
    comm = { enabled = true, verbose = false, review = false, rejected = {} },
    ignored = {},              -- objectives the player has chosen to skip: key -> name
    waypointNotes = {},        -- in-game authored waypoint notes: key -> note text
    waypointFixes = {},        -- in-game recorded coordinate corrections: key -> fix meta
    waypointSteps = {},        -- in-game authored route ordering: key -> {object, step}
}

local function applyDefaults(dst, src)
    for k, v in pairs(src) do
        if type(v) == "table" then
            if type(dst[k]) ~= "table" then dst[k] = {} end
            applyDefaults(dst[k], v)
        elseif dst[k] == nil then
            dst[k] = v
        end
    end
end

function settings:Initialize(db)
    self.db = db
    applyDefaults(db, defaults)
    -- One-time migration: switch existing profiles to the new "meta-relevant WQs
    -- only" default. Runs once; later manual toggles are preserved.
    if not db._migratedWQRelevantOnly then
        if db.route then db.route.wqRelevantOnly = true end
        db._migratedWQRelevantOnly = true
    end
    -- One-time migration: default rare alerts to "meta-relevant only" for everyone,
    -- including existing profiles. Runs once; later manual toggles are preserved.
    if not db._migratedRaresRelevant then
        if db.rares then db.rares.onlyRelevant = true end
        db._migratedRaresRelevant = true
    end
end

---@return table
function settings:Hud()
    return self.db and self.db.hud
end

---@return table
function settings:Route()
    return self.db and self.db.route
end

---@return table
function settings:Preview()
    return self.db and self.db.preview
end

---@return table
function settings:Rares()
    return self.db and self.db.rares
end
