{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "mirr_temporal_netlist.schema.json",
  "title": "MIRR Temporal Netlist",
  "description": "Canonical JSON schema for the MIRR Temporal Netlist IR (Level 3 IR). Version 1.0.",
  "type": "object",
  "required": ["ir_version", "guards", "signals", "statistics"],
  "additionalProperties": false,
  "properties": {
    "ir_version": { "type": "string", "const": "1.0" },
    "guards":     { "type": "array", "items": { "$ref": "#/definitions/CompiledGuard" } },
    "signals":    { "type": "array", "items": { "$ref": "#/definitions/GeneratedSignal" } },
    "statistics": { "$ref": "#/definitions/CompilationStatistics" }
  },
  "definitions": {
    "CompiledGuard": {
      "oneOf": [
        {
          "type": "object",
          "required": ["ShiftRegister"],
          "additionalProperties": false,
          "properties": {
            "ShiftRegister": { "$ref": "#/definitions/ShiftRegisterGuard" }
          }
        },
        {
          "type": "object",
          "required": ["Counter"],
          "additionalProperties": false,
          "properties": {
            "Counter": { "$ref": "#/definitions/CounterGuard" }
          }
        },
        {
          "type": "object",
          "required": ["Complex"],
          "additionalProperties": false,
          "properties": {
            "Complex": { "$ref": "#/definitions/ComplexGuard" }
          }
        }
      ]
    },
    "ShiftRegisterGuard": {
      "type": "object",
      "required": ["name", "input_signal", "output_signal", "stages", "delay_cycles", "condition_kind"],
      "additionalProperties": false,
      "properties": {
        "name":           { "type": "string", "minLength": 1 },
        "input_signal":   { "type": "string", "minLength": 1 },
        "output_signal":  { "type": "string", "minLength": 1 },
        "stages":         { "type": "array", "items": { "type": "string" } },
        "delay_cycles":   { "type": "integer", "minimum": 0 },
        "condition_kind": { "$ref": "#/definitions/ConditionKind" }
      }
    },
    "CounterGuard": {
      "type": "object",
      "required": ["name", "input_signal", "output_signal", "counter_signal", "comparator_signal", "target_count", "condition_kind"],
      "additionalProperties": false,
      "properties": {
        "name":               { "type": "string", "minLength": 1 },
        "input_signal":       { "type": "string", "minLength": 1 },
        "output_signal":      { "type": "string", "minLength": 1 },
        "counter_signal":     { "type": "string", "minLength": 1 },
        "comparator_signal":  { "type": "string", "minLength": 1 },
        "target_count":       { "type": "integer", "minimum": 0 },
        "condition_kind":     { "$ref": "#/definitions/ConditionKind" }
      }
    },
    "ComplexGuard": {
      "type": "object",
      "required": ["name", "sub_guards", "output_signal", "combination_logic"],
      "additionalProperties": false,
      "properties": {
        "name":              { "type": "string", "minLength": 1 },
        "sub_guards":        { "type": "array", "items": { "$ref": "#/definitions/CompiledGuard" } },
        "output_signal":     { "type": "string", "minLength": 1 },
        "combination_logic": { "$ref": "#/definitions/Expr" }
      }
    },
    "ConditionKind": {
      "oneOf": [
        {
          "type": "object",
          "required": ["SimpleSignal"],
          "additionalProperties": false,
          "properties": {
            "SimpleSignal": { "type": "string", "minLength": 1 }
          }
        },
        {
          "type": "object",
          "required": ["NegatedSignal"],
          "additionalProperties": false,
          "properties": {
            "NegatedSignal": { "type": "string", "minLength": 1 }
          }
        },
        {
          "type": "object",
          "required": ["Comparison"],
          "additionalProperties": false,
          "properties": {
            "Comparison": {
              "type": "object",
              "required": ["signal", "op", "value"],
              "additionalProperties": false,
              "properties": {
                "signal": { "type": "string", "minLength": 1 },
                "op":     { "$ref": "#/definitions/ComparisonOp" },
                "value":  { "$ref": "#/definitions/LiteralValue" }
              }
            }
          }
        }
      ]
    },
    "ComparisonOp": {
      "type": "string",
      "enum": ["Eq", "Ne", "Lt", "Le", "Gt", "Ge"]
    },
    "GeneratedSignal": {
      "type": "object",
      "required": ["name", "ty", "kind", "source"],
      "additionalProperties": false,
      "properties": {
        "name":   { "type": "string", "minLength": 1 },
        "ty":     { "$ref": "#/definitions/SignalType" },
        "kind":   { "$ref": "#/definitions/GeneratedSignalKind" },
        "source": { "oneOf": [{ "type": "null" }, { "$ref": "#/definitions/Expr" }] }
      }
    },
    "GeneratedSignalKind": {
      "type": "string",
      "enum": ["ShiftRegisterStage", "Counter", "Comparator", "LogicGate", "Intermediate"]
    },
    "CompilationStatistics": {
      "type": "object",
      "required": [
        "shift_registers_used",
        "counters_used",
        "logic_gates_used",
        "max_delay_cycles",
        "total_signals",
        "compilation_time_us"
      ],
      "additionalProperties": false,
      "properties": {
        "shift_registers_used": { "type": "integer", "minimum": 0 },
        "counters_used":        { "type": "integer", "minimum": 0 },
        "logic_gates_used":     { "type": "integer", "minimum": 0 },
        "max_delay_cycles":     { "type": "integer", "minimum": 0 },
        "total_signals":        { "type": "integer", "minimum": 0 },
        "compilation_time_us":  { "oneOf": [{ "type": "null" }, { "type": "integer", "minimum": 0 }] }
      }
    },
    "SignalType": {
      "oneOf": [
        { "type": "string", "const": "Bool" },
        {
          "type": "object",
          "required": ["Unsigned"],
          "additionalProperties": false,
          "properties": {
            "Unsigned": { "type": "integer", "minimum": 1, "maximum": 64 }
          }
        }
      ]
    },
    "LiteralValue": {
      "oneOf": [
        {
          "type": "object",
          "required": ["Bool"],
          "additionalProperties": false,
          "properties": { "Bool": { "type": "boolean" } }
        },
        {
          "type": "object",
          "required": ["Integer"],
          "additionalProperties": false,
          "properties": { "Integer": { "type": "integer", "minimum": 0 } }
        }
      ]
    },
    "Expr": {
      "oneOf": [
        {
          "type": "object",
          "required": ["Literal"],
          "additionalProperties": false,
          "properties": { "Literal": { "$ref": "#/definitions/LiteralValue" } }
        },
        {
          "type": "object",
          "required": ["Signal"],
          "additionalProperties": false,
          "properties": { "Signal": { "type": "string", "minLength": 1 } }
        },
        {
          "type": "object",
          "required": ["Unary"],
          "additionalProperties": false,
          "properties": {
            "Unary": {
              "type": "object",
              "required": ["op", "operand"],
              "additionalProperties": false,
              "properties": {
                "op":      { "type": "string", "const": "Not" },
                "operand": { "$ref": "#/definitions/Expr" }
              }
            }
          }
        },
        {
          "type": "object",
          "required": ["Binary"],
          "additionalProperties": false,
          "properties": {
            "Binary": {
              "type": "object",
              "required": ["op", "left", "right"],
              "additionalProperties": false,
              "properties": {
                "op":    { "type": "string", "enum": ["And","Or","Xor","Lt","Le","Gt","Ge","Eq","Ne","Add","Sub","Mul","Shl","Shr"] },
                "left":  { "$ref": "#/definitions/Expr" },
                "right": { "$ref": "#/definitions/Expr" }
              }
            }
          }
        }
      ]
    }
  }
}