// Package audit provides helpers for constructing audit log detail strings.
package audit

import "encoding/json"

// JSON builds a JSON details string from key-value pairs for audit logging.
// Uses json.Marshal for safe encoding rather than fmt.Sprintf with %q,
// which is fragile for edge-case Unicode.
func JSON(pairs ...string) string {
	if len(pairs)%2 != 0 {
		return "{}"
	}
	m := make(map[string]string, len(pairs)/2)
	for i := 0; i < len(pairs); i += 2 {
		m[pairs[i]] = pairs[i+1]
	}
	b, err := json.Marshal(m)
	if err != nil {
		return "{}"
	}
	return string(b)
}

// JSONWithRoles builds a JSON details string that includes a "roles" key
// mapped to a string slice. This produces a proper JSON array for the value.
func JSONWithRoles(roles []string) string {
	m := map[string][]string{"roles": roles}
	b, err := json.Marshal(m)
	if err != nil {
		return "{}"
	}
	return string(b)
}
