{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.horusbi.com.br/schemas/v2/table.schema.json",
  "title": "Lumo v2 table YAML",
  "type": "object",
  "required": [
    "header",
    "body"
  ],
  "properties": {
    "fileName": {
      "$ref": "shared.schema.json#/$defs/resourceFileName"
    },
    "header": {
      "allOf": [
        {
          "$ref": "shared.schema.json#/$defs/resourceHeaderBase"
        },
        {
          "type": "object",
          "required": [
            "id"
          ],
          "properties": {
            "kind": {
              "const": "table"
            }
          }
        }
      ]
    },
    "body": {
      "allOf": [
        {
          "$ref": "shared.schema.json#/$defs/auditFields"
        },
        {
          "type": "object",
          "required": [
            "nome",
            "table_type",
            "columns"
          ],
          "properties": {
            "nome": {
              "type": "string",
              "minLength": 1
            },
            "_state": {
              "$ref": "shared.schema.json#/$defs/readOnlyState"
            },
            "deskId": {
              "$ref": "shared.schema.json#/$defs/readOnlyDeskId"
            },
            "originFlowId": {
              "$ref": "shared.schema.json#/$defs/nullableId"
            },
            "version": {
              "$ref": "shared.schema.json#/$defs/readOnlyVersion"
            },
            "icon": {
              "type": [
                "string",
                "null"
              ]
            },
            "table_type": {
              "type": "string",
              "enum": [
                "table",
                "file",
                "cloud"
              ],
              "description": "table=normal DW table fed by a flow's InsertDatawarehouse (Datawarehouse mode) - usable in apps. file=static, fed by Excel uploads via the DW UI, no flow - usable in apps. cloud=datalake/parquet output of InsertDatawarehouse (Datalake mode) - only consumable as an ExtractLakehouse input by other flows, never inside apps."
            },
            "key_type": {
              "type": "string",
              "enum": [
                "duplicate",
                "unique"
              ],
              "description": "Doris key-model. duplicate=rows accumulate on every load (append-only). unique=rows sharing the same key_columns are deduplicated (last write wins / upsert). Only for Doris-backed tables (table_type table/file) - not allowed for cloud. NOTE: this is the physical key-model and is DIFFERENT from the InsertDatawarehouse node's PrimaryKeys, which only dedups in-memory during a single load. Changing key_type is STRUCTURAL: it forces a destructive DROP+CREATE of the physical Doris table (rows are lost; re-run the flow to reload). `lumo push` refuses such a change on a table that already has data unless you pass --force-recreate."
            },
            "key_columns": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "description": "Column names that form the Doris key - the dedup key when key_type=unique, the sort/prefix key when duplicate. Each name must match a physical column (schema comes from the flow). Changing this set is STRUCTURAL - same recreate rules as key_type. Doris-backed tables only."
            },
            "partition_column": {
              "type": [
                "string",
                "null"
              ],
              "description": "Column name used as the Doris partition, or null for none. Must match a physical column. Structural - changing it forces a recreate. Doris-backed tables only."
            },
            "columns": {
              "type": "object",
              "description": "Physical columns as a cosmetic overlay, keyed by \"COLUMN_NAME (DataType)\". The key is read-only identity (schema is owned by the flow); only the value fields are editable. To change column name/type/schema, clone the flow that feeds this table.",
              "additionalProperties": {
                "type": "object",
                "properties": {
                  "label": {
                    "type": "string"
                  },
                  "mask": {
                    "$ref": "shared.schema.json#/$defs/mask"
                  },
                  "human_description": {
                    "type": [
                      "string",
                      "null"
                    ]
                  },
                  "defaultBehavior": {
                    "type": "string"
                  }
                }
              }
            },
            "expressions": {
              "type": "array",
              "items": {
                "$ref": "shared.schema.json#/$defs/tableColumn"
              }
            },
            "tags": {
              "$ref": "shared.schema.json#/$defs/tagList"
            }
          },
          "additionalProperties": true
        },
        {
          "if": {
            "required": [
              "table_type"
            ],
            "properties": {
              "table_type": {
                "const": "cloud"
              }
            }
          },
          "then": {
            "comment": "cloud (datalake/parquet) tables have no Doris key-model.",
            "properties": {
              "key_type": false,
              "key_columns": false,
              "partition_column": false
            }
          }
        }
      ]
    }
  },
  "additionalProperties": false
}
