{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Universal Business Objects 1.0",
  "type": "object",
  "required": [
    "schema_version",
    "objects"
  ],
  "additionalProperties": false,
  "properties": {
    "schema_version": {
      "const": "1.0.0"
    },
    "objects": {
      "type": "array",
      "maxItems": 250,
      "items": {
        "type": "object",
        "required": [
          "id",
          "revision",
          "type",
          "name",
          "created_at",
          "source",
          "data",
          "relations"
        ],
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "string",
            "pattern": "^ubo-[a-f0-9-]{36}$"
          },
          "revision": {
            "type": "integer",
            "minimum": 1,
            "maximum": 250
          },
          "type": {
            "enum": [
              "product",
              "customer",
              "supplier",
              "order",
              "recipe",
              "quote",
              "project",
              "kpi",
              "evidence"
            ]
          },
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 150
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "source": {
            "type": "string",
            "maxLength": 2000
          },
          "data": {
            "type": "object"
          },
          "relations": {
            "type": "array",
            "maxItems": 30,
            "items": {
              "type": "object",
              "required": [
                "id",
                "revision"
              ],
              "additionalProperties": false,
              "properties": {
                "id": {
                  "type": "string",
                  "pattern": "^ubo-[a-f0-9-]{36}$"
                },
                "revision": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 250
                }
              }
            }
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "type": {
                  "const": "product"
                }
              }
            },
            "then": {
              "properties": {
                "data": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "sku",
                    "unit",
                    "attributes"
                  ],
                  "properties": {
                    "sku": {
                      "type": "string",
                      "description": "SKU",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "unit": {
                      "type": "string",
                      "description": "Jednostka sprzedaży",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "attributes": {
                      "type": "string",
                      "description": "Atrybuty (CSV: Atrybut;Wartość)",
                      "minLength": 0,
                      "maxLength": 60000
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "customer"
                }
              }
            },
            "then": {
              "properties": {
                "data": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "code",
                    "segment",
                    "notes"
                  ],
                  "properties": {
                    "code": {
                      "type": "string",
                      "description": "Kod klienta",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "segment": {
                      "type": "string",
                      "description": "Segment",
                      "minLength": 0,
                      "maxLength": 200
                    },
                    "notes": {
                      "type": "string",
                      "description": "Uwagi handlowe",
                      "minLength": 0,
                      "maxLength": 4000
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "supplier"
                }
              }
            },
            "then": {
              "properties": {
                "data": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "code",
                    "scope",
                    "notes"
                  ],
                  "properties": {
                    "code": {
                      "type": "string",
                      "description": "Kod dostawcy",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "scope": {
                      "type": "string",
                      "description": "Zakres dostaw",
                      "minLength": 0,
                      "maxLength": 4000
                    },
                    "notes": {
                      "type": "string",
                      "description": "Warunki / ograniczenia",
                      "minLength": 0,
                      "maxLength": 4000
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "order"
                }
              }
            },
            "then": {
              "properties": {
                "data": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "code",
                    "quantity",
                    "unit",
                    "requirements"
                  ],
                  "properties": {
                    "code": {
                      "type": "string",
                      "description": "Numer zlecenia",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "quantity": {
                      "type": "string",
                      "description": "Ilość",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "unit": {
                      "type": "string",
                      "description": "Jednostka",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "requirements": {
                      "type": "string",
                      "description": "Wymagania",
                      "minLength": 0,
                      "maxLength": 4000
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "recipe"
                }
              }
            },
            "then": {
              "properties": {
                "data": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "lines",
                    "quantity",
                    "margin"
                  ],
                  "properties": {
                    "lines": {
                      "type": "string",
                      "description": "Receptura (CSV: Pozycja;Ilość_na_szt;Koszt_jednostki_PLN;Koszt_partii_PLN)",
                      "minLength": 1,
                      "maxLength": 60000
                    },
                    "quantity": {
                      "type": "string",
                      "description": "Wielkość partii (szt.)",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "margin": {
                      "type": "string",
                      "description": "Docelowa marża (%)",
                      "minLength": 1,
                      "maxLength": 200
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "quote"
                }
              }
            },
            "then": {
              "properties": {
                "data": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "code",
                    "quantity",
                    "price",
                    "terms"
                  ],
                  "properties": {
                    "code": {
                      "type": "string",
                      "description": "Numer oferty",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "quantity": {
                      "type": "string",
                      "description": "Ilość (szt.)",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "price": {
                      "type": "string",
                      "description": "Cena sztuki netto (PLN)",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "terms": {
                      "type": "string",
                      "description": "Ważność i warunki",
                      "minLength": 1,
                      "maxLength": 4000
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "project"
                }
              }
            },
            "then": {
              "properties": {
                "data": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "code",
                    "goal",
                    "owner"
                  ],
                  "properties": {
                    "code": {
                      "type": "string",
                      "description": "Kod projektu",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "goal": {
                      "type": "string",
                      "description": "Cel projektu",
                      "minLength": 1,
                      "maxLength": 4000
                    },
                    "owner": {
                      "type": "string",
                      "description": "Odpowiedzialność",
                      "minLength": 0,
                      "maxLength": 200
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "kpi"
                }
              }
            },
            "then": {
              "properties": {
                "data": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "metric",
                    "period",
                    "unit",
                    "actual",
                    "target"
                  ],
                  "properties": {
                    "metric": {
                      "type": "string",
                      "description": "Nazwa miernika",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "period": {
                      "type": "string",
                      "description": "Okres (RRRR-MM)",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "unit": {
                      "type": "string",
                      "description": "Jednostka",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "actual": {
                      "type": "string",
                      "description": "Wynik",
                      "minLength": 0,
                      "maxLength": 200
                    },
                    "target": {
                      "type": "string",
                      "description": "Cel",
                      "minLength": 0,
                      "maxLength": 200
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "evidence"
                }
              }
            },
            "then": {
              "properties": {
                "source": {
                  "minLength": 1
                },
                "data": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "kind",
                    "date",
                    "observation",
                    "limitations"
                  ],
                  "properties": {
                    "kind": {
                      "type": "string",
                      "description": "Typ informacji (HIPOTEZA / DEKLARACJA / OBSERWACJA / POMIAR / TRANSAKCJA)",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "date": {
                      "type": "string",
                      "description": "Data obserwacji (RRRR-MM-DD)",
                      "minLength": 1,
                      "maxLength": 200
                    },
                    "observation": {
                      "type": "string",
                      "description": "Treść obserwacji",
                      "minLength": 1,
                      "maxLength": 4000
                    },
                    "limitations": {
                      "type": "string",
                      "description": "Ograniczenia",
                      "minLength": 1,
                      "maxLength": 4000
                    }
                  }
                }
              }
            }
          }
        ]
      }
    }
  }
}
