syntax = "proto3";

package nexuscrm.runtime.v1;

import "common/v1/common.proto";
import "runtime/v1/layout_authoring.proto";
import "runtime/v1/metadata_admin_read.proto";

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

// LayoutAuthoringV2Service owns current database Layout state. A successful
// mutation response means the transaction committed; no worker, Catalog
// publication, or status polling participates in the result.
service LayoutAuthoringV2Service {
  rpc ReadWorkspace(LayoutAuthoringV2ServiceReadWorkspaceRequest) returns (LayoutAuthoringV2ServiceReadWorkspaceResponse);
  rpc ReadStoredLayout(LayoutAuthoringV2ServiceReadStoredLayoutRequest) returns (LayoutAuthoringV2ServiceReadStoredLayoutResponse);
  rpc Mutate(LayoutAuthoringV2ServiceMutateRequest) returns (LayoutAuthoringV2ServiceMutateResponse);
}

message LayoutAuthoringV2ServiceReadWorkspaceRequest {
  string object_id = 1;
  reserved 2;
}

message LayoutAuthoringV2ServiceReadWorkspaceResponse {
  string object_id = 1;
  string layout_revision = 2;
  repeated StoredLayoutSummary layouts = 3;
  repeated StoredLayoutAssignment assignments = 4;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 5;
}

message LayoutAuthoringV2ServiceReadStoredLayoutRequest {
  string object_id = 1;
  string layout_id = 2;
  reserved 3;
}

message LayoutAuthoringV2ServiceReadStoredLayoutResponse {
  string layout_id = 1;
  string object_id = 2;
  string layout_revision = 3;
  LayoutAuthoringDocument document = 4;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 5;
  LayoutAuthoringStringList ordered_list_fields = 6;
  LayoutAuthoringStringList required_create_fields = 7;
}

message LayoutAuthoringV2ServiceMutateRequest {
  string object_id = 1;
  string expected_layout_revision = 2;
  repeated LayoutAuthoringHistoryRelatedListProjection history_related_lists = 3;
  oneof operation {
    LayoutAuthoringV2Create create = 10;
    LayoutAuthoringV2Update update = 11;
    LayoutAuthoringV2Clone clone = 12;
    LayoutAuthoringV2SetDefault set_default = 13;
    LayoutAuthoringV2Delete delete = 14;
    LayoutAuthoringV2Restore restore = 15;
    LayoutAuthoringV2UpsertAssignment assignment_upsert = 16;
    LayoutAuthoringV2DeleteAssignment assignment_delete = 17;
  }
}

message LayoutAuthoringV2Create {
  LayoutAuthoringDocument document = 1;
  LayoutAuthoringStringListChange list_fields = 2;
}

message LayoutAuthoringV2Update {
  string layout_id = 1;
  LayoutAuthoringDocumentChanges changes = 2;
  LayoutAuthoringStringListChange list_fields = 3;
}

message LayoutAuthoringV2Clone {
  string source_layout_id = 1;
  string target_layout_id = 2;
  string target_name = 3;
}

message LayoutAuthoringV2SetDefault { string layout_id = 1; }

message LayoutAuthoringV2Delete { string layout_id = 1; }

message LayoutAuthoringV2Restore {
  string layout_id = 1;
  uint32 history_version = 2;
}

message LayoutAuthoringV2UpsertAssignment {
  string layout_id = 1;
  LayoutAuthoringAssignmentSelectors selectors = 2;
}

message LayoutAuthoringV2DeleteAssignment { string assignment_id = 1; }

message LayoutAuthoringV2ServiceMutateResponse {
  bool changed = 1;
  string layout_revision = 2;
  optional string layout_id = 3;
  optional string assignment_id = 4;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 5;
}

// Application aliases for the closed record-history result of the root object.
message LayoutAuthoringHistoryRelatedListProjection {
  string object_id = 1;
  map<string, string> columns = 2;
}
