local _, ESKP = ...

function ESKP:InitializeDatabase()
    ESKPursuitDB = ESKPursuitDB or {}
    ESKPursuitDB.targets = ESKPursuitDB.targets or {}
    ESKPursuitDB.settings = ESKPursuitDB.settings or {}
    ESKPursuitDB.settings.window = ESKPursuitDB.settings.window or {}
    ESKPursuitDB.settings.trackerWindow = ESKPursuitDB.settings.trackerWindow or {}
    self.db = ESKPursuitDB
end

function ESKP:GetTargetState(id)
    local state = self.db.targets[id]
    if not state then
        state = { watched = false, attempts = 0, notes = "", priceHistory = {} }
        self.db.targets[id] = state
    end
    return state
end

function ESKP:IsWatched(id)
    return self:GetTargetState(id).watched
end

function ESKP:ToggleWatch(id)
    local state = self:GetTargetState(id)
    state.watched = not state.watched
    return state.watched
end

function ESKP:LogAttempt(id)
    local state = self:GetTargetState(id)
    state.attempts = (state.attempts or 0) + 1
    state.lastAttemptAt = time()
end

function ESKP:SaveNotes(id, notes)
    self:GetTargetState(id).notes = notes or ""
end

function ESKP:ResetWindowPosition()
    wipe(self.db.settings.window)
    if self.UI and self.UI.frame then
        self.UI.frame:ClearAllPoints()
        self.UI.frame:SetPoint("CENTER")
    end
end
