{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rangka-os.github.io/rangka-os/schema/0.5/model.json",
  "title": "Rangka model",
  "type": "object",
  "required": [
    "name",
    "fields"
  ],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "name": {
      "$ref": "#/$defs/name"
    },
    "label": {
      "$ref": "#/$defs/label"
    },
    "icon": {
      "type": "string"
    },
    "is_submittable": {
      "type": "boolean"
    },
    "is_child": {
      "type": "boolean"
    },
    "append_only": {
      "type": "boolean",
      "default": false,
      "description": "A ledger: rows are written only by flows, hooks and seed; nobody, the owner included, creates them through the API, updates or deletes them. A correction is a reversing row (contract 0026)."
    },
    "naming": {
      "oneOf": [
        {
          "type": "string",
          "pattern": "^[A-Z0-9-]*(\\{YYYY\\}|\\{YY\\}|\\{MM\\}|\\{[a-z][a-z0-9_]*(\\.[a-z][a-z0-9_]*)?\\}-|[A-Z0-9-])*\\{#{2,8}\\}$"
        },
        {
          "type": "object",
          "required": [
            "series"
          ],
          "properties": {
            "series": {
              "$ref": "#/$defs/name"
            },
            "pattern": {
              "$ref": "#/$defs/number_pattern"
            },
            "reset": {
              "$ref": "#/$defs/number_reset"
            }
          },
          "additionalProperties": false
        }
      ],
      "description": "e.g. INV-{YYYY}-{#####} or STRK-{outlet.code}-{YY}{MM}-{####} (a field token, one counter per rendered prefix), or {series: invoice, pattern: 'INV/{ORG}/{YY}/{####}'} (contract 0009)"
    },
    "naming_on": {
      "enum": [
        "device"
      ],
      "description": "device: the name comes with the write from a paired device and the kernel checks it against the string naming, which carries {device.code}, instead of making it; a write without a device keeps the kernel's numbering with {device.code} left out (contract 0058 §3)"
    },
    "title_field": {
      "$ref": "#/$defs/name"
    },
    "fields": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/field"
      }
    },
    "rules": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/rule"
      }
    },
    "approvals": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/approval"
      }
    },
    "on_submit": {
      "type": "array",
      "items": {
        "type": "string",
        "pattern": "^[a-z_]+\\.[a-z_]+$"
      }
    },
    "on_cancel": {
      "type": "array",
      "items": {
        "type": "string",
        "pattern": "^[a-z_]+\\.[a-z_]+$"
      }
    },
    "stock": {
      "type": "object",
      "required": [
        "moves"
      ],
      "additionalProperties": false,
      "description": "How stock.post_moves moves stock for this model (contract 0021). The six built-in stock documents keep their own rule and may not declare one.",
      "properties": {
        "date": {
          "$ref": "#/$defs/name",
          "description": "Header date field used as posting_date (default: the first required date field)"
        },
        "moves": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/stock_rule"
          }
        },
        "cogs_total": {
          "$ref": "#/$defs/name",
          "description": "Read-only currency header field that gets the value taken out by all cogs lines"
        },
        "negative": {
          "enum": [
            "allow",
            "refuse"
          ],
          "description": "refuse: submit fails when an out move would leave the outlet below zero (default allow)"
        }
      }
    },
    "actions": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "name",
          "label",
          "hook"
        ],
        "properties": {
          "name": {
            "$ref": "#/$defs/name"
          },
          "description": {
            "$ref": "#/$defs/description",
            "description": "Owner sentence of what this does, conditions included (contract 0060 §1). Changes no behaviour."
          },
          "label": {
            "$ref": "#/$defs/label"
          },
          "icon": {
            "type": "string"
          },
          "hook": {
            "type": "string",
            "pattern": "^[a-z_]+\\.[a-z_]+$"
          },
          "primary": {
            "type": "boolean"
          },
          "if": {
            "$ref": "#/$defs/cel"
          },
          "queue": {
            "type": "object",
            "additionalProperties": false,
            "description": "Run in the background (needs system.queue).",
            "properties": {
              "retries": {
                "type": "integer",
                "minimum": 0,
                "maximum": 25
              },
              "backoff": {
                "type": "string",
                "pattern": "^[0-9]+(s|m|h)$"
              },
              "timeout": {
                "type": "string",
                "pattern": "^[0-9]+(s|m|h)$"
              }
            }
          }
        },
        "additionalProperties": false
      }
    },
    "automations": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "name",
          "when",
          "do"
        ],
        "properties": {
          "name": {
            "$ref": "#/$defs/name"
          },
          "description": {
            "$ref": "#/$defs/description",
            "description": "Owner sentence of what this does, conditions included (contract 0060 §1). Changes no behaviour."
          },
          "when": {
            "type": "string"
          },
          "if": {
            "$ref": "#/$defs/cel"
          },
          "do": {
            "type": "string"
          }
        },
        "additionalProperties": false
      }
    },
    "permissions": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/permission"
      }
    },
    "views": {
      "type": "object",
      "properties": {
        "default": {
          "enum": [
            "list",
            "kanban",
            "calendar",
            "report"
          ]
        },
        "list": {
          "type": "object",
          "properties": {
            "columns": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/listcol"
              },
              "minItems": 1
            },
            "filters": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/name"
              }
            },
            "sort": {
              "type": "string",
              "pattern": "^-?[a-z_]+$"
            },
            "group_by": {
              "$ref": "#/$defs/name"
            }
          },
          "additionalProperties": false
        },
        "kanban": {
          "type": "object",
          "required": [
            "group_by"
          ],
          "properties": {
            "group_by": {
              "$ref": "#/$defs/name"
            },
            "title": {
              "$ref": "#/$defs/name"
            },
            "card": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/name"
              },
              "maxItems": 5
            },
            "sum": {
              "$ref": "#/$defs/name"
            }
          },
          "additionalProperties": false
        },
        "form": {
          "type": "object",
          "required": [
            "layout"
          ],
          "properties": {
            "layout": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/section"
              }
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "realtime": {
      "type": "boolean",
      "default": false,
      "description": "Push record changes to open screens (needs system.realtime)."
    },
    "cache": {
      "type": "string",
      "pattern": "^[0-9]+(s|m|h)$",
      "description": "Cache list reads for this long (needs system.cache)."
    }
  },
  "additionalProperties": false,
  "if": {
    "properties": {
      "is_child": {
        "const": true
      }
    },
    "required": [
      "is_child"
    ]
  },
  "then": {
    "not": {
      "anyOf": [
        {
          "required": [
            "is_submittable"
          ]
        },
        {
          "required": [
            "naming"
          ]
        }
      ]
    }
  },
  "$defs": {
    "label": {
      "type": "object",
      "description": "A label in Bahasa Indonesia (required) and optionally English.",
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 140
        },
        "en": {
          "type": "string",
          "maxLength": 140
        }
      },
      "additionalProperties": false
    },
    "description": {
      "type": "object",
      "description": "An owner sentence or two in Bahasa Indonesia (required) and optionally English (contract 0060 §1).",
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 280
        },
        "en": {
          "type": "string",
          "maxLength": 280
        }
      },
      "additionalProperties": false
    },
    "name": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]{0,62}$",
      "description": "snake_case, never renamed once released"
    },
    "cel": {
      "type": "string",
      "minLength": 1,
      "description": "A CEL expression (plus Rangka helpers)"
    },
    "option": {
      "type": "object",
      "required": [
        "value",
        "label"
      ],
      "properties": {
        "value": {
          "type": "string",
          "pattern": "^[a-z0-9_]+$"
        },
        "label": {
          "$ref": "#/$defs/label"
        },
        "tone": {
          "enum": [
            "neutral",
            "info",
            "ok",
            "warn",
            "bad",
            "muted"
          ]
        },
        "probability": {
          "type": "number",
          "minimum": 0,
          "maximum": 100
        },
        "closed": {
          "type": "boolean"
        }
      },
      "additionalProperties": false
    },
    "field": {
      "type": "object",
      "required": [
        "name",
        "type"
      ],
      "properties": {
        "name": {
          "$ref": "#/$defs/name"
        },
        "type": {
          "enum": [
            "data",
            "text",
            "int",
            "float",
            "currency",
            "percent",
            "check",
            "select",
            "multiselect",
            "link",
            "dynamic_link",
            "table",
            "date",
            "daterange",
            "datetime",
            "duration",
            "phone",
            "npwp",
            "attach",
            "image",
            "rating",
            "color",
            "json",
            "signature",
            "money",
            "geo"
          ]
        },
        "label": {
          "$ref": "#/$defs/label"
        },
        "required": {
          "type": "boolean"
        },
        "default": {
          "$ref": "#/$defs/cel"
        },
        "compute": {
          "$ref": "#/$defs/cel"
        },
        "depends_on": {
          "$ref": "#/$defs/cel"
        },
        "read_only": {
          "type": "boolean"
        },
        "set_once": {
          "type": "boolean",
          "description": "A client may fill the field while it is empty but never change a value it holds (proof: clock-in time, position, photo); flows and other kernel writes may (contract 0030 §4)"
        },
        "hidden": {
          "type": "boolean"
        },
        "in_list": {
          "type": "boolean"
        },
        "in_filter": {
          "type": "boolean"
        },
        "unique": {
          "type": "boolean"
        },
        "index": {
          "type": "boolean"
        },
        "help": {
          "$ref": "#/$defs/label"
        },
        "to": {
          "$ref": "#/$defs/name"
        },
        "of": {
          "$ref": "#/$defs/name"
        },
        "currency_field": {
          "$ref": "#/$defs/name"
        },
        "rate_field": {
          "$ref": "#/$defs/name"
        },
        "peek": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/name"
          },
          "maxItems": 6
        },
        "options": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/option"
          },
          "minItems": 1
        },
        "chip": {
          "type": "boolean"
        },
        "relative_to": {
          "$ref": "#/$defs/name"
        },
        "min_rows": {
          "type": "integer",
          "minimum": 0
        },
        "read_only_for": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/name"
          }
        },
        "hidden_for": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/name"
          }
        },
        "money": {
          "type": "boolean",
          "description": "Money field (currency fields are money by default). Changes are high risk."
        },
        "stock": {
          "type": "boolean",
          "description": "Quantity that moves stock. Changes are high risk."
        }
      },
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "properties": {
              "type": {
                "const": "link"
              }
            }
          },
          "then": {
            "required": [
              "to"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "money"
              }
            }
          },
          "then": {
            "required": [
              "currency_field"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "table"
              }
            }
          },
          "then": {
            "required": [
              "of"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "enum": [
                  "select",
                  "multiselect"
                ]
              }
            }
          },
          "then": {
            "required": [
              "options"
            ]
          }
        },
        {
          "if": {
            "required": [
              "compute"
            ]
          },
          "then": {
            "not": {
              "required": [
                "default"
              ]
            }
          }
        }
      ]
    },
    "rule": {
      "type": "object",
      "required": [
        "name",
        "on",
        "check",
        "message"
      ],
      "properties": {
        "name": {
          "$ref": "#/$defs/name"
        },
        "on": {
          "enum": [
            "save",
            "submit",
            "cancel"
          ]
        },
        "level": {
          "enum": [
            "error",
            "warn",
            "info"
          ]
        },
        "check": {
          "$ref": "#/$defs/cel"
        },
        "message": {
          "$ref": "#/$defs/label"
        }
      },
      "additionalProperties": false
    },
    "permission": {
      "type": "object",
      "required": [
        "role"
      ],
      "properties": {
        "role": {
          "$ref": "#/$defs/name"
        },
        "if": {
          "$ref": "#/$defs/cel"
        },
        "read": {
          "type": "boolean"
        },
        "create": {
          "type": "boolean"
        },
        "write": {
          "type": "boolean"
        },
        "delete": {
          "type": "boolean"
        },
        "submit": {
          "type": "boolean"
        },
        "cancel": {
          "type": "boolean"
        },
        "amend": {
          "type": "boolean"
        },
        "export": {
          "type": "boolean"
        },
        "import": {
          "type": "boolean"
        },
        "print": {
          "type": "boolean"
        },
        "customize": {
          "type": "boolean"
        }
      },
      "additionalProperties": false
    },
    "section": {
      "type": "object",
      "required": [
        "section"
      ],
      "properties": {
        "section": {
          "$ref": "#/$defs/label"
        },
        "fields": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/name"
          }
        },
        "columns": {
          "type": "array",
          "maxItems": 2,
          "items": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/name"
            }
          }
        },
        "tab": {
          "$ref": "#/$defs/label"
        }
      },
      "additionalProperties": false,
      "oneOf": [
        {
          "required": [
            "fields"
          ]
        },
        {
          "required": [
            "columns"
          ]
        }
      ]
    },
    "listcol": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*$"
    },
    "number_pattern": {
      "type": "string",
      "pattern": "^([A-Za-z0-9/._-]|\\{YYYY\\}|\\{YY\\}|\\{MM\\}|\\{ORG\\})*\\{#{1,9}\\}([A-Za-z0-9/._-]|\\{YYYY\\}|\\{YY\\}|\\{MM\\}|\\{ORG\\})*$",
      "description": "Literal text, {YYYY} {YY} {MM} {ORG}, and exactly one counter {###}"
    },
    "number_reset": {
      "type": "string",
      "enum": [
        "yearly",
        "monthly",
        "never"
      ]
    },
    "approval": {
      "type": "object",
      "required": [
        "name",
        "on",
        "approvers"
      ],
      "additionalProperties": false,
      "properties": {
        "name": {
          "$ref": "#/$defs/name"
        },
        "on": {
          "$ref": "#/$defs/name",
          "description": "submit, cancel or an action name of this model"
        },
        "when": {
          "$ref": "#/$defs/cel",
          "description": "Policy applies only when this is true (absent = always)"
        },
        "approvers": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/name"
          },
          "description": "Roles that may approve; owner always may"
        },
        "label": {
          "$ref": "#/$defs/label"
        }
      }
    },
    "stock_rule": {
      "type": "object",
      "required": [
        "lines",
        "reason"
      ],
      "additionalProperties": false,
      "properties": {
        "lines": {
          "$ref": "#/$defs/name",
          "description": "Table field whose rows move"
        },
        "in": {
          "type": "string",
          "pattern": "^(line\\.)?[a-z][a-z0-9_]{0,62}$",
          "description": "Outlet the rows come into: a header link field, or line.<field>"
        },
        "out": {
          "type": "string",
          "pattern": "^(line\\.)?[a-z][a-z0-9_]{0,62}$",
          "description": "Outlet the rows leave: a header link field, or line.<field>"
        },
        "item": {
          "$ref": "#/$defs/name",
          "description": "Line link to item (default item)"
        },
        "qty": {
          "$ref": "#/$defs/name",
          "description": "Line quantity in stock uom (default stock_qty when the line has it, else qty)"
        },
        "if": {
          "$ref": "#/$defs/cel",
          "description": "Per line, with doc and line; false skips the line"
        },
        "reason": {
          "$ref": "#/$defs/name",
          "description": "An option of stock_move.reason"
        },
        "explode": {
          "enum": [
            "recipe"
          ],
          "description": "out: consume through the item's active recipe; in: bring its materials back (cost average or source)"
        },
        "cost": {
          "description": "in only: average (default), {unit: f}, {amount: f}, {source: link}, {consumed: true, split: qty|f}",
          "oneOf": [
            {
              "enum": [
                "average"
              ]
            },
            {
              "type": "object",
              "required": [
                "unit"
              ],
              "additionalProperties": false,
              "properties": {
                "unit": {
                  "$ref": "#/$defs/name"
                }
              }
            },
            {
              "type": "object",
              "required": [
                "amount"
              ],
              "additionalProperties": false,
              "properties": {
                "amount": {
                  "$ref": "#/$defs/name"
                }
              }
            },
            {
              "type": "object",
              "required": [
                "source"
              ],
              "additionalProperties": false,
              "properties": {
                "source": {
                  "$ref": "#/$defs/name"
                }
              }
            },
            {
              "type": "object",
              "required": [
                "consumed"
              ],
              "additionalProperties": false,
              "properties": {
                "consumed": {
                  "const": true
                },
                "split": {
                  "$ref": "#/$defs/name"
                }
              }
            }
          ]
        },
        "cogs": {
          "$ref": "#/$defs/name",
          "description": "out only: read-only currency line field that gets the value the line took out"
        }
      }
    },
    "catalogue_category": {
      "enum": [
        "sales",
        "purchasing",
        "stock",
        "finance",
        "tax",
        "people",
        "operations",
        "messaging",
        "payments",
        "marketplace",
        "other"
      ],
      "description": "Catalogue group (contract 0036)"
    },
    "icon": {
      "type": "string",
      "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$",
      "description": "A Remix icon name from the design system, e.g. store-2-line (contract 0036)"
    }
  }
}
