// A shop's Pix checkout: a static code ("copia e cola") for an order, read back as a banking app
// reads it, each kind of key told apart, and what a changed code or a wrong key gives.
//
//   quillo run pix.qwe      then open http://localhost:8080/code, /code.svg, /read, /manual, /keys and /refused

use "pix.qwer"

let ORDER = { key: "ada@example.com.br", name: "Padaria São João", city: "São Paulo", amount: "42.5", description: "Pedido 1001", txid: "PEDIDO1001", postcode: "01310-100" }

route GET /code = respond(200, pix_build(ORDER)?, { "content-type": "text/plain" })

route GET /code.svg = respond(200, pix_svg(pix_build(ORDER)?, 300)?, { "content-type": "image/svg+xml" })

route GET /read = pix_parse(pix_build(ORDER)?)?

// The example of the Banco Central's BR Code manual, a random key and no amount.
route GET /manual = pix_parse("00020126580014br.gov.bcb.pix0136123e4567-e12b-12d1-a456-4266554400005204000053039865802BR5913Fulano de Tal6008BRASILIA62070503***63041D3D")?

route GET /keys = {
    cpf: pix_key_type("11144477735")?,
    cnpj: pix_key_type("11222333000181")?,
    phone: pix_key_type("+5511987654321")?,
    email: pix_key_type("ada@example.com.br")?,
    random: pix_key_type("123e4567-e12b-12d1-a456-426655440000")?,
    cleaned: [pix_key_clean("111.444.777-35")?, pix_key_clean("11.222.333/0001-81")?, pix_key_clean("(11) 98765-4321")?],
}

route GET /refused {
    let good = pix_build(ORDER)?
    {
        changed: pix_parse(replace(good, "42.50", "12.50")).error,
        bad_cpf: pix_key_type("11144477736").error,
        all_same: pix_key_type("11111111111").error,
        bad_cnpj: pix_key_type("11222333000182").error,
        phone: pix_key_type("+15551234567").error,
        decimals: pix_build(merge(ORDER, { amount: "1.999" })).error,
        txid: pix_build(merge(ORDER, { txid: "not allowed!" })).error,
        no_city: pix_build(merge(ORDER, { city: "" })).error,
    }
}
