#!/bin/zsh
# set -x

auth="$(gopass metrans/oncall-apikey)"
if test -z $ROFI_RETV
then
    rofi -show oncall -modi "oncall:$0" \
        -kb-accept-custom "" \
        -kb-accept-alt "" \
        -kb-custom-1 "Shift+Return" \
        # -sidebar-mode \
        -kb-custom-2 "Control+Return"

fi

case "$ROFI_RETV" in
    0)  echo -e "\0use-hot-keys\x1ftrue\n"
        echo -e "\0message\x1fConfirm to open, Shift-Enter to ack, Ctrl-Enter to resolve\n"
        curl -s --header "Authorization: $auth" 'https://oncall.corp.metrans.cz/api/v1/alert_groups?state=new' | jq -r '.results[] | .title+"\u0000info\u001f"+.permalinks.web+"|"+.id'
        ;;
    1) ( handlr open "${ROFI_INFO/|*}" ) & ;;
    10) (
        curl -s --request POST --header "Authorization: $auth" "https://oncall.corp.metrans.cz/api/v1/alert_groups/${ROFI_INFO/*|}/acknowledge"
        ) & ;;
    11) (
        curl -s --request POST --header "Authorization: $auth" "https://oncall.corp.metrans.cz/api/v1/alert_groups/${ROFI_INFO/*|}/resolve"
        ) & ;;
esac
