from sym.sdk.annotations import reducer, hook
from sym.sdk.integrations import slack
from sym.sdk.request_permission import PermissionLevel, RequestPermission
from sym.sdk.notifications import Notification


# Reducers fill in the blanks that your workflow needs in order to run.
# For more information, please see https://docs.symops.com/docs/reducers
@reducer
def get_permissions(event):
    """Decide who can see and take actions on requests."""
    return RequestPermission(
        # Only admins can see requests in the Sym web app.
        webapp_view=PermissionLevel.ADMIN,
        # Only admins can approve or deny requests, or revoke access.
        approve_deny=PermissionLevel.ADMIN,
        # Users can approve their own requests. This is great for testing!
        allow_self_approval=True
    )


@reducer
def get_request_notifications(event):
    """Send notifications about new requests to Slack."""
    return [
        # Make sure that this channel has been created in your Slack workspace!
        Notification(destinations=[slack.channel("#sym-requests")])
    ]


@hook
def after_approve(event):
    send_next_steps_message()


@hook
def after_deny(event):
    send_next_steps_message()


def send_next_steps_message():
    message = (
        ":tada: Congratulations, you just ran an approval-only Sym Flow!\n\n\n"
        "*Next, you might want to try:*\n"
        "\t•\tAdding <https://docs.symops.com/docs/customizing-implpy-logic|custom logic> to `SYM_TEMPLATE_VAR_IMPL_PATH`\n"
        "\t•\tAdding an integration, such as <https://docs.symops.com/docs/pagerduty|PagerDuty>, to your `sym_environment`\n"
        "\t•\tChecking out our <https://github.com/symopsio/examples|Examples Repo> to see some full Flows in action\n\n"
        f"_Hint: You can remove this message by modifying the hooks in `SYM_TEMPLATE_VAR_IMPL_PATH`, and running `terraform apply` to apply your changes._\n\n\n"
        "If you have any questions or would like help, please reach out at support@symops.com, or by running `/sym support` in Slack."
    )

    slack.send_message(slack.channel("#sym-requests"), message)
