from django.conf import settings

from action_manager.actions.webhook_actions.webhook_action import (
    WebHookAction,
    WebHookActionExecutedEvent
)
from action_manager.exception import PolyaxonActionException
from event_manager.event_actions import EXECUTED

PAGER_DUTY_WEBHOOK_ACTION_EXECUTED = 'pagerduty_webhook_action.{}'.format(EXECUTED)


class PagerDutyWebHookActionExecutedEvent(WebHookActionExecutedEvent):
    event_type = PAGER_DUTY_WEBHOOK_ACTION_EXECUTED


class PagerDutyWebHookAction(WebHookAction):
    key = 'pagerduty_webhook'
    name = 'PagerDuty WebHook'
    event_type = PAGER_DUTY_WEBHOOK_ACTION_EXECUTED
    description = "PagerDuty webhooks to send payload to a pagerduty room."

    def _get_config(self):
        """Configuration for pagerduty webhooks.

        should be a list of urls and potentially a method.

        If no method is given, then by default we use POST.
        """
        return self._get_from_settings(settings.INTEGRATIONS_PAGER_DUTY_WEBHOOKS, 'service_key')

    def _prepare(self, context):
        return {
            'event_type': context.get('event_type'),
            'description': context.get('description'),
            'details': context.get('details'),
            'incident_key': context.get('incident_key'),
            'client': 'polyaxon',
            # 'client_url': get_absolute_uri(),
            'contexts': context.get('contexts'),
        }

    def _pre_execute_web_hook(self, data, config):
        service_key = config.get('service_key')
        if service_key:
            data['service_key'] = service_key

        return data

@@ -673,16 +673,22 @@
 to send 
+event 
 payload 
@@ -690,18 +690,16 @@
 load to 
-a 
 pagerdut
@@ -703,13 +703,8 @@
 duty
- room
 .%22%0A%0A
@@ -837,16 +837,32 @@
 a method
+ and service key
 .%0A%0A     

Update pagerduty docstring