# CalculateSubtreeDepth

## Overview

CalculateSubtreeDepth recursively calculates the maximum depth of a node's subtree, including the node itself. This is used in conjunction with CalculateNodeDepth to determine the total tree depth after a move operation, ensuring the combined depth does not exceed the maximum allowed depth.

## Business Rules

- A leaf node (no children) has subtree depth of 1
- For nodes with children, subtree depth = 1 + max(child subtree depths)
- All children are fetched by querying parentId = nodeId
- Recursion continues until leaf nodes are reached
- The calculation is exhaustive — it traverses the entire subtree

## Process Flow

```mermaid
flowchart TD
    A[Receive nodeId] --> B[SELECT all children where parentId = nodeId]
    B --> C{Has children?}
    C -->|No| D[Return 1]
    C -->|Yes| E[Initialize maxChildDepth = 0]
    E --> F[For each child]
    F --> G[Recursively calculate child subtree depth]
    G --> H{childDepth > maxChildDepth?}
    H -->|Yes| I[Update maxChildDepth = childDepth]
    H -->|No| J[Continue to next child]
    I --> J
    J --> K{More children?}
    K -->|Yes| F
    K -->|No| L[Return 1 + maxChildDepth]
```

## External Dependencies

- None

## Error Scenarios

- **MAX_DEPTH_EXCEEDED**: Combined new parent depth + subtree depth exceeds maxDepth — enforced by the calling command, not this query directly

## Test Cases

- returns 1 for leaf node (no children)
- returns 2 for node with one level of children
- returns 3 for node with two levels of children
- returns max depth for node with multiple branches
