{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://casoon.github.io/typst-business-templates/schema/invoice-config.schema.json",
  "title": "Invoice configuration",
  "description": "User-owned version-1 tax and presentation configuration. Rates are explicit inputs; national applicability is not inferred. Monetary totals round half away from zero to two decimal places.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "version",
    "presentation",
    "default_tax",
    "taxes"
  ],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "version": {
      "const": 1
    },
    "presentation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "language",
        "format_locale",
        "currency",
        "layout"
      ],
      "properties": {
        "language": {
          "enum": [
            "de",
            "en",
            "es",
            "fr",
            "it",
            "nl",
            "pt"
          ]
        },
        "format_locale": {
          "enum": [
            "de-DE",
            "en-GB",
            "fr-FR",
            "en-US",
            "es-ES",
            "it-IT",
            "nl-NL",
            "pt-PT"
          ]
        },
        "currency": {
          "type": "string",
          "pattern": "^[A-Z]{3}$"
        },
        "layout": {
          "enum": [
            "din",
            "international"
          ]
        },
        "paper": {
          "enum": [
            "a4",
            "us-letter"
          ],
          "default": "a4"
        },
        "currency_display": {
          "enum": [
            "auto",
            "code",
            "symbol"
          ],
          "default": "auto",
          "description": "Auto uses $ for USD with en-US; explicit symbol currently supports USD."
        },
        "tax_label": {
          "type": "string",
          "minLength": 1,
          "description": "Override the invoice tax column heading."
        }
      }
    },
    "default_tax": {
      "type": "string",
      "minLength": 1,
      "description": "Key in taxes used when an invoice line omits tax."
    },
    "taxes": {
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "minLength": 1
      },
      "additionalProperties": {
        "oneOf": [
          {
            "$ref": "#/$defs/tax"
          },
          {
            "$ref": "#/$defs/sales-tax"
          }
        ]
      }
    },
    "required_fields": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "type": "string",
        "minLength": 1
      },
      "description": "Required input paths rooted at company, data or facts; * traverses arrays."
    },
    "notes": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "Appended after invoice-specific notes; rendered in PDF and included in CII."
    },
    "display_fields": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "path",
          "labels"
        ],
        "properties": {
          "path": {
            "type": "string"
          },
          "labels": {
            "type": "object",
            "additionalProperties": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      }
    },
    "tax_model": {
      "enum": [
        "vat",
        "sales-tax"
      ],
      "default": "vat"
    }
  },
  "$defs": {
    "tax": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "category",
        "rate"
      ],
      "properties": {
        "category": {
          "enum": [
            "S",
            "Z",
            "E",
            "AE"
          ],
          "description": "Standard/reduced taxable, zero-rated, exempt, or reverse charge."
        },
        "rate": {
          "type": "string",
          "pattern": "^\\d{1,3}(\\.\\d{1,4})?$",
          "description": "Explicit percentage: S above zero up to 100; Z/E/AE exactly zero."
        },
        "exemption_reason": {
          "type": "string",
          "minLength": 1
        },
        "exemption_reason_code": {
          "type": "string",
          "minLength": 1
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "category": {
                "const": "E"
              }
            }
          },
          "then": {
            "anyOf": [
              {
                "required": [
                  "exemption_reason"
                ]
              },
              {
                "required": [
                  "exemption_reason_code"
                ]
              }
            ]
          }
        }
      ]
    },
    "sales-tax": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "components"
      ],
      "properties": {
        "components": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/component"
          },
          "description": "Additive components on the same net basis; empty for untaxed lines."
        }
      }
    },
    "component": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "label",
        "rate"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "label": {
          "type": "string",
          "minLength": 1
        },
        "rate": {
          "type": "string",
          "pattern": "^\\d{1,3}(\\.\\d{1,4})?$"
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "required": [
          "tax_model"
        ],
        "properties": {
          "tax_model": {
            "const": "sales-tax"
          }
        }
      },
      "then": {
        "properties": {
          "taxes": {
            "additionalProperties": {
              "$ref": "#/$defs/sales-tax"
            }
          }
        }
      },
      "else": {
        "properties": {
          "taxes": {
            "additionalProperties": {
              "$ref": "#/$defs/tax"
            }
          }
        }
      }
    }
  ]
}
