// A payroll file for the bank in a fixed-width format, and the bank's return file read back.
//
//   quillo run fixed_width.qwe      then open http://localhost:8080/payroll.txt

use "fixed_width.qwer"

let SPEC = [
    { name: "record", width: 2, type: "text" },
    { name: "account", width: 34, type: "text" },
    { name: "name", width: 24, type: "text" },
    { name: "amount", width: 12, type: "decimal", decimals: 2 },
    { name: "pay_date", width: 8, type: "date", format: "YYYYMMDD" },
    { name: "spare", width: 6, type: "filler" },
    { name: "reference", width: 10, type: "text", align: "right", pad: "0" },
]

let PAY = [
    { record: "01", account: "NL91ABNA0417164300", name: "Ada Lovelace", amount: "4250.00", pay_date: "2026-11-25", reference: "1042" },
    { record: "01", account: "GB29NWBK60161331926819", name: "Grace Hopper", amount: 3980.5, pay_date: "2026-11-25", reference: "1043" },
]

route GET /payroll.txt = respond(200, fixed_write(PAY, SPEC, "\r\n")?, { "content-type": "text/plain" })

route GET /return {
    let text = fixed_write(PAY, SPEC, "\r\n")?
    fixed_parse(text, SPEC)?
}

route GET /too_long = { error: fixed_write([{ ...PAY[1], name: "Grace Brewster Murray Hopper" }], SPEC, "\n").error }
