{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Papercut Tutorial 1.0.0",
  "type": "object",
  "additionalProperties": false,
  "required": ["schema_version", "tutorial_id", "status", "request", "project", "materials", "design", "states", "steps", "assets", "validation"],
  "properties": {
    "schema_version": {"const": "1.0.0"},
    "tutorial_id": {"$ref": "#/$defs/id"},
    "status": {"enum": ["draft", "partial", "ready"]},
    "request": {
      "type": "object", "additionalProperties": false,
      "required": ["text", "locale", "assumptions", "image_mode"],
      "properties": {
        "text": {"$ref": "#/$defs/text"}, "locale": {"$ref": "#/$defs/text"},
        "assumptions": {"type": "array", "items": {"$ref": "#/$defs/text"}},
        "image_mode": {"enum": ["hybrid", "svg_only"]}
      }
    },
    "project": {
      "type": "object", "additionalProperties": false,
      "required": ["title", "subject", "difficulty", "estimated_minutes", "technique", "paper"],
      "properties": {
        "title": {"$ref": "#/$defs/text"}, "subject": {"$ref": "#/$defs/text"},
        "difficulty": {"enum": ["beginner", "intermediate", "advanced"]},
        "estimated_minutes": {"type": "integer", "minimum": 1},
        "technique": {"enum": ["flat_cut", "fold_cut"]},
        "paper": {
          "type": "object", "additionalProperties": false,
          "required": ["width_mm", "height_mm", "gsm", "color"],
          "properties": {
            "width_mm": {"type": "number", "exclusiveMinimum": 0},
            "height_mm": {"type": "number", "exclusiveMinimum": 0},
            "gsm": {"type": "number", "exclusiveMinimum": 0}, "color": {"type": "string", "pattern": "^#[0-9a-fA-F]{6}$"}
          }
        }
      }
    },
    "materials": {
      "type": "array", "minItems": 1,
      "items": {
        "type": "object", "additionalProperties": false,
        "required": ["id", "name", "quantity", "unit", "notes"],
        "properties": {
          "id": {"$ref": "#/$defs/id"}, "name": {"$ref": "#/$defs/text"},
          "quantity": {"type": "number", "exclusiveMinimum": 0},
          "unit": {"$ref": "#/$defs/text"}, "notes": {"type": "string"}
        }
      }
    },
    "design": {
      "type": "object", "additionalProperties": false,
      "required": ["id", "unit", "origin", "geometry_type", "outline", "regions", "cut_paths", "folds", "minimum_bridge_mm"],
      "properties": {
        "id": {"$ref": "#/$defs/id"}, "unit": {"const": "mm"}, "origin": {"const": "top_left"},
        "geometry_type": {"enum": ["polygon", "svg"]},
        "outline": {"type": "array", "minItems": 3, "items": {"$ref": "#/$defs/point"}},
        "regions": {
          "type": "array", "minItems": 1,
          "items": {
            "type": "object", "additionalProperties": false,
            "required": ["id", "kind", "geometry"],
            "properties": {
              "id": {"$ref": "#/$defs/id"}, "kind": {"enum": ["keep", "remove"]},
              "geometry": {"$ref": "#/$defs/geometry"}
            }
          }
        },
        "cut_paths": {
          "type": "array", "minItems": 1,
          "items": {
            "type": "object", "additionalProperties": false,
            "required": ["id", "points", "svg_path", "description"],
            "properties": {
              "id": {"$ref": "#/$defs/id"},
              "points": {"type": "array", "items": {"$ref": "#/$defs/point"}},
              "svg_path": {"type": ["string", "null"]}, "description": {"$ref": "#/$defs/text"}
            }
          }
        },
        "folds": {
          "type": "array", "items": {
            "type": "object", "additionalProperties": false,
            "required": ["id", "line", "direction", "layers_after", "transform"],
            "properties": {
              "id": {"$ref": "#/$defs/id"},
              "line": {"type": "array", "minItems": 2, "maxItems": 2, "items": {"$ref": "#/$defs/point"}},
              "direction": {"$ref": "#/$defs/text"}, "layers_after": {"type": "integer", "minimum": 2},
              "transform": {"type": "array", "minItems": 6, "maxItems": 6, "items": {"type": "number"}}
            }
          }
        },
        "minimum_bridge_mm": {"type": ["number", "null"], "exclusiveMinimum": 0}
      }
    },
    "states": {
      "type": "array", "minItems": 2,
      "items": {
        "type": "object", "additionalProperties": false,
        "required": ["id", "design_id", "label", "layers", "fold_ids", "traced", "completed_cut_ids", "detached", "orientation"],
        "properties": {
          "id": {"$ref": "#/$defs/id"}, "design_id": {"$ref": "#/$defs/id"}, "label": {"$ref": "#/$defs/text"},
          "layers": {"type": "integer", "minimum": 1}, "fold_ids": {"$ref": "#/$defs/ids"},
          "traced": {"type": "boolean"}, "completed_cut_ids": {"$ref": "#/$defs/ids"},
          "detached": {"type": "boolean"}, "orientation": {"$ref": "#/$defs/text"}
        }
      }
    },
    "steps": {
      "type": "array", "minItems": 1,
      "items": {
        "type": "object", "additionalProperties": false,
        "required": ["id", "order", "action", "title", "instruction", "before_state_id", "after_state_id", "cut_ids", "material_ids", "asset_ids", "checkpoint", "caution"],
        "properties": {
          "id": {"$ref": "#/$defs/id"}, "order": {"type": "integer", "minimum": 1},
          "action": {"enum": ["prepare", "fold", "trace", "cut", "unfold", "finish"]},
          "title": {"$ref": "#/$defs/text"}, "instruction": {"$ref": "#/$defs/text"},
          "before_state_id": {"$ref": "#/$defs/id"}, "after_state_id": {"$ref": "#/$defs/id"},
          "cut_ids": {"$ref": "#/$defs/ids"}, "material_ids": {"$ref": "#/$defs/ids"},
          "asset_ids": {"type": "array", "minItems": 1, "uniqueItems": true, "items": {"$ref": "#/$defs/id"}},
          "checkpoint": {"$ref": "#/$defs/text"}, "caution": {"type": "string"}
        }
      }
    },
    "assets": {
      "type": "array", "minItems": 1,
      "items": {
        "type": "object", "additionalProperties": false,
        "required": ["id", "design_id", "role", "status", "path", "media_type", "alt", "source", "reason", "prompt", "state_ids"],
        "properties": {
          "id": {"$ref": "#/$defs/id"}, "design_id": {"$ref": "#/$defs/id"},
          "role": {"enum": ["step_diagram", "template", "final_preview", "cover"]},
          "status": {"enum": ["pending", "generated", "failed", "skipped"]},
          "path": {"type": ["string", "null"], "minLength": 1},
          "media_type": {"enum": ["image/svg+xml", "image/png", "image/jpeg", "image/webp"]},
          "alt": {"$ref": "#/$defs/text"}, "source": {"enum": ["geometry", "ai"]},
          "reason": {"type": ["string", "null"]}, "prompt": {"type": ["string", "null"]},
          "state_ids": {"type": "array", "minItems": 1, "uniqueItems": true, "items": {"$ref": "#/$defs/id"}}
        },
        "allOf": [
          {"if": {"properties": {"status": {"const": "generated"}}}, "then": {"properties": {"path": {"type": "string", "minLength": 1}}}, "else": {"properties": {"path": {"type": "null"}}}},
          {"if": {"properties": {"status": {"enum": ["failed", "skipped", "pending"]}}}, "then": {"properties": {"reason": {"type": "string", "minLength": 1}}}}
        ]
      }
    },
    "validation": {
      "type": "object", "additionalProperties": false,
      "required": ["geometry_review", "visual_review", "physical_trial", "limitations"],
      "properties": {
        "geometry_review": {"$ref": "#/$defs/check"}, "visual_review": {"$ref": "#/$defs/check"},
        "physical_trial": {"$ref": "#/$defs/check"},
        "limitations": {"type": "array", "items": {"$ref": "#/$defs/text"}}
      }
    }
  },
  "$defs": {
    "id": {"type": "string", "pattern": "^[a-z][a-z0-9_-]*$", "maxLength": 80},
    "text": {"type": "string", "minLength": 1},
    "ids": {"type": "array", "uniqueItems": true, "items": {"$ref": "#/$defs/id"}},
    "point": {"type": "array", "minItems": 2, "maxItems": 2, "items": {"type": "number"}},
    "geometry": {
      "type": "object", "additionalProperties": false,
      "required": ["points", "svg_path"],
      "properties": {
        "points": {"type": "array", "items": {"$ref": "#/$defs/point"}},
        "svg_path": {"type": ["string", "null"]}
      }
    },
    "check": {
      "type": "object", "additionalProperties": false,
      "required": ["result", "method", "evidence"],
      "properties": {
        "result": {"enum": ["not_checked", "passed", "failed", "not_tested"]},
        "method": {"type": "string"}, "evidence": {"type": "string"}
      },
      "allOf": [{"if": {"properties": {"result": {"const": "passed"}}}, "then": {"properties": {"method": {"minLength": 1}, "evidence": {"minLength": 1}}}}]
    }
  }
}
