{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://sagaantipodas.com/ia/aventuras/sangre-y-poder/v3/state-schema.json",
  "title": "Sangre y poder runtime state v3",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "session_id",
    "contract_version",
    "state_schema_version",
    "source_revision",
    "runtime_version",
    "route",
    "scene",
    "dalma",
    "economy",
    "events",
    "knowledge",
    "terminal"
  ],
  "properties": {
    "session_id": {
      "type": "string",
      "minLength": 1
    },
    "contract_version": {
      "const": "3.0"
    },
    "state_schema_version": {
      "const": "3.0"
    },
    "source_revision": {
      "type": "string",
      "minLength": 1
    },
    "runtime_version": {
      "type": "string",
      "minLength": 1
    },
    "route": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "locked"
      ],
      "properties": {
        "name": {
          "enum": [
            "free",
            "normal"
          ]
        },
        "locked": {
          "const": true
        }
      }
    },
    "scene": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "current_id",
        "status"
      ],
      "properties": {
        "current_id": {
          "type": "string"
        },
        "previous_id": {
          "type": [
            "string",
            "null"
          ]
        },
        "status": {
          "enum": [
            "ready",
            "active",
            "paused",
            "completed"
          ]
        }
      }
    },
    "dalma": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "vida",
        "pri",
        "inventory"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "vida": {
          "type": "object",
          "required": [
            "current",
            "max"
          ],
          "properties": {
            "current": {
              "type": "integer",
              "minimum": 0
            },
            "max": {
              "type": "integer",
              "minimum": 1
            }
          },
          "additionalProperties": false
        },
        "pri": {
          "type": "object",
          "required": [
            "current",
            "max"
          ],
          "properties": {
            "current": {
              "type": "integer",
              "minimum": 0
            },
            "max": {
              "type": "integer",
              "minimum": 0
            }
          },
          "additionalProperties": false
        },
        "inventory": {
          "type": "object",
          "required": [
            "equipment",
            "items",
            "unique_find_ids"
          ],
          "properties": {
            "equipment": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "uniqueItems": true
            },
            "items": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "unique_find_ids": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "uniqueItems": true
            }
          },
          "additionalProperties": false
        }
      }
    },
    "economy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "remaining_money"
      ],
      "properties": {
        "remaining_money": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "events": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "applied_ids"
      ],
      "properties": {
        "applied_ids": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        }
      }
    },
    "knowledge": {
      "type": "object",
      "additionalProperties": {
        "type": [
          "boolean",
          "null",
          "string",
          "number"
        ]
      }
    },
    "terminal": {
      "type": "boolean"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "terminal": {
            "const": true
          }
        }
      },
      "then": {
        "properties": {
          "scene": {
            "properties": {
              "status": {
                "enum": [
                  "completed",
                  "paused"
                ]
              }
            }
          }
        }
      }
    }
  ],
  "x-invariants": [
    "state.dalma.vida.current <= state.dalma.vida.max",
    "state.economy.remaining_money >= 0",
    "null no se convierte en false sin evidencia",
    "event_id no se aplica dos veces",
    "unique_find_id no se concede dos veces",
    "terminal=true impide nuevas escenas con el mismo Dalma",
    "delivered_to_asher=true implica with_dalma=false",
    "scene_19_completed=true desactiva la variante A",
    "route_locked=true impide cambiar Free/Normal",
    "una transición requiere estado persistido y evento registrado"
  ]
}
