from admyral.workflow import workflow, Schedule
from admyral.typings import JsonValue
from admyral.actions import (
    search_jira_issues,
    send_slack_message,
    send_list_elements_to_workflow,
)


"""

admyral workflow push alert_vulnerability_sla_breach_in_slack -f workflows/vulnerability_sla_breach.py --activate
admyral workflow push vulnerability_sla_breach -f workflows/vulnerability_sla_breach.py --activate


Setup

1. Create a Slack app (https://api.slack.com/apps)
2. Go to "Interactivity & Shortcuts" and enable interactivity
3. In the Request URL field enter the URL of the Admyral Webhook trigger

"""


@workflow(
    description="Monitoring of vulnerability SLA breaches - alerting out of SLA vulnerabilities",
)
def alert_vulnerability_sla_breach_in_slack(payload: dict[str, JsonValue]):
    send_slack_message(
        channel_id="C06QP0KV1L2",  # TODO: Change to your desired channel
        text=f"{payload["shared"]["header"]}\n[{payload["element"]["key"]}] {payload["element"]["fields"]["summary"]}\n"
        f"Created by: {payload["element"]["fields"]["creator"]["displayName"]}\n"
        f"Status: {payload["element"]["fields"]["status"]["name"]}\n"
        f"Created on: {payload["element"]["fields"]["created"]}\n"
        f"Link: https://admyral.atlassian.net/browse/{payload["element"]["key"]}",  # TODO: adapt your Jira URL
        secrets={"SLACK_SECRET": "slack_secret"},
    )


@workflow(
    description="Monitoring of vulnerability SLA breaches",
    triggers=[Schedule(interval_days=1)],
)
def vulnerability_sla_breach(payload: dict[str, JsonValue]):
    # filter for jira tickets that didn't change in the last 7 days
    no_change_last_7_days = search_jira_issues(
        jql='project = SJ AND status IN ("In Progress", "To Do") AND updated < -1w AND updated >= -8d',  # AND priority IN (Highest, High)
        limit=1000,
        secrets={"JIRA_SECRET": "jira_secret"},
    )
    send_list_elements_to_workflow(
        workflow_name="alert_vulnerability_sla_breach_in_slack",
        elements=no_change_last_7_days,
        shared_data={
            "header": "🚨 No progress in the last 7 days 🚨",
        },
    )

    # filter for jira tickets whose SLA is about to be breached (10 days left)
    soon_breached_slas = search_jira_issues(
        jql='project = SJ AND status IN ("In Progress", "To Do") AND created = -80d',  # AND priority IN (Highest, High)
        limit=1000,
        secrets={"JIRA_SECRET": "jira_secret"},
    )
    send_list_elements_to_workflow(
        workflow_name="alert_vulnerability_sla_breach_in_slack",
        elements=soon_breached_slas,
        shared_data={
            "header": "🚨 About to be breached SLAs in 10 days 🚨",
        },
    )

    # filter for jira tickets that just breached SLA
    breached_slas = search_jira_issues(
        jql='project = SJ AND status IN ("In Progress", "To Do") AND created = -91d',  # AND priority IN (Highest, High)
        limit=1000,
        secrets={"JIRA_SECRET": "jira_secret"},
    )
    send_list_elements_to_workflow(
        workflow_name="alert_vulnerability_sla_breach_in_slack",
        elements=breached_slas,
        shared_data={
            "header": "🚨 Just breached SLAs 🚨",
        },
    )
