# By: Riasat Ullah
# This file contains all constants and functions related to the Rollbar integration.

from utils import constants


# Rollbar variables
var_body = 'body'
var_data = 'data'
var_event_name = 'event_name'
var_item = 'item'
var_last_occurrence = 'last_occurrence'
var_occurrence = 'occurrence'

# Sub variables of the "item" attribute
var_item_id = 'id'
var_item_level = 'level'
var_item_status = 'status'
var_item_title = 'title'

# Rollbar event names
deploy = 'deploy'
exp_repeat_item = 'exp_repeat_item'
occurrence = 'occurrence'
item_velocity = 'item_velocity'
new_item = 'new_item'
reactivated_item = 'reactivated_item'
reopened_item = 'reopened_item'
resolved_item = 'resolved_item'

# Rollbar status values
active_status = 1
resolved_status = 2

# item.level attribute from Rollbar mapped to the urgency levels of TaskCall
severity_mapping = {
    10: constants.minor_urgency,
    20: constants.low_urgency,
    30: constants.medium_urgency,
    40: constants.high_urgency,
    50: constants.critical_urgency
}


def get_rollbar_incident_description(request_body, event_name):
    '''
    Gets the description of the incident.
    :param request_body: (dict) body of the request
    :param event_name: type of the event
    :return: (str) -> description of the incident
    '''
    if event_name == occurrence:
        if occurrence in request_body[var_data]:
            return str(request_body[var_data][occurrence][var_body])
    else:
        if var_item in request_body[var_data] and var_last_occurrence in request_body[var_data][var_item]:
            return str(request_body[var_data][var_item][var_last_occurrence][var_body])
    return str(request_body)
