syntax = "proto3";

package wix.devcenter.components.overrides.v1;
option java_package = "com.wixpress.devcenter.components.overrides.v1";

import "google/protobuf/field_mask.proto";
import "google/api/annotations.proto";
import "wix/api/annotations.proto";
import "wix/api/validations.proto";
import "wix/api/entity.proto";
import "wix/api/permissions.proto";
import "wix/api/callback.proto";
import "wix/http/field_mask.proto";
import "wix/common/query.proto";
import "wix/common/paging.proto";
import "wix/devcenter/components/overrides/v1/components_override.proto";
import "com/wix/components/overrides/upstream/wix/common/query.proto";
import "com/wix/components/overrides/upstream/wix/common/paging.proto";

/**
  Manages ComponentsOverrides
 */
service ComponentsOverridesService {
  option (wix.api.service_maturity) = ALPHA;
  option (wix.api.service_exposure) = INTERNAL;
  option (wix.api.service_entity).message = "wix.devcenter.components.overrides.v1.ComponentsOverride";

  /**
    Creates a new ComponentsOverride.
   */
  rpc CreateComponentsOverride (CreateComponentsOverrideRequest) returns (CreateComponentsOverrideResponse) {
    option (google.api.http).post = "/v1/components-override";
    option (wix.api.permission).name = "DEV_CENTER.COMPONENTS_OVERRIDE_WRITE";
    option (wix.api.required) = "CreateComponentsOverrideRequest.components_override";
    option (wix.api.required) = "CreateComponentsOverrideRequest.components_override.id";
    option (wix.api.required) = "CreateComponentsOverrideRequest.components_override.app_id";
    option (wix.api.required) = "CreateComponentsOverrideRequest.components_override.app_version";
    option (wix.api.required) = "CreateComponentsOverrideRequest.components_override.external_id";
    option (wix.api.crud) = {
      method: CREATE
      create_options: {
        item_field: "components_override"
      }
    };
    option (wix.api.callback) = {
      event_type: CREATED
    };
  }

  /**
    Retrieves a ComponentsOverride.
   */
  rpc GetComponentsOverride (GetComponentsOverrideRequest) returns (GetComponentsOverrideResponse) {
    option (google.api.http).get = "/v1/components-override/{components_override_id}";
    option (wix.api.required) = "GetComponentsOverrideRequest.components_override_id";
    option (.wix.api.permission) = {
      name: "DEV_CENTER.COMPONENTS_OVERRIDE_READ"
      type: MANUAL
    };
    option (.wix.api.additional_permission) = {
      name: "DEV_CENTER.READ_COMPONENTS_OVERRIDE"
      type: MANUAL
    };
    option (wix.api.crud) = {
      method: GET_ITEM
      get_options: {
        id_field: "components_override_id"
      }
    };
  }

  /**
  Retrieves list of ComponentsOverride.
 */
  rpc ListComponentsOverrides (ListComponentsOverridesRequest) returns (ListComponentsOverridesResponse) {
    option (google.api.http).get = "/v1/components-overrides";
    option (wix.api.permission).name = "DEV_CENTER.COMPONENTS_OVERRIDE_READ";
    option (wix.api.required) = "ListComponentsOverridesRequest.components_override_ids";
    option (wix.api.crud) = {
      method: LIST
      list_options: {
        items_field: "components_overrides"
      }
    };
  }

  /**
  Update ComponentsOverride.
  */
  rpc UpdateComponentsOverrides (UpdateComponentsOverrideRequest) returns (UpdateComponentsOverrideResponse) {
    option (google.api.http).patch = "/v1/components-override/{components_override.id}";
    option (wix.api.permission).name = "DEV_CENTER.COMPONENTS_OVERRIDE_WRITE";
    option (wix.api.required) = "UpdateComponentsOverrideRequest.components_override";
    option (wix.api.required) = "UpdateComponentsOverrideRequest.components_override.id";
    option (wix.api.required) = "UpdateComponentsOverrideRequest.components_override.app_id";
    option (wix.api.required) = "UpdateComponentsOverrideRequest.components_override.app_version";
    option (wix.api.required) = "UpdateComponentsOverrideRequest.components_override.external_id";
    option (wix.api.crud) = {
      method: UPDATE
    };
    option (wix.api.callback) = {
      event_type: UPDATED
    };
  }

  /**
  Promote an override to the production version of the app.
  This includes committing the components to the Dev Center components service and then promoting a new version to production.
  Does not delete the override itself, it should be done in a separate call when the override is ready to be deleted
   */
  rpc PromoteOverride (PromoteOverrideRequest) returns (PromoteOverrideResponse) {
    option (google.api.http).post = "/v1/components-override/promote";
    option (wix.api.permission).name = "DEV_CENTER.COMPONENTS_OVERRIDE_PROMOTE";
    option (wix.api.required) = "PromoteOverrideRequest.components_override_id";
    option (.wix.api.callback) = {
      event_type: ACTION
      payload: "wix.devcenter.components.overrides.v1.OverrideActionDomainEventEntity"
    };
  }

  /**
  Commit the override components to Dev Center components. Does not promote a new version to production.
  Does not delete the override itself, it should be done in a separate call when the override is ready to be deleted
   */
  rpc CommitOverride (CommitOverrideRequest) returns (CommitOverrideResponse) {
    option (google.api.http).post = "/v1/components-override/commit";
    option (wix.api.permission).name = "DEV_CENTER.COMPONENTS_OVERRIDE_COMMIT";
    option (wix.api.required) = "CommitOverrideRequest.components_override_id";
    option (.wix.api.callback) = {
      event_type: ACTION
      payload: "wix.devcenter.components.overrides.v1.OverrideActionDomainEventEntity"
    };
  }

  /**
    Deleting a ComponentsOverride permanently removes them from the ComponentsOverride List.
   */
  rpc DeleteComponentsOverride (DeleteComponentsOverrideRequest) returns (DeleteComponentsOverrideResponse) {
    option (google.api.http).delete = "/v1/components-override/{components_override_id}";
    option (wix.api.permission).name = "DEV_CENTER.COMPONENTS_OVERRIDE_PROMOTE";
    option (wix.api.required) = "DeleteComponentsOverrideRequest.components_override_id";
    option (wix.api.callback) = {
      event_type: DELETED
    };
    option (wix.api.crud) = {
      method: DELETE
      delete_options: {
        id_field: "components_override_id"
      }
    };
  }

  /**
  Query ComponentsOverrides.
 */
  rpc QueryComponentsOverrides (QueryComponentsOverridesRequest) returns (QueryComponentsOverridesResponse) {
    option (google.api.http).post = "/v1/components-overrides/query";
    option (wix.api.permission).name = "DEV_CENTER.COMPONENTS_OVERRIDE_READ";
    option (wix.api.crud) = {
      method: QUERY
      query_options: {
        paging {
          type: CURSOR
          paging_metadata_field: "paging_metadata" // in response object
        };
        query_field: "query"
        items_field: "components_overrides"
        wql: {
          pattern: {
            operator: ALL_APPLICABLE_OPERATORS // will be smart - allow by type
            field: "id"
            field: "app_id"
            sort: NONE // NONE|ASC|DESC|BOTH. NONE is the default.
          }
        }
      }
    };
  }
}

message CreateComponentsOverrideRequest {
  // ComponentsOverride to be created.
  ComponentsOverride components_override = 1;
}

message CreateComponentsOverrideResponse {
  // The created ComponentsOverride.
  ComponentsOverride components_override = 1;
}

message GetComponentsOverrideRequest {
  // ID of the ComponentsOverride to retrieve.
  string components_override_id = 1 [(wix.api.maxLength) = 50];
}

message GetComponentsOverrideResponse {
  // The requested ComponentsOverride.
  ComponentsOverride components_override = 1;
}

message ListComponentsOverridesRequest {
  // IDs of the ComponentsOverride to retrieve.
  repeated string components_override_ids = 1 [(.wix.api.maxSize) = 50];
}

message ListComponentsOverridesResponse {
  // The requested list of ComponentsOverride.
  repeated ComponentsOverride components_overrides = 1 [(.wix.api.maxSize) = 50];
}

message UpdateComponentsOverrideRequest {
  // ComponentsOverride to be updated.
  ComponentsOverride components_override = 1;
  // The list of fields to update.
  google.protobuf.FieldMask field_mask = 2;
}

message UpdateComponentsOverrideResponse {
  // The updated ComponentsOverride.
  ComponentsOverride components_override = 1;
}

message DeleteComponentsOverrideRequest {
  // Id of the ComponentsOverride to delete.
  string components_override_id = 1 [(wix.api.maxLength) = 50];
}

message DeleteComponentsOverrideResponse {
}

message PromoteOverrideRequest {
  string components_override_id = 1 [(wix.api.maxLength) = 50]; // Id of the ComponentsOverride to promote.
}

message PromoteOverrideResponse {
}

message CommitOverrideRequest {
  string components_override_id = 1 [(wix.api.maxLength) = 50]; // Id of the ComponentsOverride to commit
  bool only_emit_event = 2; // This is a temporary flag until Wix CLI open their experiment

}

message CommitOverrideResponse {
}

enum DeleteType {
  UNKNOWN_DELETE_TYPE = 0;
  DISMISS = 1;
  PROMOTE = 2;
}

message QueryComponentsOverridesRequest {
  com.wix.components.overrides.upstream.wix.common.QueryV2 query = 1; // WQL expression
}

message QueryComponentsOverridesResponse {
  repeated ComponentsOverride components_overrides = 1; // The retrieved Components Overrides
  com.wix.components.overrides.upstream.wix.common.PagingMetadataV2 paging_metadata = 2; // Paging data
}

message OverrideActionDomainEventEntity {
  ComponentsOverride components_override = 1; // The override that had an event
  ActionType action_type = 2; // The action which happened
}

enum ActionType {
  UNKNOWN_ACTION = 0;
  OVERRIDE_PROMOTED = 1;
  OVERRIDE_COMMITTED = 2;
}