syntax = "proto3";

package wix.devcenter.apps.dependencies.v2;
option java_package = "com.wixpress.devcenter.apps.dependencies.v2";

import "google/api/annotations.proto";
import "google/protobuf/wrappers.proto";
import "google/protobuf/field_mask.proto";
import "wix/api/annotations.proto";
import "wix/api/callback.proto";
import "wix/api/entity.proto";
import "wix/api/permissions.proto";
import "wix/api/validations.proto";
import "wix/http/field_mask.proto";
import "wix/devcenter/apps/dependencies/v2/upstream/wix/common/paging.proto";
import "wix/devcenter/apps/dependencies/v2/upstream/wix/common/bulk.proto";
import "wix/devcenter/apps/dependencies/v2/app_dependency.proto";
import "wix/devcenter/apps/dependencies/v2/upstream/wix/common/tags.proto";
import "google/protobuf/struct.proto";
import "wix/devcenter/apps/dependencies/v2/upstream/wix/common/query.proto";
import "wix/api/api_errors.proto";

/**
  Manages AppDependencies
 */
service AppDependencies {
  option (.wix.api.service_maturity) = ALPHA;
  option (.wix.api.service_exposure) = PRIVATE;
  option (.wix.api.service_entity).message = "wix.devcenter.apps.dependencies.v2.AppDependency";

  /**
    Creates an AppDependency.
   */
  rpc CreateAppDependency (CreateAppDependencyRequest) returns (CreateAppDependencyResponse) {
    option (google.api.http).post = "/v2/app-dependencies";
    option (.wix.api.required) = "CreateAppDependencyRequest.app_dependency";
    option (.wix.api.required) = "CreateAppDependencyRequest.app_dependency.app_id";
    option (.wix.api.required) = "CreateAppDependencyRequest.app_dependency.version";
    option (.wix.api.required) = "CreateAppDependencyRequest.app_dependency.dependencies";
    option (.wix.api.permission) = {
      name: "DEV_CENTER.APP_DEPENDENCIES_CREATE"
      type: MANUAL
    };
    option (.wix.api.crud) = {
      method: CREATE
      create_options: {
        item_field: "app_dependency"
      }
    };
    option (.wix.api.emits) = {
      event_type: CREATED
    };
  }

  /**
    Retrieves an AppDependency.
   */
  rpc GetAppDependency (GetAppDependencyRequest) returns (GetAppDependencyResponse) {
    option (google.api.http).get = "/v2/app-dependencies/{app_id}";
    option (.wix.api.required) = "GetAppDependencyRequest.app_id";
    option (.wix.api.permission) = {
      name: "DEV_CENTER.APP_DEPENDENCIES_READ"
      type: MANUAL
    };
    option (.wix.api.crud) = {
      method: GET_ITEM
      get_options: {
        id_field: "app_id"
      }
    };
  }

  /**
  Updates a AppDependency.


  Each time the AppDependency is updated,
  `revision` increments by 1.
  The current `revision` must be passed when updating the AppDependency.
  This ensures you're working with the latest AppDependency
  and prevents unintended overwrites.
   */
  rpc UpdateAppDependency (UpdateAppDependencyRequest) returns (UpdateAppDependencyResponse) {
    option (google.api.http).patch = "/v2/app-dependencies/{app_dependency.id}";
    option (.wix.api.required) = "UpdateAppDependencyRequest.app_dependency";
    option (.wix.api.required) = "UpdateAppDependencyRequest.app_dependency.app_id";
    option (.wix.api.required) = "UpdateAppDependencyRequest.app_dependency.revision";
    option (.wix.api.permission) = {
      name: "DEV_CENTER.APP_DEPENDENCIES_WRITE"
      type: MANUAL
    };
    option (.wix.api.emits) = {
      event_type: UPDATED
    };
    /**
    (explanation)

    Update endpoint usually accepts FieldMask object. From REST client perspective it is redundant as it can be inferred
    from the actual JSON payload that was sent. This field annotation will tell the framework to use the JSON body and
    automatically construct the fieldmask.

    Refer to [AIP 4006 - Update Endpoints][1] to learn more about update endpoint and why we prefer patch over put.

    [1]: https://dev.wix.com/docs/rnd-general/articles/p13n-guidelines-aips/guidance-aips/standard-methods/4006-update
     */
    option (wix.http.infer_fieldmask) = {
      from: "app_dependency"
      fieldmask: "field_mask"
    };
    option (.wix.api.crud) = {
      method: UPDATE
      update_options: {
        item_field: "app_dependency"
        fieldmask_field: "field_mask"
      }
    };
  }

    /**
 Getting appDependency with the highest version for a specific appId.
*/
    rpc GetMaxVersionOfAppDependencies (GetMaxVersionOfAppDependenciesRequest) returns (GetMaxVersionOfAppDependenciesResponse) {
        option (google.api.http).get = "/v2/app-dependencies/get-max/{app_id}";
        option (.wix.api.required) = "GetMaxVersionOfAppDependenciesRequest.app_id";
        option (.wix.api.permission) = {
            name: "DEV_CENTER.APP_DEPENDENCIES_READ"
            type: MANUAL
        };
    }

  /**
    Retrieves a list of AppDependencies, given the provided [paging, filtering, and sorting][1].

    Up to 1,000 AppDependencies can be returned per request.

    To learn how to query AppDependencies, see [API Query Language][2].

    [1]: https://dev.wix.com/docs/rest/articles/getting-started/sorting-and-paging
    [2]: https://dev.wix.com/docs/rest/articles/getting-started/api-query-language
  */
  rpc QueryAppDependencies (QueryAppDependenciesRequest) returns (QueryAppDependenciesResponse) {
    option (.wix.api.permission).name = "DEV_CENTER.APP_DEPENDENCY_QUERY";
    option (google.api.http) = {
        post: "/v2/app-dependencies/query"
        additional_bindings : {
          get: "/v2/app-dependencies/query";
        }
    };
    option (.wix.api.crud) = {
      method: QUERY
      query_options: {
        items_field: 'app_dependencies'
      }
    };
  }

  // Synchronously update tags on multiple AppDependencies, by list of AppDependencies ids
  // A tag that appears both in the list of assign and unassign tags, will be assigned
  rpc BulkUpdateAppDependencyTags (BulkUpdateAppDependencyTagsRequest) returns (BulkUpdateAppDependencyTagsResponse) {
    option (google.api.http).post = "/v2/bulk/app-dependencies/update-tags";
    option (.wix.api.required) = "BulkUpdateAppDependencyTagsRequest.app_dependency_ids";
    option (.wix.api.permission).name = "DEV_CENTER.APP_DEPENDENCY_UPDATE_TAGS";
    option (.wix.api.crud) = {
      method: BULK_UPDATE_TAGS
    };
    option (wix.api.error) = {
      http_code: INVALID_ARGUMENT
      application_code: "EMPTY_ASSIGN_AND_UNASSIGN_LISTS"
    };
  }


  // Asynchronously update tags on multiple AppDependencies, by provided filter
  // An empty filter will update all AppDependencies
  // A tag that appears both in the list of assign and unassign tags, will be assigned
  rpc BulkUpdateAppDependencyTagsByFilter (BulkUpdateAppDependencyTagsByFilterRequest) returns (BulkUpdateAppDependencyTagsByFilterResponse) {
    option (google.api.http).post = "/v2/bulk/app-dependencies/update-tags-by-filter";
    option (.wix.api.required) = "BulkUpdateAppDependencyTagsByFilterRequest.filter";
    option (.wix.api.permission).name = "DEV_CENTER.APP_DEPENDENCY_UPDATE_TAGS";
    option (.wix.api.crud) = {
      method: BULK_UPDATE_TAGS_BY_FILTER
    };
    option (wix.api.error) = {
      http_code: INVALID_ARGUMENT
      application_code: "EMPTY_ASSIGN_AND_UNASSIGN_LISTS"
    };
  }

}

message CreateAppDependencyRequest {
  // AppDependency to be created.
  AppDependency app_dependency = 1;
}

message CreateAppDependencyResponse {
  // The created AppDependency.
  AppDependency app_dependency = 1;
}

message GetAppDependencyRequest {
  /**
  (explanation)

  Each field may contain various validations. Refer to [AIP 3004 - Field Validations][1] to learn about available
  validations like `maxLength` and `format`.

  [1]: https://dev.wix.com/docs/rnd-general/articles/p13n-guidelines-aips/guidance-aips/validations/3004-field-validations
  */

  // AppId of the AppDependency to retrieve.
  string app_id = 1 [(.wix.api.format) = GUID];
  google.protobuf.Int64Value version = 2; // App dependency version, default 0
  bool consistent = 3; // Fetch consistent data
}

message GetAppDependencyResponse {
  // The requested AppDependency.
  AppDependency app_dependency = 1;
}

message UpdateAppDependencyRequest {
  // AppDependency to be updated, may be partial.
  AppDependency app_dependency = 1;

  /*
  Set of fields to update.

  Fields that aren't included in `fieldMask.paths` are ignored.
  */
  google.protobuf.FieldMask field_mask = 2;
}

message UpdateAppDependencyResponse {
  // Updated AppDependency.
  AppDependency app_dependency = 1;
}

message QueryAppDependenciesRequest {
  // WQL expression.
  wix.devcenter.apps.dependencies.v2.upstream.wix.common.CursorQuery query = 1;
}

message QueryAppDependenciesResponse {
  // List of AppDependencies.
  repeated AppDependency app_dependencies = 1;

  // Paging metadata
  wix.devcenter.apps.dependencies.v2.upstream.wix.common.CursorPagingMetadata paging_metadata = 2;
}

message BulkUpdateAppDependencyTagsRequest {
  // List of NileProtoTagsEntities that their tags will update.
  repeated string app_dependency_ids = 1 [(.wix.api.minSize) = 1, (.wix.api.maxSize) = 100, (.wix.api.format) = GUID ];
  //List of Tags to assign
  wix.devcenter.apps.dependencies.v2.upstream.wix.common.Tags assign_tags = 2;
  //List of Tags to unAssign
  wix.devcenter.apps.dependencies.v2.upstream.wix.common.Tags unassign_tags = 3;
}

message BulkUpdateAppDependencyTagsResponse {
  //Results
  repeated BulkUpdateAppDependencyTagsResult results = 1 [(.wix.api.minSize) = 1, (.wix.api.maxSize) = 100];
  //Metadata regarding the bulk update operation
  wix.devcenter.apps.dependencies.v2.upstream.wix.common.BulkActionMetadata bulk_action_metadata = 2;

  message BulkUpdateAppDependencyTagsResult {
    //Metadata regarding the specific single update operation
    wix.devcenter.apps.dependencies.v2.upstream.wix.common.ItemMetadata item_metadata = 2;
  }
}

message BulkUpdateAppDependencyTagsByFilterRequest {
  //Filter
  google.protobuf.Struct filter = 1;
  //List of Tags to assign
  wix.devcenter.apps.dependencies.v2.upstream.wix.common.Tags assign_tags = 2;
  //List of Tags to unAssign
  wix.devcenter.apps.dependencies.v2.upstream.wix.common.Tags unassign_tags = 3;
}

message BulkUpdateAppDependencyTagsByFilterResponse {
  //Job ID
  string job_id = 1 [(.wix.api.format) = GUID];
}

message GetMaxVersionOfAppDependenciesRequest {
  // App id to get the max version for.
  string app_id = 1 [(.wix.api.format) = GUID];
  bool consistent = 2; // Fetch consistent data
}

message GetMaxVersionOfAppDependenciesResponse {
  google.protobuf.Int64Value app_dependency_max_version = 1;
}
