{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://intelligentliving.art/schemas/sources/v1.json",
  "title": "Living Art Source Registry v1",
  "type": "object",
  "required": [
    "type",
    "version",
    "sources"
  ],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "type": {
      "const": "https://intelligentliving.art/protocols/source-registry/v1.json"
    },
    "version": {
      "type": "string"
    },
    "sources": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id",
          "title",
          "kind",
          "lifecycle",
          "materialPolicy",
          "rights",
          "locators",
          "allowedUses",
          "cache"
        ],
        "properties": {
          "id": {
            "type": "string",
            "pattern": "^[a-z0-9.-]+$"
          },
          "title": {
            "type": "string",
            "minLength": 1
          },
          "kind": {
            "type": "string",
            "minLength": 1
          },
          "lifecycle": {
            "enum": [
              "active",
              "superseded",
              "withdrawn"
            ]
          },
          "materialPolicy": {
            "enum": [
              "embedded",
              "reference-only",
              "runtime-licensed",
              "private",
              "withdrawn"
            ]
          },
          "rights": {
            "type": "object",
            "required": [
              "basis",
              "reviewRequired"
            ],
            "properties": {
              "basis": {
                "enum": [
                  "cc0",
                  "public-domain",
                  "creator-authorised",
                  "licensed",
                  "reference-only",
                  "unknown",
                  "withdrawn"
                ]
              },
              "reviewRequired": {
                "type": "boolean"
              },
              "attribution": {
                "type": "string"
              },
              "jurisdiction": {
                "type": "string"
              },
              "expiresAt": {
                "type": "string",
                "format": "date-time"
              },
              "notes": {
                "type": "string"
              }
            },
            "additionalProperties": false
          },
          "locators": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "kind",
                "value"
              ],
              "properties": {
                "kind": {
                  "enum": [
                    "uri",
                    "ipfs",
                    "arweave",
                    "https",
                    "catalogue",
                    "search",
                    "api",
                    "private-id"
                  ]
                },
                "value": {
                  "type": "string",
                  "minLength": 1
                },
                "role": {
                  "enum": [
                    "primary",
                    "mirror",
                    "archive",
                    "recovery",
                    "origin",
                    "discovery",
                    "private"
                  ]
                },
                "format": {
                  "enum": [
                    "content",
                    "car-v1"
                  ]
                },
                "sha256": {
                  "$ref": "#/$defs/hash"
                },
                "ipfsRootCid": {
                  "type": "string",
                  "minLength": 1
                },
                "domainAllowlist": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                }
              },
              "additionalProperties": false
            }
          },
          "allowedUses": {
            "type": "array",
            "items": {
              "enum": [
                "discover",
                "retrieve",
                "index",
                "paraphrase",
                "quote",
                "persist",
                "generate"
              ]
            },
            "uniqueItems": true
          },
          "cache": {
            "type": "object",
            "required": [
              "mode"
            ],
            "properties": {
              "mode": {
                "enum": [
                  "none",
                  "request",
                  "session",
                  "ttl",
                  "permanent"
                ]
              },
              "ttlSeconds": {
                "type": "integer",
                "minimum": 1
              }
            },
            "additionalProperties": false
          },
          "replication": {
            "type": "object",
            "required": [
              "strategy",
              "minimumLocations",
              "repairIpfsFromArchive"
            ],
            "properties": {
              "strategy": {
                "enum": [
                  "none",
                  "ipfs-pinned",
                  "ipfs-arweave"
                ]
              },
              "minimumLocations": {
                "type": "integer",
                "minimum": 0
              },
              "repairIpfsFromArchive": {
                "type": "boolean"
              }
            },
            "additionalProperties": false
          },
          "supersededBy": {
            "type": "string"
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "replication": {
                  "properties": {
                    "strategy": {
                      "const": "ipfs-arweave"
                    }
                  },
                  "required": [
                    "strategy"
                  ]
                }
              },
              "required": [
                "replication"
              ]
            },
            "then": {
              "properties": {
                "materialPolicy": {
                  "const": "embedded"
                },
                "allowedUses": {
                  "contains": {
                    "const": "persist"
                  }
                },
                "locators": {
                  "allOf": [
                    {
                      "contains": {
                        "properties": {
                          "kind": {
                            "const": "ipfs"
                          }
                        },
                        "required": [
                          "kind"
                        ]
                      }
                    },
                    {
                      "contains": {
                        "properties": {
                          "kind": {
                            "const": "arweave"
                          }
                        },
                        "required": [
                          "kind"
                        ]
                      }
                    }
                  ]
                }
              }
            }
          }
        ],
        "additionalProperties": false
      }
    }
  },
  "$defs": {
    "hash": {
      "type": "string",
      "pattern": "^(0x)?[a-fA-F0-9]{64}$"
    }
  },
  "additionalProperties": false
}
