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

// PrivateListViewService is one app-blind capability on the private Runtime
// endpoint. Both operations are evaluated for the authenticated delegated
// principal. Product routes, visibility aliases, and payload wrappers stay L1-owned.
service PrivateListViewService {
  rpc ReadPrivateListViews(ReadPrivateListViewsRequest) returns (ReadPrivateListViewsResponse);
  rpc MutatePrivateListView(MutatePrivateListViewRequest) returns (MutatePrivateListViewResponse);
}

enum PrivateListViewReadSelector {
  PRIVATE_LIST_VIEW_READ_SELECTOR_UNSPECIFIED = 0;
  PRIVATE_LIST_VIEW_READ_SELECTOR_LIST = 1;
  PRIVATE_LIST_VIEW_READ_SELECTOR_DESCRIBE = 2;
}

enum PrivateListViewSortDirection {
  PRIVATE_LIST_VIEW_SORT_DIRECTION_UNSPECIFIED = 0;
  PRIVATE_LIST_VIEW_SORT_DIRECTION_ASCENDING = 1;
  PRIVATE_LIST_VIEW_SORT_DIRECTION_DESCENDING = 2;
}

enum PrivateListViewMutationOperation {
  PRIVATE_LIST_VIEW_MUTATION_OPERATION_UNSPECIFIED = 0;
  PRIVATE_LIST_VIEW_MUTATION_OPERATION_CREATE = 1;
  PRIVATE_LIST_VIEW_MUTATION_OPERATION_UPDATE = 2;
  PRIVATE_LIST_VIEW_MUTATION_OPERATION_DELETE = 3;
}

message ReadPrivateListViewsRequest {
  PrivateListViewReadSelector selector = 1;
  string object_id = 2;
  string view_id = 3;
  optional uint64 min_catalog_revision = 4;
}

// PrivateListView has no owner or visibility field. Every returned view is
// private and owned by the authenticated delegated principal by construction.
message PrivateListView {
  string view_id = 1;
  string object_id = 2;
  string label = 3;
  string filter_expression = 4;
  string filter_logic = 5;
  repeated string field_ids = 6;
  string sort_field_id = 7;
  PrivateListViewSortDirection sort_direction = 8;
  string stable_name = 9;
}

message ReadPrivateListViewsResponse {
  repeated PrivateListView views = 1;
  PrivateListView view = 2;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 3;
}

// PrivateListViewFieldSelection preserves PATCH presence even when field_ids
// is empty and therefore clears the selection.
message PrivateListViewFieldSelection {
  repeated string field_ids = 1;
}

message PrivateListViewPatch {
  optional string label = 1;
  optional string filter_expression = 2;
  optional string filter_logic = 3;
  PrivateListViewFieldSelection fields = 4;
  optional string sort_field_id = 5;
  optional PrivateListViewSortDirection sort_direction = 6;
  optional string stable_name = 7;
}

// Identity, tenant, application installation, owner, and authority are trusted
// transport facts. They are intentionally absent from this request.
message MutatePrivateListViewRequest {
  PrivateListViewMutationOperation operation = 1;
  string object_id = 3;
  string view_id = 4;
  PrivateListViewPatch patch = 5;
  optional uint64 min_catalog_revision = 6;
}

message MutatePrivateListViewResponse {
  PrivateListView view = 1;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 2;
}
