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

// LayoutAuthoringService exposes one typed Layout lifecycle. Authentication,
// delegated authority, admission, and transport policy remain host-owned.
service LayoutAuthoringService {
  rpc Submit(LayoutAuthoringServiceSubmitRequest) returns (LayoutAuthoringServiceSubmitResponse);
  rpc GetCommand(LayoutAuthoringServiceGetCommandRequest) returns (LayoutAuthoringServiceGetCommandResponse);
  rpc ReadWorkspace(LayoutAuthoringServiceReadWorkspaceRequest) returns (LayoutAuthoringServiceReadWorkspaceResponse);
  rpc ReadStoredLayout(LayoutAuthoringServiceReadStoredLayoutRequest) returns (LayoutAuthoringServiceReadStoredLayoutResponse);
}

enum LayoutAuthoringWaitMode {
  LAYOUT_AUTHORING_WAIT_MODE_UNSPECIFIED = 0;
  LAYOUT_AUTHORING_WAIT_MODE_WAIT_FOR_TERMINAL = 1;
  LAYOUT_AUTHORING_WAIT_MODE_RETURN_AFTER_ACCEPTANCE = 2;
}

enum LayoutAuthoringCommandState {
  LAYOUT_AUTHORING_COMMAND_STATE_UNSPECIFIED = 0;
  LAYOUT_AUTHORING_COMMAND_STATE_ACCEPTED = 1;
  LAYOUT_AUTHORING_COMMAND_STATE_IN_PROGRESS = 2;
  LAYOUT_AUTHORING_COMMAND_STATE_SUCCEEDED = 3;
  LAYOUT_AUTHORING_COMMAND_STATE_FAILED = 4;
}

enum LayoutAuthoringCommandFailureCategory {
  LAYOUT_AUTHORING_COMMAND_FAILURE_CATEGORY_UNSPECIFIED = 0;
  LAYOUT_AUTHORING_COMMAND_FAILURE_CATEGORY_INVALID_REQUEST = 1;
  LAYOUT_AUTHORING_COMMAND_FAILURE_CATEGORY_FORBIDDEN = 2;
  LAYOUT_AUTHORING_COMMAND_FAILURE_CATEGORY_MISSING = 3;
  LAYOUT_AUTHORING_COMMAND_FAILURE_CATEGORY_CONFLICT = 4;
}

enum LayoutAuthoringChangeMode {
  LAYOUT_AUTHORING_CHANGE_MODE_UNSPECIFIED = 0;
  LAYOUT_AUTHORING_CHANGE_MODE_PRESERVE = 1;
  LAYOUT_AUTHORING_CHANGE_MODE_SET = 2;
  LAYOUT_AUTHORING_CHANGE_MODE_CLEAR = 3;
}

message LayoutAuthoringCatalogViewRef {
  uint64 catalog_revision = 1;
  string authoring_view_digest = 2;
}

message LayoutAuthoringServiceSubmitRequest {
  string command_id = 1;
  string idempotency_key = 2;
  uint64 base_catalog_revision = 3;
  LayoutAuthoringWaitMode wait_mode = 4;
  oneof operation {
    LayoutAuthoringCreateLayout create = 10;
    LayoutAuthoringUpdateLayout update = 11;
    LayoutAuthoringCloneLayout clone = 12;
    LayoutAuthoringSetDefaultLayout set_default = 13;
    LayoutAuthoringDeleteLayout delete = 14;
    LayoutAuthoringRestoreLayout restore = 15;
    LayoutAuthoringUpsertAssignment assignment_upsert = 16;
    LayoutAuthoringDeleteAssignment assignment_delete = 17;
  }
}

message LayoutAuthoringCreateLayout {
  LayoutAuthoringDocument document = 1;
  LayoutAuthoringStringListChange list_fields = 2;
  optional string expected_workspace_version = 3;
}

message LayoutAuthoringUpdateLayout {
  string layout_id = 1;
  string expected_aggregate_version = 2;
  LayoutAuthoringDocumentChanges changes = 3;
  LayoutAuthoringStringListChange list_fields = 4;
}

message LayoutAuthoringCloneLayout {
  string source_layout_id = 1;
  string source_resource_version = 2;
  string target_layout_id = 3;
  string target_name = 4;
  string expected_workspace_version = 5;
}

message LayoutAuthoringSetDefaultLayout {
  string object_id = 1;
  string layout_id = 2;
  string expected_workspace_version = 3;
}

message LayoutAuthoringDeleteLayout {
  string layout_id = 1;
  string expected_resource_version = 2;
  string expected_workspace_version = 3;
}

message LayoutAuthoringRestoreLayout {
  string layout_id = 1;
  uint32 history_version = 2;
  string expected_resource_version = 3;
}

message LayoutAuthoringAssignmentSelectors {
  optional string profile_id = 1;
  optional string role_id = 2;
  optional string record_type_id = 3;
}

message LayoutAuthoringUpsertAssignment {
  string object_id = 1;
  string layout_id = 2;
  LayoutAuthoringAssignmentSelectors selectors = 3;
  string expected_workspace_version = 4;
}

message LayoutAuthoringDeleteAssignment {
  string assignment_id = 1;
  string expected_resource_version = 2;
  string expected_workspace_version = 3;
}

message LayoutAuthoringServiceGetCommandRequest {
  string command_id = 1;
  string request_digest = 2;
}

message LayoutAuthoringServiceSubmitResponse {
  bool duplicate = 1;
  LayoutAuthoringServiceGetCommandResponse command = 2;
}

message LayoutAuthoringServiceGetCommandResponse {
  string command_id = 1;
  string request_digest = 2;
  LayoutAuthoringCommandState state = 3;
  oneof result {
    LayoutAuthoringCreateLayoutResult create = 10;
    LayoutAuthoringUpdateLayoutResult update = 11;
    LayoutAuthoringCloneLayoutResult clone = 12;
    LayoutAuthoringSetDefaultLayoutResult set_default = 13;
    LayoutAuthoringDeleteLayoutResult delete = 14;
    LayoutAuthoringRestoreLayoutResult restore = 15;
    LayoutAuthoringUpsertAssignmentResult assignment_upsert = 16;
    LayoutAuthoringDeleteAssignmentResult assignment_delete = 17;
  }
  LayoutAuthoringCommandFailure failure = 20;
  LayoutAuthoringCatalogViewRef catalog_view_ref = 21;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 22;
}

message LayoutAuthoringCommandFailure {
  LayoutAuthoringCommandFailureCategory category = 1;
  string message = 2;
  // Canonical JSON Pointer rooted at the semantic CommandOperation.
  string field_reference = 3;
}

message LayoutAuthoringCreateLayoutResult {
  LayoutAuthoringLayoutSnapshot layout = 1;
}

message LayoutAuthoringUpdateLayoutResult {
  LayoutAuthoringLayoutSnapshot layout = 1;
}

message LayoutAuthoringCloneLayoutResult {
  LayoutAuthoringLayoutSnapshot layout = 1;
}

message LayoutAuthoringSetDefaultLayoutResult {
  string object_id = 1;
  string layout_id = 2;
  string workspace_version = 3;
}

message LayoutAuthoringDeleteLayoutResult {
  string layout_id = 1;
  string object_id = 2;
  string workspace_version = 3;
}

message LayoutAuthoringRestoreLayoutResult {
  LayoutAuthoringLayoutSnapshot layout = 1;
  uint32 history_version = 2;
}

message LayoutAuthoringUpsertAssignmentResult {
  LayoutAuthoringAssignmentSummary assignment = 1;
  string workspace_version = 2;
}

message LayoutAuthoringDeleteAssignmentResult {
  string assignment_id = 1;
  string object_id = 2;
  string workspace_version = 3;
}

message LayoutAuthoringLayoutSnapshot {
  LayoutAuthoringDocument document = 1;
  repeated string ordered_list_fields = 2;
  string resource_version = 3;
  string aggregate_version = 4;
  string workspace_version = 5;
}

message LayoutAuthoringServiceReadWorkspaceRequest {
  string object_id = 1;
  LayoutAuthoringCatalogViewRef view_ref = 2;
}

message LayoutAuthoringServiceReadWorkspaceResponse {
  string object_id = 1;
  string workspace_version = 2;
  repeated LayoutAuthoringLayoutSummary layouts = 3;
  repeated LayoutAuthoringAssignmentSummary assignments = 4;
  LayoutAuthoringCatalogViewRef view_ref = 5;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 6;
}

message LayoutAuthoringServiceReadStoredLayoutRequest {
  string layout_id = 1;
  LayoutAuthoringCatalogViewRef view_ref = 2;
}

message LayoutAuthoringServiceReadStoredLayoutResponse {
  LayoutAuthoringDocument document = 1;
  repeated string ordered_list_fields = 2;
  LayoutAuthoringLifecycleState lifecycle = 3;
  string resource_version = 4;
  string aggregate_version = 5;
  string workspace_version = 6;
  LayoutAuthoringCatalogViewRef view_ref = 7;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 8;
  LayoutAuthoringCreateContext create_context = 9;
}

message LayoutAuthoringLayoutSummary {
  string layout_id = 1;
  string object_id = 2;
  string name = 3;
  string kind = 4;
  bool is_default = 5;
  string resource_version = 6;
  string created_at = 7;
  string last_modified_at = 8;
}

message LayoutAuthoringAssignmentSummary {
  string assignment_id = 1;
  string object_id = 2;
  string layout_id = 3;
  LayoutAuthoringAssignmentSelectors selectors = 4;
  uint32 specificity = 5;
  string resource_version = 6;
  string created_at = 7;
  string last_modified_at = 8;
  optional string created_by_id = 9;
}

message LayoutAuthoringLifecycleState {
  string provenance_kind = 1;
  uint32 generation_version = 2;
  bool customized = 3;
  bool is_default = 4;
  string created_at = 5;
  string last_modified_at = 6;
}

message LayoutAuthoringDocument {
  string layout_id = 1;
  string object_id = 2;
  string name = 3;
  string kind = 4;
  uint32 layout_version = 5;
  repeated string compact_fields = 6;
  repeated string tabs = 7;
  repeated LayoutAuthoringSection sections = 8;
  repeated LayoutAuthoringRelatedList related_lists = 9;
  repeated LayoutAuthoringRecordPanel record_panels = 10;
  LayoutAuthoringActionPlacement header_actions = 11;
  LayoutAuthoringActionPlacement quick_actions = 12;
  repeated LayoutAuthoringRecordStateRule record_states = 13;
  repeated string primary_header_actions = 14;
}

message LayoutAuthoringSection {
  string section_id = 1;
  string label = 2;
  optional string tab_id = 3;
  optional string kind = 4;
  optional string component_name = 5;
  repeated LayoutAuthoringNamedValue component_configuration = 6;
  uint32 columns = 7;
  repeated string fields = 8;
  repeated LayoutAuthoringFieldLabel field_labels = 9;
  repeated LayoutAuthoringFieldBehaviorGroup field_behaviors = 10;
  optional string visibility_condition = 11;
}

message LayoutAuthoringFieldLabel {
  string field_reference = 1;
  string label = 2;
}

message LayoutAuthoringFieldBehaviorGroup {
  string field_reference = 1;
  LayoutAuthoringFieldBehaviorList behaviors = 2;
}

message LayoutAuthoringFieldBehaviorList {
  repeated LayoutAuthoringFieldBehavior values = 1;
}

message LayoutAuthoringFieldBehavior {
  string mode = 1;
  string behavior = 2;
}

message LayoutAuthoringRelatedList {
  string related_list_id = 1;
  string label = 2;
  string secondary_label = 3;
  string object_id = 4;
  string lookup_field_id = 5;
  repeated string field_ids = 6;
  repeated LayoutAuthoringPrefillMapping prefill_mappings = 7;
}

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

message LayoutAuthoringRecordPanel {
  string panel_id = 1;
  string renderer_id = 2;
  string position = 3;
  string tab_id = 4;
  string tab_label = 5;
  int32 order = 6;
  string visibility_condition = 7;
  repeated LayoutAuthoringNamedValue renderer_configuration = 8;
}

message LayoutAuthoringActionPlacement {
  bool authored = 1;
  repeated LayoutAuthoringAction actions = 2;
}

message LayoutAuthoringAction {
  string name = 1;
  string label = 2;
  string kind = 3;
  optional string icon = 4;
  optional string target_object_id = 5;
  optional string component = 6;
  optional string visibility_condition = 7;
  repeated LayoutAuthoringNamedValue configuration = 8;
  repeated LayoutAuthoringNamedValue extensions = 9;
}

message LayoutAuthoringRecordStateRule {
  string rule_id = 1;
  string condition = 2;
  int32 priority = 3;
  LayoutAuthoringRecordStateBanner banner = 4;
  repeated LayoutAuthoringRecordStateLink links = 5;
  repeated LayoutAuthoringRecordStateActionOverride action_overrides = 6;
  LayoutAuthoringRecordStateReadOnly read_only = 7;
}

message LayoutAuthoringRecordStateBanner {
  string severity = 1;
  string title = 2;
  string body = 3;
  repeated string link_ids = 4;
}

message LayoutAuthoringRecordStateLink {
  string link_id = 1;
  string label = 2;
  string reference_field_id = 3;
  string object_id = 4;
  string placement = 5;
}

message LayoutAuthoringRecordStateActionOverride {
  string action_key = 1;
  string mode = 2;
  string reason = 3;
}

message LayoutAuthoringRecordStateReadOnly {
  bool enabled = 1;
  string reason = 2;
}

message LayoutAuthoringNamedValue {
  string name = 1;
  LayoutAuthoringValue value = 2;
}

message LayoutAuthoringValue {
  oneof value {
    LayoutAuthoringNullValue null_value = 1;
    bool boolean_value = 2;
    string string_value = 3;
    string decimal_value = 4;
    LayoutAuthoringValueList list_value = 5;
    LayoutAuthoringNamedValueList object_value = 6;
  }
}

message LayoutAuthoringNullValue {}

message LayoutAuthoringValueList {
  repeated LayoutAuthoringValue values = 1;
}

message LayoutAuthoringNamedValueList {
  repeated LayoutAuthoringNamedValue values = 1;
}

message LayoutAuthoringStringList {
  repeated string values = 1;
}

message LayoutAuthoringStringChange {
  LayoutAuthoringChangeMode mode = 1;
  optional string value = 2;
}

message LayoutAuthoringUint32Change {
  LayoutAuthoringChangeMode mode = 1;
  optional uint32 value = 2;
}

message LayoutAuthoringStringListChange {
  LayoutAuthoringChangeMode mode = 1;
  LayoutAuthoringStringList value = 2;
}

message LayoutAuthoringSectionList {
  repeated LayoutAuthoringSection values = 1;
}

message LayoutAuthoringSectionListChange {
  LayoutAuthoringChangeMode mode = 1;
  LayoutAuthoringSectionList value = 2;
}

message LayoutAuthoringRelatedListList {
  repeated LayoutAuthoringRelatedList values = 1;
}

message LayoutAuthoringRelatedListListChange {
  LayoutAuthoringChangeMode mode = 1;
  LayoutAuthoringRelatedListList value = 2;
}

message LayoutAuthoringRecordPanelList {
  repeated LayoutAuthoringRecordPanel values = 1;
}

message LayoutAuthoringRecordPanelListChange {
  LayoutAuthoringChangeMode mode = 1;
  LayoutAuthoringRecordPanelList value = 2;
}

message LayoutAuthoringActionPlacementChange {
  LayoutAuthoringChangeMode mode = 1;
  LayoutAuthoringActionPlacement value = 2;
}

message LayoutAuthoringRecordStateRuleList {
  repeated LayoutAuthoringRecordStateRule values = 1;
}

message LayoutAuthoringRecordStateRuleListChange {
  LayoutAuthoringChangeMode mode = 1;
  LayoutAuthoringRecordStateRuleList value = 2;
}

message LayoutAuthoringDocumentChanges {
  LayoutAuthoringStringChange name = 1;
  LayoutAuthoringStringChange kind = 2;
  LayoutAuthoringUint32Change layout_version = 3;
  LayoutAuthoringStringListChange compact_fields = 4;
  LayoutAuthoringStringListChange tabs = 5;
  LayoutAuthoringSectionListChange sections = 6;
  LayoutAuthoringRelatedListListChange related_lists = 7;
  LayoutAuthoringRecordPanelListChange record_panels = 8;
  LayoutAuthoringActionPlacementChange header_actions = 9;
  LayoutAuthoringActionPlacementChange quick_actions = 10;
  LayoutAuthoringRecordStateRuleListChange record_states = 11;
  LayoutAuthoringStringListChange primary_header_actions = 12;
}

// Read-only create requirements from the same immutable view as the layout.
// Message absence means unknown (an older view); presence with no fields means
// known-empty. The list is canonical and never enters a write request.
message LayoutAuthoringCreateContext {
  repeated string required_fields = 1;
}
