// flynt-disable required-annotation-breaking-change
syntax = "proto3";

package wix.stores.catalog.provision.v1;
option java_package = "com.wixpress.stores.catalog.provision.v1";

import "google/api/annotations.proto";
import "wix/api/annotations.proto";
import "wix/api/entity.proto";
import "wix/api/api_errors.proto";
import "wix/api/permissions.proto";
import "wix/api/validations.proto";
import "wix/api/subscription.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/wrappers.proto";
import "com/wixpress/metasite/event/api/special.proto";
import "wix/stores/catalog/provision/v1/provision.proto";
import "wix/api/callback.proto";


/**
  Manages Provisioning of Stores. Should not be interacted with unless managing the installation of the Stores app.
  Outside the install flow, provision status should be checked by conducting a migration experiment.
 */
service StoresProvisionService {
  option (wix.api.service_maturity) = GA;
  option (wix.api.service_exposure) = PUBLIC;
  option (wix.api.service_entity).message = "wix.stores.catalog.provision.v1.Provision";

  // Retrieves the version of Stores Catalog installed on a site.
  rpc GetCatalogVersion (GetCatalogVersionRequest) returns (GetCatalogVersionResponse) {
    option (wix.api.permission).name = "WIX_STORES.CATALOG_VERSION_READ";
    option (google.api.http) = {
      get: "/v3/provision/version"
    };
  }

    // flynt-disable deprecate-method-before-delete

  // Asynchronously Installs BASS App dependency
  rpc InstallBassAppDependency (InstallBassAppDependencyRequest) returns (InstallBassAppDependencyResponse) {
    option (wix.api.permission).name = "CATALOG.PROVISION_WRITE";
    option (.wix.api.exposure) = PRIVATE;
  }

  // Responds to MetaSite Special Events (sent when an app is installed)
  rpc OnStoresAppsInstalled (com.wixpress.metasite.event.api.MetaSiteSpecialEvent) returns (google.protobuf.Empty) {
    option (.wix.api.subscription) = {
      topic: "meta-site-manager-special-events"
    };
    option (.wix.api.exposure) = PRIVATE;
  }

  option (.wix.api.event) = {
    topic: "stores-default-delivery-profile-setup"
    payload: "wix.stores.catalog.provision.v1.DefaultDeliveryProfileSetup"
    segment: USERS
    exposure: PRIVATE
  };

  rpc InstallStores(InstallStoresRequest) returns (InstallStoresResponse) {
    option (wix.api.permission).name = "CATALOG.PROVISION_WRITE";
    option (.wix.api.exposure) = PRIVATE;
    option (.wix.api.emits) = {
      event_type: ACTION
      action_message_fqn: "wix.stores.catalog.provision.v1.StoresV3CatalogProvisioned"
    };
    option (.wix.api.required) = "InstallStoresRequest.store_id";
    option (.wix.api.required) = "InstallStoresRequest.meta_site_id";

    // Ecom App is not install on the v1 site
    option (.wix.api.error) = {
      http_code: FAILED_PRECONDITION
      application_code: "REQUIRED_APP_NOT_INSTALLED"
    };
  }
  
  // publish an action event for provision to backfill BI
  rpc BackfillV3Site(BackfillV3SiteRequest) returns (BackfillV3SiteResponse) {
    option (wix.api.permission).name = "CATALOG.PROVISION_WRITE";
    option (wix.api.exposure) = INTERNAL;
    option (.wix.api.emits) = {
      event_type: ACTION
      action_message_fqn: "wix.stores.catalog.provision.v1.StoresV3CatalogProvisioned"
    };
    option (.wix.api.required) = "BackfillV3SiteRequest.meta_site_id";
    option (.wix.api.error) = {
      http_code: FAILED_PRECONDITION
      application_code: "REQUIRED_APP_NOT_INSTALLED"
    };
    option (.wix.api.error) = {
      http_code: FAILED_PRECONDITION
      application_code: "BACKFILLING_V3_FOR_V1"
    };
  }
}

message BackfillV3SiteRequest {
  // meta_site_id
  string meta_site_id = 1 [(wix.api.format) = GUID];
}

message BackfillV3SiteResponse {}

message GetCatalogVersionRequest {}

message GetCatalogVersionResponse {
  // The version of Stores Catalog installed on a site.
  Version catalog_version = 1;
}

message DefaultDeliveryProfileSetup {
  // metasite id of site
  google.protobuf.StringValue meta_site_id = 1 [(wix.api.format) = GUID];
}

message InstallBassAppDependencyRequest {}

message InstallBassAppDependencyResponse {}

message InstallStoresRequest {
  reserved 5;
  // Original StoreId
  google.protobuf.StringValue origin_store_id = 1 [(.wix.api.format) = GUID];
  // Original MetaSiteId
  google.protobuf.StringValue origin_meta_site_id = 2 [(.wix.api.format) = GUID];
  // StoreId
  string store_id = 3 [(.wix.api.format) = GUID];
  // MetaSiteId
  string meta_site_id = 4 [(.wix.api.format) = GUID];
}
message InstallStoresResponse {
}
