import Foundation

/// Provides a stable node identity for mesh networking.
/// - `nodeID`: Stable per install (persisted in UserDefaults)
/// - `instance`: Monotonic counter per session start
struct NodeIdentity {
    private static let nodeIDKey = "com.attable.mesh.nodeID"
    private static let instanceKey = "com.attable.mesh.instance"
    
    /// Get or create a stable nodeID for this installation
    static var nodeID: String {
        if let existing = UserDefaults.standard.string(forKey: nodeIDKey) {
            return existing
        }
        let newID = UUID().uuidString
        UserDefaults.standard.set(newID, forKey: nodeIDKey)
        return newID
    }
    
    /// Increment and return the current instance counter
    static func nextInstance() -> Int {
        let current = UserDefaults.standard.integer(forKey: instanceKey)
        let next = current + 1
        UserDefaults.standard.set(next, forKey: instanceKey)
        return next
    }
    
    /// Get the current instance without incrementing (for reference)
    static var currentInstance: Int {
        return UserDefaults.standard.integer(forKey: instanceKey)
    }
}
