from typing import OrderedDict


def events_v2_data(alert, routing_key, with_record=True):
    """Helper method to generate the payload to create an event using PagerDuty Events API v2

    Args:
        alert (Alert): Alert relevant to the triggered rule
        routing_key (str): Routing key for this PagerDuty integration
        with_record (boolean): Option to add the record data or not

    Returns:
        dict: Contains JSON blob to be used as event
    """
    summary = 'StreamAlert Rule Triggered - {}'.format(alert.rule_name)
    details = OrderedDict()
    details['description'] = alert.rule_description
    if with_record:
        details['record'] = alert.record

    payload = {
        'summary': summary,
        'source': alert.log_source,
        'severity': 'critical',
        'custom_details': details
    }
    return {
        'routing_key': routing_key,
        'payload': payload,
        'event_action': 'trigger',
        'client': 'StreamAlert'
    }