{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "/schemas/engineering-project.schema.json",
  "title": "Engineering Lab Project Traceability Model",
  "type": "object",
  "required": [
    "schema_version",
    "project",
    "entities",
    "relations",
    "governance",
    "engineering_inputs",
    "manufacturing",
    "strategic_revalidation"
  ],
  "properties": {
    "schema_version": {
      "type": "string"
    },
    "project": {
      "type": "object",
      "required": [
        "id",
        "name",
        "phase"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "phase": {
          "type": "string"
        },
        "source_document": {
          "type": "string"
        },
        "updated_at": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "entities": {
      "type": "object",
      "required": [
        "needs",
        "requirements",
        "solutions",
        "models",
        "verifications",
        "decisions",
        "evidence",
        "risks",
        "assumptions"
      ],
      "properties": {
        "needs": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/entity"
          }
        },
        "requirements": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/requirement"
          }
        },
        "solutions": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/entity"
          }
        },
        "models": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/model"
          }
        },
        "verifications": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/verification"
          }
        },
        "decisions": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/decision"
          }
        },
        "evidence": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/evidence"
          }
        },
        "risks": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/risk"
          }
        },
        "assumptions": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/assumption"
          }
        }
      },
      "additionalProperties": false
    },
    "relations": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/relation"
      }
    },
    "governance": {
      "type": "object",
      "required": [
        "gates"
      ],
      "properties": {
        "gates": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/gate"
          }
        }
      },
      "additionalProperties": true
    },
    "engineering_inputs": {
      "type": "object",
      "required": [
        "known",
        "measurements_required"
      ],
      "properties": {
        "known": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/engineeringInput"
          }
        },
        "measurements_required": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/measurementInput"
          }
        }
      },
      "additionalProperties": false
    },
    "manufacturing": {
      "type": "object",
      "required": [
        "architecture",
        "bom",
        "cost_model"
      ],
      "properties": {
        "architecture": {
          "type": "object",
          "required": [
            "strategy",
            "process_route"
          ],
          "properties": {
            "strategy": {
              "type": "string"
            },
            "process_route": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/processStep"
              }
            }
          },
          "additionalProperties": true
        },
        "bom": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/bomItem"
          }
        },
        "cost_model": {
          "type": "object",
          "required": [
            "currency",
            "basis_date",
            "scenarios"
          ],
          "properties": {
            "currency": {
              "type": "string"
            },
            "basis_date": {
              "type": "string"
            },
            "scenarios": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/costScenario"
              }
            }
          },
          "additionalProperties": true
        }
      },
      "additionalProperties": true
    },
    "strategic_revalidation": {
      "type": "object",
      "required": [
        "status",
        "spend_status",
        "engineering_baseline",
        "baseline_status",
        "baseline_finality",
        "current_objective",
        "next_sequence",
        "spend_rule"
      ],
      "properties": {
        "status": {
          "enum": [
            "active",
            "closed",
            "superseded"
          ]
        },
        "spend_status": {
          "enum": [
            "HOLD",
            "GO",
            "GO_WITH_CONDITIONS",
            "PIVOT",
            "STOP"
          ]
        },
        "engineering_baseline": {
          "type": "string"
        },
        "baseline_status": {
          "type": "string"
        },
        "baseline_finality": {
          "type": "string"
        },
        "current_objective": {
          "type": "string"
        },
        "next_sequence": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "spend_rule": {
          "type": "string"
        },
        "simplification_questions": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "additionalProperties": true
    }
  },
  "$defs": {
    "entity": {
      "type": "object",
      "required": [
        "id",
        "title",
        "status"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "title": {
          "type": "string"
        },
        "statement": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "status": {
          "type": "string"
        },
        "source": {
          "type": "string"
        },
        "next_action": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "requirement": {
      "allOf": [
        {
          "$ref": "#/$defs/entity"
        },
        {
          "type": "object",
          "required": [
            "trace_id",
            "statement",
            "priority"
          ],
          "properties": {
            "trace_id": {
              "type": "string"
            },
            "priority": {
              "enum": [
                "low",
                "medium",
                "high",
                "critical"
              ]
            },
            "acceptance_criteria": {
              "type": "string"
            },
            "acceptance_criteria_status": {
              "enum": [
                "tbd",
                "draft",
                "defined",
                "verified"
              ]
            },
            "validation_note": {
              "type": "string"
            }
          }
        }
      ]
    },
    "model": {
      "allOf": [
        {
          "$ref": "#/$defs/entity"
        },
        {
          "type": "object",
          "required": [
            "kind"
          ],
          "properties": {
            "kind": {
              "enum": [
                "cad",
                "cae",
                "calculation",
                "diagram",
                "economic_model",
                "dfm",
                "other"
              ]
            }
          }
        }
      ]
    },
    "verification": {
      "allOf": [
        {
          "$ref": "#/$defs/entity"
        },
        {
          "type": "object",
          "required": [
            "method"
          ],
          "properties": {
            "method": {
              "type": "string"
            },
            "acceptance_criteria": {
              "type": "string"
            }
          }
        }
      ]
    },
    "decision": {
      "allOf": [
        {
          "$ref": "#/$defs/entity"
        },
        {
          "type": "object",
          "properties": {
            "outcome": {
              "enum": [
                "pending",
                "accepted",
                "rejected",
                "changed",
                "hold"
              ]
            },
            "rationale": {
              "type": "string"
            }
          }
        }
      ]
    },
    "evidence": {
      "allOf": [
        {
          "$ref": "#/$defs/entity"
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string"
            },
            "uri": {
              "type": "string"
            }
          }
        }
      ]
    },
    "relation": {
      "type": "object",
      "required": [
        "id",
        "from",
        "to",
        "type"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "from": {
          "type": "string"
        },
        "to": {
          "type": "string"
        },
        "type": {
          "enum": [
            "derives_from",
            "satisfies",
            "represented_by",
            "verifies",
            "based_on",
            "supports",
            "documents",
            "mitigates",
            "addresses",
            "assumes"
          ]
        }
      },
      "additionalProperties": false
    },
    "gate": {
      "type": "object",
      "required": [
        "id",
        "title",
        "goal",
        "criteria"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "title": {
          "type": "string"
        },
        "goal": {
          "type": "string"
        },
        "criteria": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/gateCriterion"
          }
        }
      },
      "additionalProperties": true
    },
    "gateCriterion": {
      "type": "object",
      "required": [
        "id",
        "label",
        "rule"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "label": {
          "type": "string"
        },
        "rule": {
          "type": "string"
        },
        "severity": {
          "enum": [
            "required",
            "recommended"
          ]
        }
      },
      "additionalProperties": true
    },
    "engineeringInput": {
      "type": "object",
      "required": [
        "id",
        "name",
        "value",
        "status"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "value": {},
        "unit": {
          "type": "string"
        },
        "status": {
          "enum": [
            "confirmed_family",
            "confirmed_variant",
            "measured_specimen",
            "assumption"
          ]
        },
        "source_evidence": {
          "type": "string"
        },
        "note": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "measurementInput": {
      "type": "object",
      "required": [
        "id",
        "name",
        "status",
        "method",
        "why_needed"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "status": {
          "enum": [
            "needed",
            "measured",
            "not_applicable"
          ]
        },
        "value": {},
        "unit": {
          "type": "string"
        },
        "method": {
          "type": "string"
        },
        "why_needed": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "risk": {
      "allOf": [
        {
          "$ref": "#/$defs/entity"
        },
        {
          "type": "object",
          "required": [
            "severity",
            "likelihood",
            "risk_statement",
            "mitigation"
          ],
          "properties": {
            "severity": {
              "enum": [
                "low",
                "medium",
                "high",
                "critical"
              ]
            },
            "likelihood": {
              "enum": [
                "unlikely",
                "possible",
                "likely",
                "unknown"
              ]
            },
            "risk_statement": {
              "type": "string"
            },
            "mitigation": {
              "type": "string"
            },
            "residual_status": {
              "enum": [
                "open",
                "controlled",
                "accepted",
                "closed"
              ]
            }
          }
        }
      ]
    },
    "assumption": {
      "allOf": [
        {
          "$ref": "#/$defs/entity"
        },
        {
          "type": "object",
          "required": [
            "statement",
            "confidence",
            "validation_plan"
          ],
          "properties": {
            "statement": {
              "type": "string"
            },
            "confidence": {
              "enum": [
                "low",
                "medium",
                "high"
              ]
            },
            "validation_plan": {
              "type": "string"
            },
            "status": {
              "enum": [
                "unverified",
                "validated",
                "rejected",
                "superseded"
              ]
            }
          }
        }
      ]
    },
    "processStep": {
      "type": "object",
      "required": [
        "id",
        "name",
        "process",
        "status"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "process": {
          "type": "string"
        },
        "status": {
          "enum": [
            "planned",
            "validated",
            "supplier_required",
            "tbd"
          ]
        },
        "note": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "bomItem": {
      "type": "object",
      "required": [
        "id",
        "group",
        "name",
        "make_buy",
        "qty",
        "status"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "group": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "make_buy": {
          "enum": [
            "make",
            "buy",
            "mixed"
          ]
        },
        "qty": {
          "type": "number"
        },
        "unit": {
          "type": "string"
        },
        "material": {
          "type": "string"
        },
        "process": {
          "type": "string"
        },
        "status": {
          "enum": [
            "costing_assumption",
            "defined",
            "validated",
            "tbd"
          ]
        },
        "cost_low_rub": {
          "type": "number"
        },
        "cost_high_rub": {
          "type": "number"
        },
        "note": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "costScenario": {
      "type": "object",
      "required": [
        "id",
        "name",
        "quantity",
        "direct_cost_low_rub",
        "direct_cost_high_rub",
        "status"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "quantity": {
          "type": "integer"
        },
        "direct_cost_low_rub": {
          "type": "number"
        },
        "direct_cost_high_rub": {
          "type": "number"
        },
        "status": {
          "enum": [
            "rough_order_of_magnitude",
            "supplier_quote",
            "validated"
          ]
        },
        "exclusions": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "note": {
          "type": "string"
        }
      },
      "additionalProperties": true
    }
  }
}
