{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://developers.besso.com/integrations/v1/import-run.schema.json",
  "title": "Besso import run, schema version 1",
  "description": "The body of POST /api/integrations/v1/import_runs. Leave an empty field out rather than sending null.",
  "type": "object",
  "allOf": [
    { "$ref": "#/$defs/envelope" },
    { "properties": { "records": { "items": { "$ref": "#/$defs/record" } } } }
  ],
  "$defs": {
    "envelope": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema_version", "mode", "records"],
      "properties": {
        "schema_version": { "const": 1 },
        "source_system": { "type": "string", "minLength": 1, "maxLength": 50 },
        "mode": { "enum": ["full", "changes"] },
        "extracted_at": { "type": "string", "format": "date-time" },
        "client_run_id": { "type": "string", "minLength": 1, "maxLength": 100 },
        "records": { "type": "array", "minItems": 1, "maxItems": 30000 }
      },
      "if": { "title": "Full run", "properties": { "mode": { "const": "full" } }, "required": ["mode"] },
      "then": { "required": ["extracted_at"] },
      "else": { "title": "Changes run" }
    },
    "record": {
      "type": "object",
      "if": { "title": "Delete", "properties": { "action": { "const": "delete" } }, "required": ["action"] },
      "then": { "$ref": "#/$defs/delete_record" },
      "else": { "$ref": "#/$defs/upsert_record" }
    },
    "delete_record": {
      "title": "Delete",
      "type": "object",
      "additionalProperties": false,
      "required": ["record_id", "action"],
      "properties": {
        "record_id": { "$ref": "#/$defs/record_id" },
        "action": { "const": "delete" }
      }
    },
    "upsert_record": {
      "title": "Upsert",
      "type": "object",
      "additionalProperties": false,
      "required": ["record_id", "import_country", "export_country", "origin_country", "hs_code",
                   "import_date", "shipment_date", "customs_value"],
      "properties": {
        "record_id": { "$ref": "#/$defs/record_id" },
        "action": { "const": "upsert" },
        "import_country": { "$ref": "#/$defs/country" },
        "export_country": { "$ref": "#/$defs/country" },
        "origin_country": { "$ref": "#/$defs/country" },
        "hs_code": { "$ref": "#/$defs/hs_code" },
        "product_description": { "type": "string", "maxLength": 2000 },
        "import_date": { "$ref": "#/$defs/date" },
        "shipment_date": { "$ref": "#/$defs/date" },
        "incoterm": { "enum": ["EXW", "FCA", "CPT", "CIP", "DAP", "DPU", "DDP", "FAS", "FOB", "CFR", "CIF"] },
        "quantity": { "$ref": "#/$defs/quantity" },
        "net_weight": { "$ref": "#/$defs/weight" },
        "customs_value": { "$ref": "#/$defs/money" },
        "ex_works_value": { "$ref": "#/$defs/money" },
        "fob_value": { "$ref": "#/$defs/money" },
        "additional_costs": { "$ref": "#/$defs/money" },
        "duty_paid": { "$ref": "#/$defs/money" },
        "import_supplier": { "$ref": "#/$defs/supplier" },
        "export_supplier": { "$ref": "#/$defs/supplier" },
        "materials": { "type": "array", "maxItems": 500, "items": { "$ref": "#/$defs/material" } }
      }
    },
    "record_id": { "type": "string", "minLength": 1, "maxLength": 100, "pattern": "^\\S(.*\\S)?$" },
    "country": { "type": "string", "pattern": "^[A-Z]{2}$" },
    "hs_code": { "type": "string", "pattern": "^[0-9]{6,12}$" },
    "date": { "type": "string", "format": "date" },
    "amount": {
      "type": ["string", "number"],
      "pattern": "^[0-9]{1,13}(\\.[0-9]{1,4})?$",
      "minimum": 0,
      "maximum": 9999999999999.9999,
      "multipleOf": 0.0001
    },
    "decimal": {
      "type": ["string", "number"],
      "pattern": "^[0-9]{1,16}(\\.[0-9]{1,6})?$",
      "minimum": 0,
      "maximum": 9999999999999999.999999,
      "multipleOf": 0.000001
    },
    "money": {
      "type": "object",
      "additionalProperties": false,
      "required": ["amount", "currency"],
      "properties": {
        "amount": { "$ref": "#/$defs/amount" },
        "currency": { "type": "string", "pattern": "^[A-Z]{3}$" }
      }
    },
    "quantity": {
      "type": "object",
      "additionalProperties": false,
      "required": ["value"],
      "properties": {
        "value": { "type": "integer", "minimum": 0, "maximum": 2147483647 },
        "unit": { "type": "string", "minLength": 1, "maxLength": 20 }
      }
    },
    "weight": {
      "type": "object",
      "additionalProperties": false,
      "required": ["value", "unit"],
      "properties": {
        "value": { "$ref": "#/$defs/decimal" },
        "unit": { "enum": ["g", "kg"] }
      }
    },
    "supplier": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "country"],
      "properties": {
        "name": { "type": "string", "minLength": 1, "maxLength": 255 },
        "country": { "$ref": "#/$defs/country" },
        "site_name": { "type": "string", "maxLength": 255 },
        "supplier_id": { "type": "string", "minLength": 1, "maxLength": 100 }
      }
    },
    "material": {
      "type": "object",
      "additionalProperties": false,
      "required": ["hs_code", "origin_country"],
      "properties": {
        "hs_code": { "$ref": "#/$defs/hs_code" },
        "origin_country": { "$ref": "#/$defs/country" },
        "description": { "type": "string", "maxLength": 2000 },
        "quantity": { "$ref": "#/$defs/quantity" },
        "net_weight": { "$ref": "#/$defs/weight" },
        "invoice_value": { "$ref": "#/$defs/money" },
        "cif_value": { "$ref": "#/$defs/money" }
      }
    }
  }
}
