namespace dyf.schema.v2;

table Node {
  children: [uint32];              // child node IDs (empty = leaf)
  hyperplanes: [float32];          // flattened (num_bits * embedding_dim)
  num_bits: uint8;                 // to reshape hyperplanes
  bucket_ids_to_children: [uint64]; // parallel with children: bucket_id for each child
  centroid: [float32];             // node centroid (embedding_dim)
  num_items: uint32;               // items in subtree
  batch_index: int32 = -1;         // Arrow batch index (leaves only, -1 for internal)
  depth: uint8;
  eigenvalues: [float32];          // per-component PCA eigenvalues
}

table BatchDescriptor {
  offset: uint64;                  // byte offset from data section start
  length: uint64;                  // byte length of Arrow IPC batch
  num_rows: uint32;
}

table FieldDescriptor {
  name: string;                    // e.g., "community_id"
  arrow_type: string;              // "int32", "float32", "utf8", etc.
}

table FieldLayer {
  name: string;                    // e.g., "community_id"
  fields: [FieldDescriptor];
  batches: [BatchDescriptor];
}

table BuildParams {
  max_depth: uint8;
  num_bits: uint8;
  min_leaf_size: uint32;
  seed: uint64;
  quantization: string;            // "float32", "float16", "int8"
  compression: string;             // "none", "zstd", "lz4"
}

table KeyValue {
  key: string;
  value: string;
}

table Index {
  // Fields 0-8: same order as v1 schema for binary compatibility
  version: string;                 // "2.0"
  embedding_dim: uint16;
  total_items: uint64;
  num_leaves: uint32;
  root: uint32;                    // root node index in nodes[]
  nodes: [Node];
  batches: [BatchDescriptor];      // base leaf batches
  build_params: BuildParams;
  metadata: [KeyValue];
  // New v2 fields (appended after v1 fields for compat)
  overflow_batches: [BatchDescriptor]; // appended item batches
  field_layers: [FieldLayer];      // per-layer field batches
}

root_type Index;
