{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:absolutems:wp_invoices:invoice_result:v1",
  "title": "Invoice result v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "invoice_id",
    "status",
    "document",
    "supplier",
    "invoice_number",
    "invoice_date",
    "currency",
    "items",
    "subtotal",
    "tax",
    "total",
    "validations",
    "warnings",
    "confidence",
    "processing"
  ],
  "properties": {
    "invoice_id": {"type": "string", "minLength": 1, "maxLength": 128},
    "status": {"enum": ["queued", "processing", "completed", "failed"]},
    "document": {
      "type": "object",
      "additionalProperties": false,
      "required": ["filename", "content_type", "size_bytes", "artifact_id"],
      "properties": {
        "filename": {"type": "string", "minLength": 1, "maxLength": 255},
        "content_type": {"type": "string", "minLength": 1, "maxLength": 128},
        "size_bytes": {"type": "integer", "minimum": 0},
        "artifact_id": {"type": ["string", "null"], "maxLength": 128}
      }
    },
    "supplier": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "tax_id"],
      "properties": {
        "name": {"type": ["string", "null"]},
        "tax_id": {"type": ["string", "null"]}
      }
    },
    "invoice_number": {"type": ["string", "null"]},
    "invoice_date": {"type": ["string", "null"]},
    "currency": {"type": "string", "pattern": "^[A-Z]{3}$"},
    "items": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["description", "quantity", "unit_price", "tax", "line_total", "confidence"],
        "properties": {
          "product_code": {"type": ["string", "null"], "pattern": "^\\d{4,8}$"},
          "description": {"type": ["string", "null"]},
          "quantity": {"type": ["number", "null"]},
          "unit_price": {"type": ["number", "null"]},
          "tax": {"type": ["number", "null"]},
          "line_total": {"type": ["number", "null"]},
          "confidence": {"type": "number", "minimum": 0, "maximum": 1}
        }
      }
    },
    "subtotal": {"type": ["number", "null"]},
    "tax": {"type": ["number", "null"]},
    "total": {"type": ["number", "null"]},
    "validations": {
      "type": "object",
      "additionalProperties": false,
      "required": ["items_sum_matches_subtotal", "subtotal_plus_tax_matches_total", "mathematically_consistent"],
      "properties": {
        "items_sum_matches_subtotal": {"type": ["boolean", "null"]},
        "subtotal_plus_tax_matches_total": {"type": ["boolean", "null"]},
        "mathematically_consistent": {"type": "boolean"}
      }
    },
    "warnings": {"type": "array", "items": {"type": "string"}},
    "confidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["document_type", "overall"],
      "properties": {
        "document_type": {"type": "number", "minimum": 0, "maximum": 1},
        "overall": {"type": "number", "minimum": 0, "maximum": 1}
      }
    },
    "processing": {
      "type": "object",
      "additionalProperties": false,
      "required": ["engine", "extractor_version"],
      "properties": {
        "engine": {"type": "string", "minLength": 1, "maxLength": 64},
        "extractor_version": {"type": "string", "minLength": 1, "maxLength": 64}
      }
    }
  }
}
