@startuml Wasot ER Diagram

entity "element" as element {
  + **id**: INTEGER [PK]
  --
  # parent_id: INTEGER [FK]
  block_id: TEXT
  args: JSON
  content: TEXT
  element_type: TEXT
  block_type: TEXT
  line_idx: INTEGER
  --
  **Indexes**
  idx_element_block_id (block_id) UNIQUE
  idx_element_parent_line (parent_id, line_idx)
  idx_element_type (element_type)
  idx_element_block_type (block_type)
  idx_element_line_idx (line_idx)
}
note right of element
  **Constraints:**
  * CHECK(block_type IN ('block', 'text'))
  * CHECK(id <> parent_id)
  * `block_id` should only be set
    when `block_type` = 'block'.
end note

entity "tag" as tag {
  + **id**: INTEGER [PK]
  --
  name: TEXT
  --
  **Indexes**
  idx_tag_name (name) UNIQUE
}

entity "element_tag" as element_tag {
  + **element_id**: INTEGER [PK, FK]
  + **tag_id**: INTEGER [PK, FK]
}
note right of element_tag
  **Note:** The composite primary key
  `(element_id, tag_id)` already
  serves as an efficient index.
end note

entity "ancestor_relationship" as ancestor_relationship {
  + **ancestor_id**: INTEGER [PK, FK]
  + **descendant_id**: INTEGER [PK, FK]
}
note right of ancestor_relationship
  **Constraint:**
  * CHECK(ancestor_id <> descendant_id)
end note

' --- Relationships ---
element "1" --o{ "0..*" element
element ||--|{ element_tag
tag     ||--|{ element_tag
element "1" --o{ "0..*" ancestor_relationship : "ancestor"
element "1" --o{ "0..*" ancestor_relationship : "descendant"

@enduml