@compiler >= 6

include "String.aes"
include "token.aes"

main contract Webhooks =
    record webhook =
        { name : string, target: string, meta: string}
    record state =
        { owner        : address,
          webhooks     : map(address, map(string, webhook)) }

    stateful entrypoint init() : state =
        { owner = Call.caller,
          webhooks = {} }

    function require_owner() =
        require(Call.caller == state.owner, "only_owner_call_allowed")

    stateful entrypoint add_webhook(name: string, target: string, meta: string) : bool =
        require(Map.lookup(Call.caller, state.webhooks) != None, "Address not registered.")
        put(state{webhooks[Call.caller][name] = {name = name, target = target, meta = meta}})
        true

    entrypoint get_webhooks() : map(string, webhook) =
        Map.lookup_default(Call.caller, state.webhooks, {})

    stateful entrypoint delete_webhook(name: string) =
        let oldmap = Map.lookup_default(Call.caller, state.webhooks, {})
        let newmap = Map.delete(name, oldmap)
        put(state{webhooks[Call.caller] = newmap})

