{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://www.schemastore.org/vespertide-model.json",
  "$defs": {
    "ColumnDef": {
      "type": "object",
      "properties": {
        "comment": {
          "type": ["string", "null"]
        },
        "default": {
          "anyOf": [
            {
              "$ref": "#/$defs/DefaultValue"
            },
            {
              "type": "null"
            }
          ]
        },
        "foreign_key": {
          "anyOf": [
            {
              "$ref": "#/$defs/ForeignKeySyntax"
            },
            {
              "type": "null"
            }
          ]
        },
        "index": {
          "anyOf": [
            {
              "$ref": "#/$defs/StrOrBoolOrArray"
            },
            {
              "type": "null"
            }
          ]
        },
        "name": {
          "type": "string"
        },
        "nullable": {
          "type": "boolean"
        },
        "primary_key": {
          "anyOf": [
            {
              "$ref": "#/$defs/PrimaryKeySyntax"
            },
            {
              "type": "null"
            }
          ]
        },
        "type": {
          "$ref": "#/$defs/ColumnType"
        },
        "unique": {
          "anyOf": [
            {
              "$ref": "#/$defs/StrOrBoolOrArray"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": ["name", "type", "nullable"]
    },
    "ColumnType": {
      "anyOf": [
        {
          "$ref": "#/$defs/SimpleColumnType"
        },
        {
          "$ref": "#/$defs/ComplexColumnType"
        }
      ]
    },
    "ComplexColumnType": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "varchar"
            },
            "length": {
              "type": "integer",
              "format": "uint32",
              "minimum": 0
            }
          },
          "required": ["kind", "length"]
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "numeric"
            },
            "precision": {
              "type": "integer",
              "format": "uint32",
              "minimum": 0
            },
            "scale": {
              "type": "integer",
              "format": "uint32",
              "minimum": 0
            }
          },
          "required": ["kind", "precision", "scale"]
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "char"
            },
            "length": {
              "type": "integer",
              "format": "uint32",
              "minimum": 0
            }
          },
          "required": ["kind", "length"]
        },
        {
          "type": "object",
          "properties": {
            "custom_type": {
              "type": "string"
            },
            "kind": {
              "type": "string",
              "const": "custom"
            }
          },
          "required": ["kind", "custom_type"]
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "enum"
            },
            "name": {
              "type": "string"
            },
            "values": {
              "$ref": "#/$defs/EnumValues"
            }
          },
          "required": ["kind", "name", "values"]
        }
      ]
    },
    "DefaultValue": {
      "description": "A value that can be a string, boolean, or number.\nThis is used for default values where columns can use literal values directly.",
      "anyOf": [
        {
          "type": "boolean"
        },
        {
          "type": "integer",
          "format": "int64"
        },
        {
          "type": "number",
          "format": "double"
        },
        {
          "type": "string"
        }
      ]
    },
    "EnumValues": {
      "description": "Enum values definition - either all string or all integer",
      "anyOf": [
        {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/NumValue"
          }
        }
      ]
    },
    "ForeignKeyDef": {
      "type": "object",
      "properties": {
        "on_delete": {
          "anyOf": [
            {
              "$ref": "#/$defs/ReferenceAction"
            },
            {
              "type": "null"
            }
          ]
        },
        "on_update": {
          "anyOf": [
            {
              "$ref": "#/$defs/ReferenceAction"
            },
            {
              "type": "null"
            }
          ]
        },
        "ref_columns": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "ref_table": {
          "type": "string"
        }
      },
      "required": ["ref_table", "ref_columns"]
    },
    "ForeignKeySyntax": {
      "anyOf": [
        {
          "description": "table.column",
          "type": "string"
        },
        {
          "$ref": "#/$defs/ForeignKeyDef"
        }
      ]
    },
    "NumValue": {
      "description": "Integer enum variant with name and numeric value",
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "value": {
          "type": "integer",
          "format": "int32"
        }
      },
      "required": ["name", "value"]
    },
    "PrimaryKeyDef": {
      "type": "object",
      "properties": {
        "auto_increment": {
          "type": "boolean",
          "default": false
        }
      }
    },
    "PrimaryKeySyntax": {
      "anyOf": [
        {
          "type": "boolean"
        },
        {
          "$ref": "#/$defs/PrimaryKeyDef"
        }
      ]
    },
    "ReferenceAction": {
      "type": "string",
      "enum": ["cascade", "restrict", "set_null", "set_default", "no_action"]
    },
    "SimpleColumnType": {
      "type": "string",
      "enum": [
        "small_int",
        "integer",
        "big_int",
        "real",
        "double_precision",
        "text",
        "boolean",
        "date",
        "time",
        "timestamp",
        "timestamptz",
        "interval",
        "bytea",
        "uuid",
        "json",
        "jsonb",
        "inet",
        "cidr",
        "macaddr",
        "xml"
      ]
    },
    "StrOrBoolOrArray": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        {
          "type": "boolean"
        }
      ]
    },
    "TableConstraint": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "auto_increment": {
              "type": "boolean",
              "default": false
            },
            "columns": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "type": {
              "type": "string",
              "const": "primary_key"
            }
          },
          "required": ["type", "columns"]
        },
        {
          "type": "object",
          "properties": {
            "columns": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "name": {
              "type": ["string", "null"]
            },
            "type": {
              "type": "string",
              "const": "unique"
            }
          },
          "required": ["type", "columns"]
        },
        {
          "type": "object",
          "properties": {
            "columns": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "name": {
              "type": ["string", "null"]
            },
            "on_delete": {
              "anyOf": [
                {
                  "$ref": "#/$defs/ReferenceAction"
                },
                {
                  "type": "null"
                }
              ]
            },
            "on_update": {
              "anyOf": [
                {
                  "$ref": "#/$defs/ReferenceAction"
                },
                {
                  "type": "null"
                }
              ]
            },
            "ref_columns": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "ref_table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "foreign_key"
            }
          },
          "required": ["type", "columns", "ref_table", "ref_columns"]
        },
        {
          "type": "object",
          "properties": {
            "expr": {
              "type": "string"
            },
            "name": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "check"
            }
          },
          "required": ["type", "name", "expr"]
        },
        {
          "type": "object",
          "properties": {
            "columns": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "name": {
              "type": ["string", "null"]
            },
            "type": {
              "type": "string",
              "const": "index"
            }
          },
          "required": ["type", "columns"]
        }
      ]
    }
  },
  "title": "TableDef",
  "type": "object",
  "properties": {
    "columns": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/ColumnDef"
      }
    },
    "constraints": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/TableConstraint"
      }
    },
    "description": {
      "type": ["string", "null"]
    },
    "name": {
      "type": "string"
    }
  },
  "required": ["name", "columns", "constraints"]
}
