local addonName, RF = ...

-------------------------------------------------------------------------------
-- NotesManager: RosterNotes beyond WoW's 31-character limit
-- Stored per-guild in SavedVariables: guildDB.notes[name] = text
-- Synced via SyncEngine (NOTE/NDEL message types)
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- Get extended note for a member
-------------------------------------------------------------------------------
function RF:GetExtendedNote(name)
    local guildDB = RF:GetGuildDB()
    if not guildDB then return nil end
    return guildDB.notes[name]
end

-------------------------------------------------------------------------------
-- Set extended note for a member
-------------------------------------------------------------------------------
function RF:SetExtendedNote(name, text)
    local guildDB = RF:GetGuildDB()
    if not guildDB then
        RF:Print("Cannot set note - not in a guild.")
        return false
    end

    if not guildDB.noteTimestamps then guildDB.noteTimestamps = {} end

    if text and text:trim() ~= "" then
        guildDB.notes[name] = text
    else
        guildDB.notes[name] = nil -- remove empty notes
    end

    guildDB.noteTimestamps[name] = time()
    RF:AddLog("NOTE", "RosterNote updated for " .. name)
    RF:FireCallbacks("NOTE_UPDATED", name, text)
    return true
end

-------------------------------------------------------------------------------
-- Delete extended note
-------------------------------------------------------------------------------
function RF:DeleteExtendedNote(name)
    local guildDB = RF:GetGuildDB()
    if not guildDB then return false end

    if not guildDB.noteTimestamps then guildDB.noteTimestamps = {} end

    guildDB.notes[name] = nil
    guildDB.noteTimestamps[name] = time()
    RF:FireCallbacks("NOTE_UPDATED", name, nil)
    return true
end

-------------------------------------------------------------------------------
-- Get all notes (for export)
-------------------------------------------------------------------------------
function RF:GetAllExtendedNotes()
    local guildDB = RF:GetGuildDB()
    if not guildDB then return {} end
    return guildDB.notes
end

-------------------------------------------------------------------------------
-- Search notes by text
-------------------------------------------------------------------------------
function RF:SearchNotes(query)
    local guildDB = RF:GetGuildDB()
    if not guildDB then return {} end

    query = query:lower()
    local results = {}
    for name, text in pairs(guildDB.notes) do
        if name:lower():find(query, 1, true) or text:lower():find(query, 1, true) then
            tinsert(results, { name = name, note = text })
        end
    end
    return results
end
