{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Database Schema Location Registry",
  "description": "项目内 docs/schemas/SCHEMA-REGISTRY.json 的位置记忆契约，支持单来源项目和多来源、多数据库项目",
  "type": "object",
  "required": ["version", "default_project_directory"],
  "additionalProperties": false,
  "properties": {
    "$schema": {
      "type": "string"
    },
    "version": {
      "enum": [1, 2]
    },
    "default_project_directory": {
      "const": "docs/schemas"
    },
    "entries": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Entry"
      }
    },
    "systems": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/definitions/System"
      },
      "description": "多来源、多数据库项目中的来源系统和数据库系统目录"
    },
    "documents": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/definitions/Document"
      },
      "description": "多来源、多数据库项目中的 schema 文档目录和派生关系"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "version": { "const": 1 }
        }
      },
      "then": {
        "required": ["entries"]
      }
    },
    {
      "if": {
        "properties": {
          "version": { "const": 2 }
        }
      },
      "then": {
        "required": ["systems", "documents"]
      }
    }
  ],
  "definitions": {
    "Entry": {
      "oneOf": [
        {
          "type": "object",
          "required": ["table_name", "document", "storage"],
          "additionalProperties": false,
          "properties": {
            "table_name": {
              "type": "string",
              "pattern": "^[a-zA-Z_][a-zA-Z0-9_.]*$"
            },
            "document": {
              "type": "string",
              "minLength": 1,
              "description": "schema 文档路径；project 时相对项目根"
            },
            "storage": {
              "const": "project"
            },
            "last_verified": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        {
          "type": "object",
          "required": ["table_name", "document", "storage", "shared_root", "project_reference"],
          "additionalProperties": false,
          "properties": {
            "table_name": {
              "type": "string",
              "pattern": "^[a-zA-Z_][a-zA-Z0-9_.]*$"
            },
            "document": {
              "type": "string",
              "minLength": 1,
              "description": "保留用户明确指定的 shared schema canonical 路径"
            },
            "storage": {
              "const": "shared"
            },
            "shared_root": {
              "type": "string",
              "minLength": 1
            },
            "project_reference": {
              "type": "string",
              "minLength": 1,
              "description": "项目文档内对 shared schema 的引用说明或链接"
            },
            "last_verified": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      ]
    },
    "System": {
      "type": "object",
      "required": ["id", "kind", "name"],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-zA-Z_][a-zA-Z0-9_.-]*$",
          "description": "项目内稳定的来源或数据库系统标识"
        },
        "kind": {
          "enum": ["source", "database"]
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "engine": {
          "type": "string",
          "minLength": 1,
          "description": "数据库引擎；source 类型可记录 api、csv、vendor-db 等来源形态"
        },
        "purpose": {
          "type": "string",
          "minLength": 1
        },
        "environment": {
          "type": "string",
          "minLength": 1
        },
        "description": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "Document": {
      "type": "object",
      "required": ["schema_id", "entity", "role", "document", "storage"],
      "additionalProperties": false,
      "properties": {
        "schema_id": {
          "type": "string",
          "pattern": "^[a-zA-Z_][a-zA-Z0-9_.-]*$",
          "description": "项目内稳定且唯一的 schema 文档标识，不随物理表名变化"
        },
        "entity": {
          "type": "string",
          "pattern": "^[a-zA-Z_][a-zA-Z0-9_.-]*$",
          "description": "跨来源、跨数据库保持不变的业务实体标识"
        },
        "role": {
          "enum": ["source", "canonical", "target"],
          "description": "source=来源契约，canonical=业务规范，target=数据库落地投影"
        },
        "system_id": {
          "type": "string",
          "pattern": "^[a-zA-Z_][a-zA-Z0-9_.-]*$",
          "description": "source/target 文档所属的系统目录项；canonical 文档不填写"
        },
        "document": {
          "type": "string",
          "minLength": 1,
              "description": "schema 文档路径；project 时相对项目根，shared 时保留用户明确指定的公共路径"
        },
        "storage": {
          "enum": ["project", "shared"]
        },
        "shared_root": {
          "type": "string",
          "minLength": 1
        },
        "project_reference": {
          "type": "string",
          "minLength": 1,
          "description": "项目文档内对 shared schema 的引用说明或链接"
        },
        "derived_from": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[a-zA-Z_][a-zA-Z0-9_.-]*$"
          },
          "description": "该 schema 从哪些 schema_id 派生；用于表达 source→canonical→target 关系"
        },
        "last_verified": {
          "type": "string",
          "minLength": 1
        },
        "description": {
          "type": "string",
          "minLength": 1
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "role": { "enum": ["source", "target"] }
            }
          },
          "then": {
            "required": ["system_id"]
          }
        },
        {
          "if": {
            "properties": {
              "role": { "const": "canonical" }
            }
          },
          "then": {
            "not": {
              "required": ["system_id"]
            }
          }
        },
        {
          "if": {
            "properties": {
              "storage": { "const": "shared" }
            }
          },
          "then": {
            "required": ["shared_root", "project_reference"]
          }
        }
      ]
    }
  }
}
