{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://foundryapparatus.affonso.dev/schemas/v1/recipe.schema.json",
  "title": "Foundry Apparatus v1 - recipe",
  "description": "Machine recipe or acquisition crafting recipe (foundry_apparatus/recipes/, or data/<namespace>/recipe/ in datapacks).",
  "type": "object",
  "allOf": [{ "$ref": "common.schema.json#/$defs/definition_header" }],
  "required": ["type"],
  "properties": {
    "type": { "enum": ["foundry_apparatus:machine_recipe", "foundry_apparatus:crafting_shaped", "foundry_apparatus:crafting_shapeless"] },
    "enabled": { "type": "boolean", "default": true }
  },
  "oneOf": [
    { "$ref": "#/$defs/machine_recipe" },
    { "$ref": "#/$defs/crafting_shaped" },
    { "$ref": "#/$defs/crafting_shapeless" }
  ],
  "unevaluatedProperties": false,
  "$defs": {
    "machine_recipe": {
      "type": "object",
      "required": ["type", "machines", "duration", "inputs", "outputs"],
      "properties": {
        "type": { "const": "foundry_apparatus:machine_recipe" },
        "priority": { "description": "Higher runs first; ties broken by id.", "type": "integer", "default": 0 },
        "machines": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "$ref": "common.schema.json#/$defs/identifier" } },
        "duration": { "description": "Duration in ticks.", "type": "integer", "minimum": 1 },
        "inputs": { "type": "array", "items": { "$ref": "common.schema.json#/$defs/resource_amount" } },
        "outputs": {
          "description": "Outputs produced on completion: a resource amount (always produced, or rolled independently with 'chance'), or a one_of group (exactly one of its results, or none).",
          "type": "array",
          "items": {
            "anyOf": [
              { "$ref": "common.schema.json#/$defs/resource_amount" },
              { "$ref": "#/$defs/output_group" }
            ]
          }
        },
        "conditions": { "type": "array", "items": { "$ref": "common.schema.json#/$defs/typed_entry" } }
      }
    },
    "output_group": {
      "description": "Exactly one result is picked per completion: each result's 'chance' is an exclusive slice of one roll (percent, decimals allowed); the slices must sum to at most 100 and the remainder produces nothing. An optional group 'chance' first decides whether the group rolls at all.",
      "type": "object",
      "required": ["one_of"],
      "properties": {
        "one_of": {
          "type": "array",
          "minItems": 1,
          "items": {
            "allOf": [
              { "$ref": "common.schema.json#/$defs/resource_amount" },
              { "required": ["chance"] }
            ]
          }
        },
        "chance": { "$ref": "common.schema.json#/$defs/chance" }
      },
      "additionalProperties": false
    },
    "ingredient": {
      "$ref": "common.schema.json#/$defs/resource_ref"
    },
    "result": {
      "type": "object",
      "required": ["id"],
      "properties": {
        "id": { "$ref": "common.schema.json#/$defs/identifier" },
        "count": { "type": "integer", "minimum": 1, "maximum": 99, "default": 1 },
        "components": { "$ref": "common.schema.json#/$defs/components_patch" }
      },
      "additionalProperties": false
    },
    "crafting_shaped": {
      "type": "object",
      "required": ["type", "pattern", "key", "result"],
      "properties": {
        "type": { "const": "foundry_apparatus:crafting_shaped" },
        "pattern": { "type": "array", "minItems": 1, "maxItems": 3, "items": { "type": "string", "minLength": 1, "maxLength": 3 } },
        "key": {
          "type": "object",
          "propertyNames": { "type": "string", "pattern": "^[^ ]$" },
          "additionalProperties": { "$ref": "#/$defs/ingredient" }
        },
        "result": { "$ref": "#/$defs/result" }
      }
    },
    "crafting_shapeless": {
      "type": "object",
      "required": ["type", "ingredients", "result"],
      "properties": {
        "type": { "const": "foundry_apparatus:crafting_shapeless" },
        "ingredients": { "type": "array", "minItems": 1, "maxItems": 9, "items": { "$ref": "#/$defs/ingredient" } },
        "result": { "$ref": "#/$defs/result" }
      }
    }
  }
}
