syntax = "proto3";

package wix.devcenter.apps.permissions.v1;
option java_package = "com.wixpress.devcenter.apps.permissions.v1";

import "google/protobuf/field_mask.proto";
import "google/api/annotations.proto";
import "wix/api/annotations.proto";
import "wix/api/validations.proto";
import "wix/common/paging.proto";
import "wix/common/query.proto";
import "wix/api/entity.proto";
import "wix/api/permissions.proto";
import "wix/api/callback.proto";
import "wix/http/field_mask.proto";
import "wix/devcenter/apps/permissions/v1/app_permission.proto";
import "google/protobuf/empty.proto";
import "wix/common/domainevents/domainevents.proto";
import "wix/api/subscription.proto";
import "permissions_messages.proto";
import "wix/sdk/definitions.proto";

// Manages AppPermissions
service AppPermissionsService {
  option (.wix.api.service_maturity) = GA;
  option (.wix.api.service_exposure) = PUBLIC;
  option (wix.api.service_entity).message = "wix.devcenter.apps.permissions.v1.AppPermission";

  // Adds a permission to the specified app.
  //
  // When users install your app on a site, it requests this permission.
  //
  // > **Important**: Disregard the **Authentication** note below. You can only call this method by authenticating with an account-level [API key](https://dev.wix.com/docs/rest/articles/get-started/api-keys).    
  rpc CreateAppPermission (CreateAppPermissionRequest) returns (CreateAppPermissionResponse) {
    option (google.api.http).post = "/v1/app-permissions";
    option (.wix.api.permission) = {
        name: "DEV_CENTER.CREATE_APP_PERMISSION";
        type: MANUAL;
    };
    option (.wix.api.required) = "CreateAppPermissionRequest.app_permission";
    option (.wix.api.required) = "CreateAppPermissionRequest.app_permission.app_id";
    option (.wix.api.required) = "CreateAppPermissionRequest.app_permission.permission";
    option (.wix.api.required) = "CreateAppPermissionRequest.app_permission.permission.permission_id";

    // Triggered when a permission is added to an app.
    option (.wix.api.crud) = {method: CREATE};
    option (.wix.api.emits) = {
      event_type: CREATED
    };
  }

  // Retrieves the specified permission for the specified app. 
  rpc GetAppPermission (GetAppPermissionRequest) returns (GetAppPermissionResponse) {
    option (wix.api.exposure) = INTERNAL;
    option (.wix.api.permission) = {
      name:"DEV_CENTER.READ_APP_PERMISSIONS"
      type: MANUAL;
    };
    option (.wix.api.required) = "GetAppPermissionRequest.app_id";
    option (.wix.api.required) = "GetAppPermissionRequest.permission_id";
  }

  // Lists all permissions for the specified app.
  //
  // These are the permissions your app requests when users install it on a site.
  rpc ListAppPermissions (ListAppPermissionsRequest) returns (ListAppPermissionsResponse) {
    option (google.api.http).get = "/v1/app-permissions";
    option (.wix.api.permission) = {
      name:"DEV_CENTER.READ_APP_PERMISSIONS"
      type: MANUAL;
    };
    option (.wix.api.required) = "ListAppPermissionsRequest.app_id";
  }

  // Deletes a permission from the specified app.
  //
  // After deleting a permission, your app no longer requests it when users install it on a site. As a result, the app can no longer make calls to APIs that require this permission.
  //
  // > **Important**: Disregard the **Authentication** note below. You can only call this method by authenticating with an account-level [API key](https://dev.wix.com/docs/rest/articles/get-started/api-keys).    
  rpc DeleteAppPermission (DeleteAppPermissionRequest) returns (DeleteAppPermissionResponse) {
    option (google.api.http).delete = "/v1/app-permissions";
    option (.wix.api.permission) = {
      name: "DEV_CENTER.DELETE_APP_PERMISSION";
      type: MANUAL;
    };
    option (.wix.api.required) = "DeleteAppPermissionRequest.app_id";
    option (.wix.api.required) = "DeleteAppPermissionRequest.permission_id";
    option (.wix.sdk.signature) = {
      params: "app_id"
      params: "permission_id"
    };
    option (.wix.api.emits) = {
      event_type: DELETED
    };
  }

  /*
    Consuming app domain event and creating permissions for app
  */
  rpc ConsumeAppDomainEvents (wix.common.domainevents.DomainEvent) returns (google.protobuf.Empty) {
    option (wix.api.exposure) = PRIVATE;
    option (wix.api.subscription) = {
      entity: "wix.devcenter.apps.v1.App",
    };
  }

  /*
    Consuming old app domain event and updating permissions for app - part of migration flow
  */
  rpc ConsumeOldAppPermissionChanged (com.wixpress.appservice.api.OldPermissionChangedEvent) returns (google.protobuf.Empty) {
    option (wix.api.exposure) = PRIVATE;
    option (wix.api.subscription) = {
      topic: "app-permission-changed-in-old-service"
      segment: USERS
    };
  }

  // Consumes business schema domain events to request permissions for apps
  rpc ConsumeSchemaChangedEvent (wix.common.domainevents.DomainEvent) returns (google.protobuf.Empty) {
    option (wix.api.exposure) = PRIVATE;
    option (wix.api.subscription) = {
      entity: "wix.coreservices.businessschema.v1.Entity",
    };
  }
}

message CreateAppPermissionRequest {
  // Permission to add to the specified app.
  AppPermission app_permission = 1; 
}

message CreateAppPermissionResponse {
  // Details of permission that was added to the app.
  AppPermission app_permission = 1;
}

message GetAppPermissionRequest {
  // App ID. 
  //
  // You can find the app ID on the [Home page](https://manage.wix.com/app-selector?title=Select+an+App&primaryButtonText=Select+Site&actionUrl=https%3A%2F%2Fdev.wix.com%2Fapps%2F%7BappId%7D%home) in the app's dashboard.
  string app_id = 1 [(.wix.api.format) = GUID];
  // Permission ID.
  //
  // To find the permission ID, navigate to the [Permissions page](https://dev.wix.com/app-selector?title=Select+an+App&primaryButtonText=Select+Site&actionUrl=https%3A%2F%2Fdev.wix.com%2Fapps%2F%7BappId%7D%2Fdev-center-permissions/add) in the app's dashboard and search for the [permission you want to configure](https://dev.wix.com/docs/build-apps/develop-your-app/access/authorization/configure-permissions-for-your-app#step-1--identify-required-permissions).
  string permission_id = 2 [(wix.api.minLength) = 1, (wix.api.maxLength) = 164];
}

message GetAppPermissionResponse {
  // Details of the retrieved app permission.
  AppPermission app_permission = 1;
}

message ListAppPermissionsRequest {
  // App ID. 
  //
  // You can find the app ID on the [Home page](https://manage.wix.com/app-selector?title=Select+an+App&primaryButtonText=Select+Site&actionUrl=https%3A%2F%2Fdev.wix.com%2Fapps%2F%7BappId%7D%home) in the app's dashboard.
  string app_id = 1 [(.wix.api.format) = GUID];
  // Whether to retrieve the most recent permissions information. This decreases performance but ensures that the retrieved information is up-to-date, even immediately after an update. 
  //
  // Default: `false`.
  bool consistent = 2; 
  // Paging configuration.
  wix.common.CursorPaging paging = 3; 
}

message ListAppPermissionsResponse {
  // List of permissions for the specified app.
  repeated AppPermission app_permissions = 1;
  // Paging metadata.
  wix.common.CursorPagingMetadata paging_metadata = 2;
}

message DeleteAppPermissionRequest {
  // App ID. 
  //
  // You can find the app ID on the [Home page](https://manage.wix.com/app-selector?title=Select+an+App&primaryButtonText=Select+Site&actionUrl=https%3A%2F%2Fdev.wix.com%2Fapps%2F%7BappId%7D%home) in the app's dashboard.
  string app_id = 1 [(.wix.api.format) = GUID];
  // Permission ID.
  //
  // To find the permission ID, navigate to the [Permissions page](https://dev.wix.com/app-selector?title=Select+an+App&primaryButtonText=Select+Site&actionUrl=https%3A%2F%2Fdev.wix.com%2Fapps%2F%7BappId%7D%2Fdev-center-permissions/add) in the app's dashboard and search for the [permission you want to configure](https://dev.wix.com/docs/build-apps/develop-your-app/access/authorization/configure-permissions-for-your-app#step-1--identify-required-permissions).
  string permission_id = 2 [(wix.api.minLength) = 1, (wix.api.maxLength) = 164];
}

message DeleteAppPermissionResponse {
}

message PermissionActionDomainEventEntity {
  AppPermission app_permission = 1; // The permission that had an event
  ActionType action_type = 2; // The action which happened
}

enum ActionType {
  APP_PERMISSION_DELETED = 0;
}

message CreateAppPermissionForMigrationRequest {
  AppPermission app_permission = 1; // AppPermission to be created
}

message CreateAppPermissionForMigrationResponse {
  AppPermission app_permission = 1; // The created AppPermission
}
