local _, ESKP = ...

function ESKP:InitializeTracker()
    self.nextReset = 0
    if C_DateAndTime and C_DateAndTime.GetSecondsUntilWeeklyReset then
        self.nextReset = C_DateAndTime.GetSecondsUntilWeeklyReset() or 0
    end
end

function ESKP:IsReadyNow(target)
    if target.reset ~= "Weekly" then return true end
    local state = self:GetTargetState(target.id)
    if not state.lastAttemptAt then return true end
    local resetSeconds = C_DateAndTime and C_DateAndTime.GetSecondsUntilWeeklyReset and C_DateAndTime.GetSecondsUntilWeeklyReset() or self.nextReset or 0
    local nextResetAt = time() + resetSeconds
    return state.lastAttemptAt < (nextResetAt - 7 * 24 * 60 * 60)
end

function ESKP:GetTargets(filter)
    local results = {}
    for _, target in ipairs(self.Targets) do
        local include = filter == nil or filter == "ALL"
        if filter == "WATCHLIST" then include = self:IsWatched(target.id) end
        if filter == "READY" then include = self:IsReadyNow(target) end
        if filter == target.type then include = true end
        if include then table.insert(results, target) end
    end
    table.sort(results, function(a, b)
        local aWatched, bWatched = self:IsWatched(a.id), self:IsWatched(b.id)
        if aWatched ~= bWatched then return aWatched end
        return a.name < b.name
    end)
    return results
end
