# =invoice: fill a form, get a Markdown invoice with correct totals.
#
# What this app teaches, in three lines:
# 1. An `inputs:` form turns each field into an argv slot; the script does the real validation.
# 2. Money is integer cents in the script, so totals never drift like floats do.
# 3. One declared output file, created with O_EXCL: it refuses to overwrite and says so.
#
# Fill client, up to three line items (description, quantity, unit price), tax
# percent and due days, then press Create invoice. The pane previews the
# invoice. The file is saved to ~/Downloads as invoice-<client>-<date>.md.
# Enter opens it; Tab gives Reveal and "Copy as email body" (Open and Reveal
# come from `emits: File`, not from this manifest).
#
# Sample data: type demo as the client. You get a fabricated invoice marked
# SAMPLE DATA, and nothing is saved. Bad input (missing price, bad number, an
# existing file of the same name) comes back as a plain "Invoice not saved" row.
schema_version: 1
runtime: bridge
id: invoice
chip: invoice
title: Invoice
icon: file-text
description: "Build a Markdown invoice from a form and save it to Downloads. Client demo shows a sample"
placeholder_examples: ["Fill the form and press Create invoice"]
emits: File

inputs:
  - { name: client,   type: text,   label: Client,         required: true, placeholder: "Acme Co (or demo)", max_length: 80 }
  - { name: item1,    type: text,   label: Item 1,         placeholder: "Design work", max_length: 80 }
  - { name: qty1,     type: number, label: Item 1 qty,     default: 1, min: 0, step: 0.01 }
  - { name: price1,   type: number, label: Item 1 price,   min: 0, step: 0.01, placeholder: "150.00" }
  - { name: item2,    type: text,   label: Item 2,         max_length: 80 }
  - { name: qty2,     type: number, label: Item 2 qty,     default: 1, min: 0, step: 0.01 }
  - { name: price2,   type: number, label: Item 2 price,   min: 0, step: 0.01 }
  - { name: item3,    type: text,   label: Item 3,         max_length: 80 }
  - { name: qty3,     type: number, label: Item 3 qty,     default: 1, min: 0, step: 0.01 }
  - { name: price3,   type: number, label: Item 3 price,   min: 0, step: 0.01 }
  - { name: tax,      type: number, label: Tax percent,    default: 0, min: 0, max: 100, step: 0.01 }
  - { name: due_days, type: number, label: Due in days,    default: 30, min: 0, max: 365, step: 1 }

submit:
  label: Create invoice

bridge:
  command:
    macos: "python3 {{scripts_dir}}/bolt-invoice.py {{client}} {{item1}} {{qty1}} {{price1}} {{item2}} {{qty2}} {{price2}} {{item3}} {{qty3}} {{price3}} {{tax}} {{due_days}}"
    linux: "python3 {{scripts_dir}}/bolt-invoice.py {{client}} {{item1}} {{qty1}} {{price1}} {{item2}} {{qty2}} {{price2}} {{item3}} {{qty3}} {{price3}} {{tax}} {{due_days}}"
    windows: "python {{scripts_dir}}/bolt-invoice.py {{client}} {{item1}} {{qty1}} {{price1}} {{item2}} {{qty2}} {{price2}} {{item3}} {{qty3}} {{price3}} {{tax}} {{due_days}}"
  parse: json
  timeout_seconds: 8

presentation:
  widget: list
  title_field: title
  subtitle_field: subtitle
  right_widget:
    kind: markdown
    mode: selected_row
    bind:
      title: "{{row.title}}"
      subtitle: "{{row.subtitle}}"
      body: "{{row.body}}"
      actions: [copy-email]

# No `primary: true`: Enter keeps the file behaviour from `emits: File`.
actions:
  - { id: copy-email, kind: composer, target: row, label: Copy as email, insert: "{{row.email}}" }
