///|
/// A relation between two axis-aligned boxes.
pub(all) enum BoxRelation {
  Disjoint
  Touching
  Overlapping
  Contains
  Within
  Equal
} derive(Debug, Eq)

///|
/// A product's world-space spatial record.
pub(all) struct SpatialEntry {
  product_id : Int
  entity_id : Int
  ifc_type : String
  name : String?
  container : Int?
  bounds : BoundingBox
  geometry_count : Int
  point_count : Int
  triangle_count : Int
} derive(Debug)

///|
/// A hit returned from a spatial query.
pub(all) struct SpatialHit {
  product_id : Int
  entity_id : Int
  bounds : BoundingBox
  relation : BoxRelation
  distance : Double
  overlap_volume : Double
} derive(Debug)

///|
/// A configurable spatial grid used for broad-phase BIM queries.
pub(all) struct SpatialIndex {
  entries : Array[SpatialEntry]
  cells : Map[String, Array[Int]]
  bounds : BoundingBox
  cell_size : Double
} derive(Debug)

///|
/// Options for constructing a spatial index.
pub(all) struct SpatialIndexOptions {
  cell_size : Double
  include_empty : Bool
  maximum_entries : Int
} derive(Debug)

///|
/// A candidate pair found by broad-phase clash detection.
pub(all) struct ClashPair {
  left : SpatialEntry
  right : SpatialEntry
  relation : BoxRelation
  overlap : BoundingBox
  overlap_volume : Double
  clearance : Double
} derive(Debug)

///|
/// Policy controlling which product pairs are reported as clashes.
pub(all) struct ClashOptions {
  tolerance : Double
  minimum_overlap_volume : Double
  include_touching : Bool
  include_same_container : Bool
  ignored_types : Array[String]
  maximum_pairs : Int
} derive(Debug)

///|
/// Aggregate result of a clash or clearance analysis.
pub(all) struct ClashReport {
  pairs : Array[ClashPair]
  candidate_count : Int
  ignored_count : Int
  overlap_volume : Double
  by_type : Map[String, Int]
} derive(Debug)

///|
/// The quantity kind used by takeoff and estimation APIs.
pub(all) enum QuantityKind {
  Count
  Length
  Area
  Volume
  SurfaceArea
  Height
  Width
  Depth
} derive(Debug, Eq, Hash)

///|
/// A measured quantity with its source and unit semantics.
pub(all) struct QuantityValue {
  kind : QuantityKind
  value : Double
  unit : String
  source : String
  estimated : Bool
} derive(Debug)

///|
/// Quantities calculated for one IFC product.
pub(all) struct ProductTakeoff {
  product : Product
  quantities : Array[QuantityValue]
  geometry_count : Int
  mesh_triangle_count : Int
  bounds : BoundingBox
} derive(Debug)

///|
/// A roll-up row grouped by type, container or an application key.
pub(all) struct TakeoffGroup {
  key : String
  product_count : Int
  quantities : Array[QuantityValue]
} derive(Debug)

///|
/// Complete quantity takeoff result.
pub(all) struct TakeoffReport {
  products : Array[ProductTakeoff]
  groups : Array[TakeoffGroup]
  unit_system : UnitSystem
  source_schema : String
  skipped_products : Int
} derive(Debug)

///|
/// Grouping keys used when rolling up quantity takeoff rows.
pub(all) enum QuantityGrouping {
  ProductType
  ProductKind
  Container
  Property(String)
} derive(Debug)

///|
/// Options for geometry and property driven quantity extraction.
pub(all) struct TakeoffOptions {
  grouping : QuantityGrouping
  include_empty : Bool
  include_unknown : Bool
  use_mesh_measurements : Bool
  include_surface_area : Bool
  minimum_value : Double
} derive(Debug)

///|
/// A formula override used by quantity schedules.
pub(all) struct QuantityFormula {
  kind : QuantityKind
  property_names : Array[String]
  fallback_to_geometry : Bool
  multiplier : Double
  unit : String
} derive(Debug)

///|
/// A non-fatal note explaining a missing or estimated quantity.
pub(all) struct QuantityNote {
  product_id : Int
  kind : QuantityKind
  message : String
  severity : DiagnosticSeverity
} derive(Debug)

///|
/// A takeoff report produced from configurable property formulas.
pub(all) struct FormulaTakeoff {
  rows : Array[ProductTakeoff]
  notes : Array[QuantityNote]
  formulas : Array[QuantityFormula]
  product_count : Int
  missing_formula_count : Int
} derive(Debug)

///|
/// Comparison used by a numeric query predicate.
pub(all) enum NumberComparison {
  LessThan
  LessOrEqual
  EqualTo
  GreaterOrEqual
  GreaterThan
  Between
} derive(Debug, Eq)

///|
/// A composable predicate over extracted IFC products.
pub(all) enum ProductPredicate {
  ProductKind(ProductKind)
  EntityType(String)
  EntityTypePrefix(String)
  NameContains(String)
  DescriptionContains(String)
  GlobalId(String)
  Tag(String)
  InContainer(Int)
  HasProperties
  HasGeometry
  PropertyExists(String)
  PropertyText(String, String)
  PropertyNumber(String, NumberComparison, Double, Double?)
  IdRange(Int, Int)
  GeometryKind(GeometryKind)
  Quantity(QuantityKind, NumberComparison, Double, Double?)
} derive(Debug)

///|
/// Boolean composition of product predicates.
pub(all) enum ProductExpression {
  MatchAll
  MatchNone
  Predicate(ProductPredicate)
  Not(ProductExpression)
  AllOf(Array[ProductExpression])
  AnyOf(Array[ProductExpression])
} derive(Debug)

///|
/// Stable ordering options for a compiled product query.
pub(all) enum ProductSort {
  ById
  ByType
  ByName
  ByKind
  ByQuantity(QuantityKind)
  ByDistance(Point3)
} derive(Debug)

///|
/// A reusable query plan for repeated product searches.
pub(all) struct ProductQueryPlan {
  expression : ProductExpression
  sort : ProductSort
  descending : Bool
  offset : Int
  limit : Int
  include_empty_geometry : Bool
} derive(Debug)

///|
/// One page returned by a product query plan.
pub(all) struct ProductQueryPage {
  products : Array[Product]
  total_matches : Int
  offset : Int
  limit : Int
  has_next : Bool
  examined : Int
} derive(Debug)

///|
/// One directed relationship in the entity dependency graph.
pub(all) struct GraphEdge {
  from : Int
  to : Int
  relation : String
  argument : Int
} derive(Debug, Eq)

///|
/// Indexed directed graph over raw IFC entity references.
pub(all) struct EntityGraph {
  nodes : Array[Int]
  edges : Array[GraphEdge]
  outgoing : Map[Int, Array[GraphEdge]]
  incoming : Map[Int, Array[GraphEdge]]
} derive(Debug)

///|
/// Degree information for one graph node.
pub(all) struct GraphDegree {
  id : Int
  incoming : Int
  outgoing : Int
  total : Int
  orphan : Bool
} derive(Debug)

///|
/// A path through the entity dependency graph.
pub(all) struct GraphPath {
  ids : Array[Int]
  edges : Array[GraphEdge]
  total_cost : Int
} derive(Debug)

///|
/// A connected component of the undirected entity graph.
pub(all) struct GraphComponent {
  root : Int
  entity_ids : Array[Int]
  edge_count : Int
  cyclic : Bool
} derive(Debug)

///|
/// Rich graph metrics for impact analysis and model diagnostics.
pub(all) struct GraphAnalysis {
  nodes : Int
  edges : Int
  roots : Array[Int]
  leaves : Array[Int]
  components : Array[GraphComponent]
  cycles : Array[Array[Int]]
  max_depth : Int
  orphan_ids : Array[Int]
} derive(Debug)

///|
/// Options controlling mesh cleanup and level-of-detail generation.
pub(all) struct MeshPipelineOptions {
  weld_tolerance : Double
  degenerate_tolerance : Double
  maximum_triangles : Int
  recenter : Bool
  scale : Double
  flip_winding : Bool
  generate_normals : Bool
  compact_vertices : Bool
} derive(Debug)

///|
/// Quality counters emitted by a mesh processing pipeline.
pub(all) struct MeshPipelineStats {
  input_points : Int
  input_triangles : Int
  output_points : Int
  output_triangles : Int
  removed_points : Int
  removed_triangles : Int
  surface_area : Double
  volume : Double
  bounds : BoundingBox
  valid : Bool
} derive(Debug)

///|
/// Processed mesh and optional normals for a renderer or exporter.
pub(all) struct MeshPipelineResult {
  mesh : Mesh
  normals : Array[Vector3]
  stats : MeshPipelineStats
} derive(Debug)

///|
/// One structural problem found by mesh validation.
pub(all) struct MeshIssue {
  code : String
  message : String
  point_index : Int?
  triangle_index : Int?
} derive(Debug)

///|
/// Validation summary for a mesh before it reaches a renderer.
pub(all) struct MeshValidation {
  valid : Bool
  issues : Array[MeshIssue]
  referenced_point_count : Int
  unreferenced_point_count : Int
  degenerate_triangle_count : Int
} derive(Debug)

///|
/// A rule for validating a model against project delivery requirements.
pub(all) struct ModelRule {
  code : String
  description : String
  severity : DiagnosticSeverity
  entity_type : String?
  minimum_arguments : Int?
  required_attributes : Array[Int]
  required_properties : Array[String]
  minimum_value : Double?
  maximum_value : Double?
} derive(Debug)

///|
/// A single rule violation with enough context for UI display.
pub(all) struct RuleViolation {
  rule_code : String
  severity : DiagnosticSeverity
  entity_id : Int?
  entity_type : String?
  message : String
  property_name : String?
} derive(Debug)

///|
/// Result of a configurable model rule evaluation.
pub(all) struct RuleReport {
  rules : Array[ModelRule]
  violations : Array[RuleViolation]
  error_count : Int
  warning_count : Int
  checked_entities : Int
  checked_products : Int
} derive(Debug)

///|
/// Filter used by the low-allocation STEP record stream API.
pub(all) struct StreamFilter {
  names : Array[String]
  first_id : Int?
  last_id : Int?
  minimum_length : Int
  maximum_length : Int
  maximum_records : Int
} derive(Debug)

///|
/// A bounded batch of raw STEP records.
pub(all) struct StreamBatch {
  sequence : Int
  start_offset : Int
  end_offset : Int
  records : Array[StepRecord]
  complete : Bool
} derive(Debug)

///|
/// Summary of a filtered stream operation.
pub(all) struct StreamSelection {
  records : Array[StepRecord]
  examined : Int
  selected : Int
  truncated : Bool
  first_offset : Int
  last_offset : Int
} derive(Debug)

///|
/// A reusable high-level workspace built around one parsed model.
pub(all) struct ModelWorkspace {
  model : Model
  catalog : ModelCatalog
  spatial : SpatialIndex
  graph : GraphAnalysis
  health : SourceHealth
  quantities : TakeoffReport?
} derive(Debug)

///|
/// A compact model snapshot used to compare revisions without serializing twice.
pub(all) struct ModelSnapshot {
  schema : String
  source_length : Int
  entity_count : Int
  product_count : Int
  shape_signatures : Array[String]
  reference_targets : Array[Int]
} derive(Debug)

///|
/// A compact health and capability view of a model workspace.
pub(all) struct WorkspaceSummary {
  schema : String
  source_length : Int
  entity_count : Int
  product_count : Int
  indexed_products : Int
  graph_nodes : Int
  graph_edges : Int
  rule_errors : Int
  rule_warnings : Int
} derive(Debug)

///|
/// A material layer decoded from an IFC material profile.
pub(all) struct MaterialLayer {
  entity_id : Int
  material_id : Int?
  material_name : String?
  thickness : Double?
  priority : Int?
  is_unbounded : Bool
} derive(Debug)

///|
/// Material assignments attached to one product.
pub(all) struct MaterialAssignment {
  product_id : Int
  association_id : Int
  material_id : Int?
  material_name : String?
  layers : Array[MaterialLayer]
  constituents : Array[String]
} derive(Debug)

///|
/// Model-wide material inventory and coverage metrics.
pub(all) struct MaterialInventory {
  assignments : Array[MaterialAssignment]
  distinct_materials : Array[String]
  layered_product_count : Int
  unassigned_product_count : Int
} derive(Debug)

///|
/// A classification reference such as OmniClass, Uniclass or a local code.
pub(all) struct ClassificationReference {
  entity_id : Int
  source : String?
  identification : String?
  name : String?
  referenced_entity : Int
} derive(Debug)

///|
/// A compact report for one product's decoded geometric representations.
pub(all) struct ProductGeometryReport {
  product_id : Int
  geometry_count : Int
  mesh_count : Int
  point_count : Int
  triangle_count : Int
  bounds : BoundingBox
  surface_area : Double
  volume : Double
  estimated_volume : Bool
  valid_mesh_count : Int
} derive(Debug)

///|
/// Aggregate geometry coverage and quality metrics.
pub(all) struct GeometryInventory {
  products : Array[ProductGeometryReport]
  represented_product_count : Int
  empty_representation_count : Int
  invalid_mesh_count : Int
  total_surface_area : Double
  total_volume : Double
} derive(Debug)

///|
/// A named coordinate system discovered from IFC placement entities.
pub(all) struct CoordinateFrame {
  entity_id : Int
  name : String
  parent_id : Int?
  transform : Transform3
  depth : Int
} derive(Debug)

///|
/// A model coordinate conversion summary.
pub(all) struct CoordinateReport {
  frames : Array[CoordinateFrame]
  world_bounds : BoundingBox
  unit_scale : Double
  has_map_conversion : Bool
  warning_count : Int
} derive(Debug)

///|
/// A room or space schedule row used by coordination tools.
pub(all) struct SpatialScheduleRow {
  id : Int
  name : String
  ifc_type : String
  path : Array[Int]
  product_count : Int
  door_count : Int
  window_count : Int
  wall_count : Int
  area : Double
  volume : Double
  bounds : BoundingBox
} derive(Debug)

///|
/// Spatial schedule for all rooms and storeys in a model.
pub(all) struct SpatialSchedule {
  rows : Array[SpatialScheduleRow]
  total_spaces : Int
  total_products : Int
  total_area : Double
  total_volume : Double
} derive(Debug)

///|
/// A field-level change that is easier to review than a complete entity diff.
pub(all) struct EntityFieldChange {
  entity_id : Int
  attribute : Int
  before : Value?
  after : Value?
  kind : ChangeKind
} derive(Debug)

///|
/// Dependency impact of a model revision.
pub(all) struct RevisionImpact {
  changed_ids : Array[Int]
  affected_ids : Array[Int]
  affected_products : Array[Int]
  affected_spatial_nodes : Array[Int]
  field_changes : Array[EntityFieldChange]
  risk : String
} derive(Debug)

///|
/// A deterministic revision report combining content and dependency changes.
pub(all) struct RevisionReport {
  diff : ModelDiff
  impact : RevisionImpact
  added_products : Array[Int]
  removed_products : Array[Int]
  modified_products : Array[Int]
} derive(Debug)

///|
/// IFC compressed GUID validation result.
pub(all) struct GuidReport {
  value : String
  valid : Bool
  decoded_bytes : Int
  normalized : String
  message : String
} derive(Debug)

///|
/// A normalized column in a property schedule.
pub(all) struct PropertyColumn {
  name : String
  value_type : String
  populated_count : Int
  numeric_count : Int
  distinct_count : Int
} derive(Debug)

///|
/// One row in a property schedule.
pub(all) struct PropertyRow {
  product_id : Int
  ifc_type : String
  name : String?
  values : Map[String, String]
  missing_count : Int
} derive(Debug)

///|
/// Tabular property data suitable for CSV or browser grids.
pub(all) struct PropertyTable {
  columns : Array[PropertyColumn]
  rows : Array[PropertyRow]
  product_count : Int
  missing_value_count : Int
} derive(Debug)

///|
/// A set of scene chunks used to stream large WebBIM models.
pub(all) struct SceneChunk {
  index : Int
  product_ids : Array[Int]
  bounds : BoundingBox
  point_count : Int
  triangle_count : Int
} derive(Debug)

///|
/// A deterministic spatial partition of renderable products.
pub(all) struct ScenePartition {
  chunks : Array[SceneChunk]
  bounds : BoundingBox
  chunk_size : Int
  product_count : Int
} derive(Debug)

///|
/// A dependency-closed model slice for partial loading.
pub(all) struct ModelSlice {
  model : Model
  selected_ids : Array[Int]
  dependency_ids : Array[Int]
  omitted_ids : Array[Int]
  root_ids : Array[Int]
} derive(Debug)

///|
/// A normalized IFC relationship edge.
pub(all) struct RelationLink {
  relation_id : Int
  relation_type : String
  relating_id : Int?
  related_ids : Array[Int]
  role : String
} derive(Debug)

///|
/// Precomputed relationship maps for repeated semantic queries.
pub(all) struct RelationIndex {
  links : Array[RelationLink]
  parents : Map[Int, Array[Int]]
  children : Map[Int, Array[Int]]
  containers : Map[Int, Int]
  property_sets : Map[Int, Array[Int]]
  classifications : Map[Int, Array[Int]]
} derive(Debug)

///|
/// A model-level semantic profile used by import dashboards.
pub(all) struct ModelProfile {
  schema : SchemaVersion
  entity_count : Int
  product_count : Int
  family_counts : Map[String, Int]
  type_counts : Map[String, Int]
  known_entity_count : Int
  unknown_entity_count : Int
  relationship_count : Int
  geometry_count : Int
  property_set_count : Int
} derive(Debug)

///|
/// A shared building element that connects two spaces.
pub(all) struct SpaceAdjacency {
  left_space : Int
  right_space : Int
  shared_elements : Array[Int]
  boundary_count : Int
  distance : Double
} derive(Debug)

///|
/// Space adjacency graph for navigation and evacuation analysis.
pub(all) struct SpaceAdjacencyGraph {
  spaces : Array[Int]
  edges : Array[SpaceAdjacency]
  neighbors : Map[Int, Array[Int]]
} derive(Debug)

///|
/// Deterministic serialization metrics for a canonical STEP document.
pub(all) struct CanonicalStepStats {
  source_length : Int
  canonical_length : Int
  entity_count : Int
  changed : Bool
  duplicate_id_count : Int
  line_count : Int
} derive(Debug)