{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://mybearings.world/schemas/bearing-record-draft-v0.1.schema.json",
  "title": "MyBearings draft bearing record envelope v0.1",
  "description": "Proposed minimal envelope for explicit synthetic fixtures; not a complete engineering schema or a deployed URL.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "synthetic",
    "product_id",
    "manufacturer",
    "designation",
    "family",
    "attributes",
    "sources",
    "relationships"
  ],
  "properties": {
    "schema_version": {
      "const": "0.1-draft"
    },
    "synthetic": {
      "type": "boolean"
    },
    "product_id": {
      "type": "string",
      "minLength": 1
    },
    "manufacturer": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "name"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "name": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "designation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "raw",
        "normalized"
      ],
      "properties": {
        "raw": {
          "type": "string",
          "minLength": 1
        },
        "normalized": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "family": {
      "type": [
        "string",
        "null"
      ]
    },
    "attributes": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/attribute"
      }
    },
    "sources": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "id",
          "kind",
          "title",
          "retrieved_at",
          "permitted_use"
        ],
        "properties": {
          "id": {
            "type": "string",
            "minLength": 1
          },
          "kind": {
            "enum": [
              "manufacturer_document",
              "manufacturer_feed",
              "supplier_listing",
              "customer_file",
              "rule_definition",
              "synthetic_fixture"
            ]
          },
          "title": {
            "type": "string"
          },
          "url": {
            "type": [
              "string",
              "null"
            ],
            "format": "uri"
          },
          "retrieved_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "permitted_use": {
            "enum": [
              "public_reference",
              "licensed_distribution",
              "tenant_private",
              "synthetic_demo",
              "unknown"
            ]
          }
        }
      }
    },
    "relationships": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "target_id",
          "type",
          "status",
          "source_ids"
        ],
        "properties": {
          "target_id": {
            "type": "string"
          },
          "type": {
            "enum": [
              "same_identity_candidate",
              "variant",
              "candidate_substitute",
              "supersession"
            ]
          },
          "status": {
            "enum": [
              "proposed",
              "reviewed",
              "rejected"
            ]
          },
          "source_ids": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      }
    }
  },
  "$defs": {
    "attribute": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "field",
        "scope",
        "status",
        "applicability",
        "value",
        "unit",
        "assertions"
      ],
      "properties": {
        "field": {
          "type": "string",
          "minLength": 1
        },
        "scope": {
          "enum": [
            "manufacturer_product",
            "supplier_listing",
            "inventory_lot"
          ]
        },
        "status": {
          "enum": [
            "populated",
            "unknown",
            "not_applicable",
            "conflicting",
            "not_researched"
          ]
        },
        "applicability": {
          "enum": [
            "applicable",
            "not_applicable",
            "unknown"
          ]
        },
        "value": {
          "type": [
            "string",
            "number",
            "boolean",
            "null"
          ]
        },
        "unit": {
          "type": [
            "string",
            "null"
          ]
        },
        "assertions": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "value",
              "unit",
              "source_id",
              "source_location",
              "method",
              "review_status"
            ],
            "properties": {
              "value": {
                "type": [
                  "string",
                  "number",
                  "boolean",
                  "null"
                ]
              },
              "unit": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "source_id": {
                "type": "string"
              },
              "source_location": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "raw_label": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "raw_value": {
                "type": [
                  "string",
                  "number",
                  "boolean",
                  "null"
                ]
              },
              "method": {
                "enum": [
                  "source_extraction",
                  "documented_rule",
                  "human_entry",
                  "synthetic_demo"
                ]
              },
              "review_status": {
                "enum": [
                  "unreviewed",
                  "rule_validated",
                  "expert_reviewed",
                  "synthetic_only"
                ]
              }
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "const": "populated"
              }
            }
          },
          "then": {
            "properties": {
              "value": {
                "not": {
                  "type": "null"
                }
              },
              "assertions": {
                "minItems": 1
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "enum": [
                  "unknown",
                  "not_applicable",
                  "conflicting",
                  "not_researched"
                ]
              }
            }
          },
          "then": {
            "properties": {
              "value": {
                "type": "null"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "conflicting"
              }
            }
          },
          "then": {
            "properties": {
              "assertions": {
                "minItems": 2
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "not_applicable"
              }
            }
          },
          "then": {
            "properties": {
              "applicability": {
                "const": "not_applicable"
              }
            }
          }
        }
      ]
    }
  }
}
