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

# Sentry variables
var_action = 'action'
var_data = 'data'
var_description_text = 'description_text'
var_description_title = 'description_title'
var_exception = 'exception'
var_event_id = 'event_id'
var_id = 'id'
var_installation = 'installation'
var_location = 'location'
var_title = 'title'
var_uuid = 'uuid'

# Sentry action values
created_action = 'created'
critical_action = 'critical'
deleted_action = 'deleted'
resolved_action = 'resolved'
triggered_action = 'triggered'
warning_action = 'warning'

# Sentry data types
error_data_type = 'error'
event_data_type = 'event'
installation_alert_type = 'installation'
issue_data_type = 'issue'
metric_alert_data_type = 'metric_alert'

# Internal variables for sentry
str_sentry_installation_id = 'sentry_installation_id'


def get_alert_type(body_data):
    '''
    Gets the type of alert that was received in Sentry.
    :param body_data: (dict) -> Sentry payload 'data' attribute
    :return: (str) data type
    '''
    if error_data_type in body_data:
        return error_data_type
    elif event_data_type in body_data:
        return event_data_type
    elif issue_data_type in body_data:
        return issue_data_type
    elif metric_alert_data_type in body_data:
        return metric_alert_data_type
    elif installation_alert_type in body_data:
        return installation_alert_type
    else:
        raise RuntimeError('Could not verify sentry alert type')


def get_alert_id(body_data, alert_type):
    '''
    Gets the alert ID for the alert received.
    :param body_data: (dict) -> Sentry payload 'data' attribute
    :param alert_type: (str) the type of alert
    :return: (str) alert ID
    '''
    if alert_type == error_data_type:
        return body_data[error_data_type][var_event_id]
    elif alert_type == event_data_type:
        return body_data[event_data_type][var_event_id]
    elif alert_type == issue_data_type:
        return body_data[issue_data_type][var_id]
    elif alert_type == metric_alert_data_type:
        return body_data[metric_alert_data_type][var_id]
    else:
        raise RuntimeError('Could not find Sentry alert ID')


def get_alert_title(body_data, alert_type):
    '''
    Gets the alert title for the alert received.
    :param body_data: (dict) -> Sentry payload 'data' attribute
    :param alert_type: (str) the type of alert
    :return: (str) alert title
    '''
    if alert_type == error_data_type:
        return body_data[error_data_type][var_title]
    elif alert_type == event_data_type:
        return body_data[event_data_type][var_title]
    elif alert_type == issue_data_type:
        return body_data[issue_data_type][var_title]
    elif alert_type == metric_alert_data_type:
        return body_data[var_description_title]
    else:
        raise RuntimeError('Could not find Sentry alert title')


def get_alert_description(body_data, alert_type):
    '''
    Gets the alert description for the alert received.
    :param body_data: (dict) -> Sentry payload 'data' attribute
    :param alert_type: (str) the type of alert
    :return: (str) alert description
    '''
    if alert_type == error_data_type:
        return str(body_data[error_data_type][var_exception])
    elif alert_type == event_data_type:
        return str(body_data[event_data_type][var_exception])
    elif alert_type == issue_data_type:
        return str(body_data[issue_data_type])
    elif alert_type == metric_alert_data_type:
        return body_data[var_description_text]
    else:
        raise RuntimeError('Could not find Sentry alert title')
