Write a Python or PySpark script to clean, transform, and aggregate a massive dataset containing complex nested JSON structures.
For this Python version, implement process_nested_events(records). Each record contains a user.id and an events array. Each event contains id, type, value, and metadata.region. Ignore malformed records or events, normalize text fields by trimming and lowercasing, and deduplicate events globally by event ID. Return records grouped by region and event type, sorted by both fields, with count and total_value.
Input is a list of dictionaries. Output is a list of dictionaries with keys region, event_type, count, and total_value.
def process_nested_events(records):