syntax = "proto3";

package nexuscrm.runtime.v1;

import "common/v1/common.proto";

option go_package = "github.com/pingcap-inc/nexuscrm-contract/wire-go/gen/runtime/v1;runtimev1";

// RelatedRecordCountService executes one bounded, already-normalized batch of
// relationship-field counts. Product route grammar and response projection
// remain caller-owned and never cross this boundary.
service RelatedRecordCountService {
  rpc CountRelatedRecords(CountRelatedRecordsRequest) returns (CountRelatedRecordsResponse);
}

message RelatedRecordCountSpec {
  // Parent identity is an authorization fact even when match_value selects an
  // alternate metadata-owned parent key.
  string parent_record_id = 1;
  string child_object_id = 2;
  string relationship_field = 3;
  // Empty means parent_record_id.
  string match_value = 4;
}

// Identity and authority are authenticated transport context. They are never
// caller-selected semantic request fields.
message CountRelatedRecordsRequest {
  repeated RelatedRecordCountSpec counts = 1;
  optional uint64 min_catalog_revision = 2;
}

enum RelatedRecordCountOutcome {
  RELATED_RECORD_COUNT_OUTCOME_UNSPECIFIED = 0;
  RELATED_RECORD_COUNT_OUTCOME_COUNTED = 1;
  RELATED_RECORD_COUNT_OUTCOME_NOT_AVAILABLE = 2;
}

message RelatedRecordCountResult {
  uint32 index = 1;
  RelatedRecordCountOutcome outcome = 2;
  // COUNTED includes an exact zero. NOT_AVAILABLE requires zero.
  uint64 count = 3;
}

message CountRelatedRecordsResponse {
  repeated RelatedRecordCountResult results = 1;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 2;
}
