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";

// PageLayoutResolutionService selects one caller-effective neutral PageLayout.
// The authenticated Runtime session owns Profile and Role identity; callers can
// supply only the object, optional RecordType, and consistency floor.
service PageLayoutResolutionService {
  rpc ResolvePageLayout(ResolvePageLayoutRequest) returns (ResolvePageLayoutResponse);
  rpc ReadObjectListFields(ReadObjectListFieldsRequest) returns (ReadObjectListFieldsResponse);
}

message ResolvePageLayoutRequest {
  string object_id = 1;
  repeated string record_type_ids = 2;
  optional uint64 min_catalog_revision = 3;
}

message ResolvePageLayoutResponse {
  string object_id = 1;
  repeated ResolvedPageLayoutSelection selections = 2;
  repeated ResolvedPageLayout layouts = 3;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 4;
}

message ReadObjectListFieldsRequest {
  repeated string object_ids = 1;
  optional uint64 min_catalog_revision = 2;
}

message ReadObjectListFieldsResponse {
  repeated ObjectListFields objects = 1;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 2;
}

message ObjectListFields {
  string object_id = 1;
  ObjectListFieldIDs ordered_field_ids = 2;
}

// A singular wrapper preserves the difference between a deliberately empty
// list and an incomplete response that omitted current ListFields state.
message ObjectListFieldIDs {
  repeated string values = 1;
}

message ResolvedPageLayoutSelection {
  optional string record_type_id = 1;
  string layout_id = 2;
}

message ResolvedPageLayout {
  string layout_id = 1;
  string document_kind = 2;
  uint32 document_format_version = 3;
  bytes layout_document = 4;
  repeated ResolvedRelatedList related_lists = 5;
}

message ResolvedRelatedList {
  string display_object_id = 1;
  string display_lookup_field = 2;
  ResolvedRelatedQueryTarget query = 3;
  ResolvedRelatedDetailTarget detail = 4;
  ResolvedRelatedListTarget list = 5;
  ResolvedRelatedCreateTarget create = 6;
}

message ResolvedRelatedQueryTarget {
  string object_id = 1;
  string lookup_field = 2;
  string parent_field_id = 3;
}

message ResolvedRelatedDetailTarget {
  string object_id = 1;
}

message ResolvedRelatedListTarget {
  string object_id = 1;
  string filter_field = 2;
  string parent_field_id = 3;
}

message ResolvedRelatedCreateTarget {
  string kind = 1;
  string object_id = 2;
  repeated ResolvedRelatedFieldMapping prefill_mappings = 3;
}

message ResolvedRelatedFieldMapping {
  string target_field_id = 1;
  string source_field_id = 2;
}
