{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://appwrite-utils.dev/schemas/collection.schema.json",
  "title": "Appwrite Collection Definition",
  "description": "Schema for defining Appwrite collections in YAML",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "description": "The name of the collection"
    },
    "id": {
      "type": "string",
      "description": "The ID of the collection (optional, auto-generated if not provided)",
      "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]{0,35}$"
    },
    "documentSecurity": {
      "type": "boolean",
      "default": false,
      "description": "Enable document-level permissions"
    },
    "enabled": {
      "type": "boolean",
      "default": true,
      "description": "Whether the collection is enabled"
    },
    "permissions": {
      "type": "array",
      "description": "Collection-level permissions",
      "items": {
        "type": "object",
        "properties": {
          "permission": {
            "type": "string",
            "enum": [
              "read",
              "create",
              "update",
              "delete"
            ],
            "description": "The permission type"
          },
          "target": {
            "type": "string",
            "description": "Permission target (e.g., 'any', 'users', 'users/verified', 'label:admin')"
          }
        },
        "required": [
          "permission",
          "target"
        ],
        "additionalProperties": false
      }
    },
    "attributes": {
      "type": "array",
      "description": "Collection attributes (fields)",
      "items": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string",
            "description": "Attribute name",
            "pattern": "^[a-zA-Z][a-zA-Z0-9]*$"
          },
          "type": {
            "type": "string",
            "enum": [
              "string",
              "integer",
              "float",
              "boolean",
              "datetime",
              "email",
              "ip",
              "url",
              "enum",
              "relationship"
            ],
            "description": "Attribute data type"
          },
          "size": {
            "type": "number",
            "description": "Maximum size for string attributes",
            "minimum": 1,
            "maximum": 1073741824
          },
          "required": {
            "type": "boolean",
            "default": false,
            "description": "Whether the attribute is required"
          },
          "array": {
            "type": "boolean",
            "default": false,
            "description": "Whether the attribute is an array"
          },
          "default": {
            "description": "Default value for the attribute"
          },
          "description": {
            "type": "string",
            "description": "Attribute description"
          },
          "min": {
            "type": "number",
            "description": "Minimum value for numeric attributes"
          },
          "max": {
            "type": "number",
            "description": "Maximum value for numeric attributes"
          },
          "elements": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Allowed values for enum attributes"
          },
          "relatedCollection": {
            "type": "string",
            "description": "Related collection name for relationship attributes"
          },
          "relationType": {
            "type": "string",
            "enum": [
              "oneToOne",
              "oneToMany",
              "manyToOne",
              "manyToMany"
            ],
            "description": "Type of relationship"
          },
          "twoWay": {
            "type": "boolean",
            "description": "Whether the relationship is bidirectional"
          },
          "twoWayKey": {
            "type": "string",
            "description": "Key name for the reverse relationship"
          },
          "onDelete": {
            "type": "string",
            "enum": [
              "cascade",
              "restrict",
              "setNull"
            ],
            "description": "Action to take when related document is deleted"
          },
          "side": {
            "type": "string",
            "enum": [
              "parent",
              "child"
            ],
            "description": "Side of the relationship"
          }
        },
        "required": [
          "key",
          "type"
        ],
        "additionalProperties": false,
        "allOf": [
          {
            "if": {
              "properties": {
                "type": {
                  "const": "enum"
                }
              }
            },
            "then": {
              "required": [
                "elements"
              ]
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "relationship"
                }
              }
            },
            "then": {
              "required": [
                "relatedCollection",
                "relationType"
              ]
            }
          }
        ]
      }
    },
    "indexes": {
      "type": "array",
      "description": "Database indexes for the collection",
      "items": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string",
            "description": "Index name"
          },
          "type": {
            "type": "string",
            "enum": [
              "key",
              "fulltext",
              "unique"
            ],
            "description": "Index type"
          },
          "attributes": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Attributes to index",
            "minItems": 1
          },
          "orders": {
            "type": "array",
            "items": {
              "type": "string",
              "enum": [
                "ASC",
                "DESC"
              ]
            },
            "description": "Sort order for each attribute"
          }
        },
        "required": [
          "key",
          "type",
          "attributes"
        ],
        "additionalProperties": false
      }
    },
    "importDefs": {
      "type": "array",
      "description": "Import definitions for data migration",
      "default": []
    }
  },
  "required": [
    "name"
  ],
  "additionalProperties": false
}