package clearance import ( "fmt" "strings" ) // EventStore persists clearance changes and audit rows. type EventStore interface { InsertClearanceEvent(agentID string, fromLevel, toLevel int, reason, source string) error } // RequestElevation raises an agent to toLevel when higher than current, logs the event, and returns the new level. func RequestElevation(store EventStore, agentID string, currentLevel, toLevel int, reason, source string) (int, error) { agentID = strings.TrimSpace(agentID) if agentID == "" { return currentLevel, fmt.Errorf("agent id required") } if toLevel < L0 { toLevel = L0 } if toLevel > L4 { toLevel = L4 } if toLevel <= currentLevel { return currentLevel, nil } reason = strings.TrimSpace(reason) if reason == "" { reason = "elevation requested" } source = strings.TrimSpace(source) if source == "" { source = "system" } if store != nil { if err := store.InsertClearanceEvent(agentID, currentLevel, toLevel, reason, source); err != nil { return currentLevel, err } } return toLevel, nil }