{
  "$schema": "http://jaycode.org/schema/document-blueprint/v1.0.0/schema",
  "lang": "JayCode",
  "type": "blueprint",
  "metadata": {
    "title": "Invoice",
    "author": "JayCode",
    "subject": "Invoice Document"
  },
  "styles": {
    "theme": "minimal",
    "themeOverrides": {
      "base": {
        "fontSize": 10,
        "fontFamily": "Helvetica"
      }
    },
    "customStyles": {
      "sectionLabel": {
        "fontSize": 8,
        "fontWeight": "bold",
        "fontColor": "#8898AA"
      },
      "boldDark": {
        "fontWeight": "bold",
        "fontColor": "#0A2540"
      },
      "muted": {
        "fontColor": "#425466",
        "fontSize": 10
      },
      "invoiceTitle": {
        "fontWeight": "bold",
        "fontSize": 26,
        "fontColor": "#635BFF",
        "hAlign": "right",
        "vAlign": "bottom"
      },
      "metaLabel": {
        "fontSize": 8,
        "fontWeight": "bold",
        "fontColor": "#8898AA"
      },
      "metaValue": {
        "fontSize": 10,
        "fontWeight": "bold",
        "fontColor": "#0A2540"
      },
      "amountDue": {
        "fontSize": 14,
        "fontWeight": "bold",
        "fontColor": "#635BFF"
      },
      "tableHeaderCell": {
        "fontSize": 8,
        "fontWeight": "bold",
        "fontColor": "#FFFFFF",
        "bgColor": "#0A2540",
        "hAlign": "left",
        "padding": [5, 8]
      },
      "tableHeaderRight": {
        "fontSize": 9,
        "fontWeight": "bold",
        "fontColor": "#FFFFFF",
        "bgColor": "#0A2540",
        "hAlign": "right",
        "padding": [5, 8]
      },
      "itemDesc": {
        "fontColor": "#0A2540",
        "vAlign": "middle"
      },
      "itemQty": {
        "fontColor": "#425466",
        "hAlign": "right",
        "vAlign": "middle"
      },
      "itemAmt": {
        "fontColor": "#0A2540",
        "hAlign": "right",
        "vAlign": "middle"
      },
      "totalLabel": {
        "fontColor": "#425466",
        "hAlign": "right",
        "fontSize": 10
      },
      "totalValue": {
        "fontColor": "#0A2540",
        "hAlign": "right",
        "fontSize": 10
      },
      "taxSummaryHeader": {
        "fontSize": 10,
        "fontWeight": "bold",
        "fontColor": "#0A2540",
        "hAlign": "left",
        "margin": [0, 0, 6, 4]
      },
      "grandTotalLabel": {
        "fontWeight": "bold",
        "fontColor": "#0A2540",
        "hAlign": "right",
        "fontSize": 11,
        "borderTop": 1,
        "borderTopColor": "#425466",
        "paddingTop": 6
      },
      "grandTotalValue": {
        "fontWeight": "bold",
        "fontColor": "#0A2540",
        "hAlign": "right",
        "fontSize": 11,
        "borderTop": 1,
        "borderTopColor": "#425466",
        "paddingTop": 6
      }
    }
  },

  "vars": {
    "invoiceNumber": "",
    "issuedDate": "",
    "dueDate": "",
    "currencyUnit": "$",
    "seller": {
      "name": "",
      "address": "",
      "city": "",
      "email": ""
    },
    "customer": {
      "name": "",
      "company": "",
      "address": "",
      "city": "",
      "email": "",
      "bankName": "",
      "bankAccount": ""
    },
    "items": [
      {
        "description": "",
        "quantity": 0,
        "vatRate": "8.5%",
        "unit": "",
        "unitPrice": 0.0,
        "amount": 0.0
      }
    ],
    "subtotal": 0.0,
    "taxAmount": 0.0,
    "discount": 0.0,
    "total": 0.0,
    "notes": "",
    "paymentLink": ""
  },

  "pageLayout": {
    "orientation": "portrait",
    "pageSize": "A4",
    "showPageNumbers": false,
    "dpi": 300,
    "margins": {
      "unit": "mm",
      "header": { "left": 0, "right": 0, "bottom": 0 },
      "body": { "top": 16, "left": 20, "right": 20, "bottom": 16 },
      "footer": { "top": 16, "left": 20, "right": 20 }
    }
  },
  "footer": [
    {
      "type": "horizontalLine",
      "style": { "margin": [0, 0, 5, 0] }
    },
    {
      "type": "paragraph",
      "value": [
        "{{ seller.name }}",
        " · ",
        "{{ seller.address }}",
        ", ",
        "{{ seller.city }}",
        " · ",
        "{{ seller.email }}"
      ],
      "style": { "fontColor": "#8898AA", "fontSize": 8, "hAlign": "center", "margin": 0 }
    }
  ],
  "body": [

    {
      "$comment": "━━━ SECTION 1: Company name (left) + INVOICE title (right) ━━━",
      "type": "table",
      "columnLayout": { "unit": "percentage", "value": [60, 40] },
      "tbody": [
        {
          "type": "row",
          "content": [
            {
              "type": "image",
              "resource": "company_logo",
              "height": 50,
              "fit": "contain",
              "style": { "vAlign": "bottom", "padding": [0, 0, 4, 0] }
            },
            {
              "type": "cell",
              "value": "INVOICE",
              "style": "invoiceTitle"
            }
          ]
        }
      ],
      "style": { "border": 0, "cellPadding": 2 }
    },

    {
      "$comment": "━━━ Divider ━━━",
      "type": "horizontalLine",
      "style": { "margin": [10, 0] }
    },

    {
      "$comment": "━━━ SECTION 2: FROM | BILL TO ━━━",
      "type": "table",
      "columnLayout": { "unit": "percentage", "value": [50, 50] },
      "tbody": [
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "FROM", "style": "sectionLabel" },
            { "type": "cell", "value": "BILL TO", "style": "sectionLabel" }
          ]
        },
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "{{ seller.name }}", "style": "boldDark" },
            { "type": "cell", "value": "{{ customer.name }}", "style": "boldDark" }
          ]
        },
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "{{ seller.address }}", "style": "muted" },
            { "type": "cell", "value": "{{ customer.address }}", "style": "muted" }
          ]
        },
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "{{ seller.city }}", "style": "muted" },
            { "type": "cell", "value": "{{ customer.city }}", "style": "muted" }
          ]
        },
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "{{ seller.email }}", "style": "muted" },
            { "type": "cell", "value": "{{ customer.email }}", "style": "muted" }
          ]
        },
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "{{ seller.bankName }}", "style": "muted" },
            { "type": "cell", "value": "" }
          ],
          "drawIf": "{{ length(seller.bankName) > 0 }}"
        },
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "{{ 'IBAN/Bank account: ' + seller.bankAccount }}", "style": "muted" },
            { "type": "cell", "value": "" }
          ],
          "drawIf": "{{ length(seller.bankAccount) > 0 }}"
        },
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "{{ seller.bankRouting }}", "style": "muted" },
            { "type": "cell", "value": "" }
          ],
          "drawIf": "{{ length(seller.bankRouting) > 0 }}"
        }
      ],
      "style": { "border": 0, "cellPadding": 2, "cellSpacing": 0 }
    },

    {
      "$comment": "━━━ Divider ━━━",
      "type": "horizontalLine",
      "style": { "margin": [10, 0] }
    },

    {
      "$comment": "━━━ SECTION 3: Invoice meta — number, dates, amount due ━━━",
      "type": "table",
      "columnLayout": { "unit": "percentage", "value": [25, 25, 25, 25] },
      "tbody": [
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "INVOICE NUMBER", "style": "metaLabel" },
            { "type": "cell", "value": "DATE ISSUED",    "style": "metaLabel" },
            { "type": "cell", "value": "DATE DUE",       "style": "metaLabel" },
            { "type": "cell", "value": "AMOUNT DUE",     "style": "metaLabel" }
          ]
        },
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "{{ invoiceNumber }}", "style": "metaValue" },
            { "type": "cell", "value": "{{ issuedDate }}",    "style": "metaValue" },
            { "type": "cell", "value": "{{ dueDate }}",       "style": "metaValue" },
            { "type": "cell", "value": "{{ total | currency(currencyUnit) }}", "style": "amountDue" }
          ]
        }
      ],
      "style": {
        "border": 0,
        "cellPadding": 10,
        "bgColor": "#F6F9FC"
      }
    },

    {
      "$comment": "━━━ SECTION 4: Line items table ━━━",
      "type": "table",
      "variant": "alternating",
      "columnLayout": { "unit": "percentage", "value": [40, 15, 15, 10, 20] },
      "thead": [
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "DESCRIPTION", "style": "tableHeaderCell" },
            { "type": "cell", "value": "QTY",  "style": "tableHeaderRight" },
            { "type": "cell", "value": "UNIT PRICE",  "style": "tableHeaderRight" },
            { "type": "cell", "value": "VAT",  "style": "tableHeaderRight" },
            { "type": "cell", "value": "NET AMOUNT",  "style": "tableHeaderRight" }
          ]
        }
      ],
      "tbody": [
        {
          "type": "repeater",
          "source": "items",
          "content": [
            {
              "type": "row",
              "content": [
                { "type": "cell", "value": "{{ item.description }}", "style": "itemDesc" },
                { "type": "cell", "value": "{{ item.quantity | toString | append(' ', item.unit) }}", "style": "itemQty" },
                { "type": "cell", "value": "{{ item.unitPrice | currency(currencyUnit) }}", "style": "itemAmt" },
                { "type": "cell", "value": "{{ item.taxRate }}", "style": "itemAmt" },
                {
                  "type": "if",
                  "condition": "{{ toBool(item.discount) }}",
                  "then": {
                    "type": "cell",
                    "value": [
                      "{{ item.amount | currency(currencyUnit) }}",
                      "\n",
                      { "type": "text", "value": "Discount", "style": { "fontSize": 7, "fontColor": "#FF0000", "margin": 0 } },
                      "\n",
                      { "type": "text", "value": "{{ '-' + (item.discount | currency(currencyUnit)) }}", "style": { "fontSize": 7, "fontColor": "#FF0000", "margin": 0 } }
                    ],
                    "style": "itemAmt"
                  },
                  "else": {
                    "type": "cell",
                    "value": "{{ item.amount | currency(currencyUnit) }}",
                    "style": "itemAmt"
                  }
                }
              ]
            }
          ]
        }
      ],
      "style": {
        "border": 0,
        "cellPadding": 8,
        "borderStyle": "solid",
        "tbody": {
          "borderBottom": 1,
          "borderBottomColor": "#E3EBF6",
          "evenRow": { "bgColor": "#FFFFFF" },
          "oddRow":  { "bgColor": "#F6F9FC" }
        }
      }
    },

    {
      "$comment": "━━━ SECTION 5: Totals - in case taxSummary is not defined ━━━",
      "type": "table",
      "drawIf": "{{ !defined(taxSummary) }}",
      "columnLayout": { "unit": "percentage", "value": [45, 35, 20] },
      "tbody": [
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "", "style": {} },
            { "type": "cell", "value": "Subtotal",    "style": "totalLabel" },
            { "type": "cell", "value": "{{ subtotal | currency(currencyUnit) }}", "style": "totalValue" }
          ]
        },
        {
          "type": "if",
          "condition": "{{ toBool(discount) }}",
          "then": [
            {
              "type": "row",
              "content": [
                { "type": "cell", "value": "", "style": {} },
                { "type": "cell", "value": "Discount", "style": "totalLabel" },
                { "type": "cell", "value": "{{ '-' + (discount | currency(currencyUnit)) }}", "style": "totalValue" }
              ]
            },
            {
              "type": "row",
              "content": [
                { "type": "cell", "value": "", "style": {} },
                { "type": "cell", "value": "Taxable Amount", "style": "totalLabel" },
                { "type": "cell", "value": "{{ (subtotal - discount) | currency(currencyUnit) }}", "style": "totalValue" }
              ]
            }
          ]
        },
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "", "style": {} },
            { "type": "cell", "value": "Tax:", "style": "totalLabel" },
            { "type": "cell", "value": "{{ taxAmount | currency(currencyUnit) }}", "style": "totalValue" }
          ]
        },
        {
          "type": "row",
          "content": [
            { "type": "cell", "value": "", "style": {} },
            { "type": "cell", "value": "Total due",  "style": "grandTotalLabel" },
            { "type": "cell", "value": "{{ total | currency(currencyUnit) }}", "style": "grandTotalValue" }
          ]
        }
      ],
      "style": { "border": 0, "cellPadding": 4 }
    },

    {
      "$comment": "━━━ SECTION 5: Totals - in case taxSummary is defined ━━━",
      "type": "table",
      "drawIf": "{{ toBool(taxSummary) }}",
      "columnLayout": { "unit": "percentage", "value": [45, 55] },
      "style": { "cellPadding": 0, "cellSpacing": 0 },
      "tbody": [
        {
          "type": "row",
          "content": [
            {
              "type": "table",
              "columnLayout": { "unit": "percentage", "value": [30, 35, 35] },
              "tbody": [
                {
                  "type": "row",
                  "content": [
                    { "value": "TAX SUMMARY", "style": "taxSummaryHeader", "colspan": 3 }
                  ]
                },
                {
                  "type": "row",
                  "content": [
                    { "type": "cell", "value": "TAX RATE", "style": "sectionLabel" },
                    { "type": "cell", "value": "TAXABLE AMOUNT", "style": "sectionLabel" },
                    { "type": "cell", "value": "TAX AMOUNT", "style": "sectionLabel" }
                  ]
                },
                {
                  "type": "repeater",
                  "source": "taxSummary",
                  "content": [
                    {
                      "type": "row",
                      "content": [
                        { "type": "cell", "value": "{{ item.taxRate }}" },
                        { "type": "cell", "value": "{{ item.taxableAmount | currency(currencyUnit) }}" },
                        { "type": "cell", "value": "{{ item.taxAmount | currency(currencyUnit) }}" }
                      ]
                    }
                  ]
                }
              ]
            },
            { 
              "type": "table",
              "columnLayout": { "unit": "percentage", "value": [60, 40] },
              "tbody": [
                {
                  "type": "row",
                  "content": [
                    { "type": "cell", "value": "Subtotal",    "style": "totalLabel" },
                    { "type": "cell", "value": "{{ subtotal | currency(currencyUnit) }}", "style": "totalValue" }
                  ]
                },
                {
                  "type": "if",
                  "condition": "{{ toBool(discount) }}",
                  "then": [
                    {
                      "type": "row",
                      "content": [
                        { "type": "cell", "value": "Discount", "style": "totalLabel" },
                        { "type": "cell", "value": "{{ '-' + (discount | currency(currencyUnit)) }}", "style": "totalValue" }
                      ]
                    },
                    {
                      "type": "row",
                      "content": [
                        { "type": "cell", "value": "Taxable Amount", "style": "totalLabel" },
                        { "type": "cell", "value": "{{ (subtotal - discount) | currency(currencyUnit) }}", "style": "totalValue" }
                      ]
                    }
                  ]
                },
                {
                  "type": "row",
                  "content": [
                    { "type": "cell", "value": "Tax:", "style": "totalLabel" },
                    { "type": "cell", "value": "{{ taxAmount | currency(currencyUnit) }}", "style": "totalValue" }
                  ]
                },
                {
                  "type": "row",
                  "content": [
                    { "type": "cell", "value": "Total due",  "style": "grandTotalLabel" },
                    { "type": "cell", "value": "{{ total | currency(currencyUnit) }}", "style": "grandTotalValue" }
                  ]
                }
              ]
            }
          ]
        }
      ]
    },

    {
      "$comment": "━━━ Hyperlink to online payment ━━━",
      "type": "hyperlink",
      "href": "{{ paymentLink }}",
      "value": "Pay your invoice online",
      "drawIf": "{{ toBool(paymentLink) }}",
      "style": {
        "borderColor": "#635BFF",
        "hAlign": "right"
      }
    },

    {
      "$comment": "━━━ SECTION 6: Notes (conditional) ━━━",
      "type": "if",
      "condition": "{{ toBool(notes) }}",
      "then": [
        {
          "type": "horizontalLine",
          "style": { "margin": [14, 0, 8, 0] }
        },
        {
          "type": "paragraph",
          "value": "NOTES",
          "style": { "fontColor": "#8898AA", "fontSize": 8, "fontWeight": "bold", "margin": [0, 0, 4, 0] }
        },
        {
          "type": "paragraph",
          "value": "{{ notes }}",
          "style": { "fontColor": "#425466", "fontSize": 9, "lineHeight": 1.5 }
        }
      ]
    }
  ]
}
