// The shop's owner gets a push on their phone for every big order, with buttons to open it or
// mark it packed, through ntfy (the free ntfy.sh, or a server of their own).
//
//   NTFY_URL=https://ntfy.sh NTFY_TOPIC=shop-orders-x7k2 quillo run ntfy.qwe      then open http://localhost:8080/big-order
//   (subscribe to the topic in the ntfy app first)

use "ntfy.qwer"

let TOPIC = env("NTFY_TOPIC", "shop-orders-demo")
let SERVER = env("NTFY_URL", null) ?? env("SINK_URL", null)

route GET /big-order {
    let order = { id: "A-1042", total: "€420.00", customer: "Ada" }
    ntfy_send(TOPIC, `**${order.customer}** ordered for ${order.total}.`, {
        server: SERVER,
        title: `Big order ${order.id}`,
        priority: "high",
        tags: ["moneybag", "shop"],
        click: `https://shop.example.com/admin/orders/${order.id}`,
        markdown: true,
        actions: [
            ntfy_action("view", "Open", `https://shop.example.com/admin/orders/${order.id}`, false, {})?,
            ntfy_action("http", "Mark packed", `https://shop.example.com/api/orders/${order.id}/packed`, true, { method: "POST", headers: { authorization: "Bearer shop-token" } })?,
        ],
    })?
}

route GET /actions = {
    view: ntfy_action("view", "Open", "https://shop.example.com", true, {})?,
    bad_kind: ntfy_action("open", "x", "https://x", false, {}).error,
    bad_topic: ntfy_send("orders and more", "hi", {}).error,
    bad_priority: ntfy_send(TOPIC, "hi", { server: SERVER, priority: 9 }).error,
}
