syntax = "proto3";

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

import "google/protobuf/empty.proto";
import "wix/api/annotations.proto";
import "wix/api/validations.proto";
import "wix/api/entity.proto";
import "wix/api/callback.proto";
import "wix/api/subscription.proto";
import "wix/api/permissions.proto";
import "wix/common/domainevents/domainevents.proto";
import "wix/devcenter/apps/versions/v1/app_version.proto";
import "pricing_model/pricing_model_messages.proto";
import "wix/devcenter/components/overrides/v1/components_override.proto";
import "wix/devcenter/apps/v1/app.proto";

/**
  Manages AppsRuntimes
 */
service AppsRuntimeWriterService {
    option (.wix.api.service_maturity) = ALPHA;
    option (.wix.api.service_exposure) = PRIVATE;
    option (.wix.api.service_entity).message = "wix.devcenter.appsruntimereader.v1.AppRuntimeData";

    /**
      Consume AppVersion DomainEvents in order to populate the Apps Runtime Data
     */
    rpc ConsumeAppVersionsDomainEvents (wix.common.domainevents.DomainEvent) returns (google.protobuf.Empty) {
        option (wix.api.exposure) = PRIVATE;
        option (wix.api.subscription) = {
            entity: "wix.devcenter.apps.versions.v1.AppVersion",
        };
    }

    /*
    consume events where app draft changes
   */
    rpc ConsumeDraftChanged (wix.devcenter.apps.versions.v1.AppDraftChangedEvent) returns (google.protobuf.Empty) {
        option (wix.api.exposure) = PRIVATE;
        option (wix.api.subscription) = {
            topic: "app-draft-changed-delayed"
            segment: USERS
        };
    }

    /**
     Consume ComponentsOverride DomainEvents in order to populate the Apps Runtime Data
    */
    rpc ConsumeComponentsOverrideDomainEvents (wix.common.domainevents.DomainEvent) returns (google.protobuf.Empty) {
        option (wix.api.exposure) = PRIVATE;
        option (wix.api.subscription) = {
            entity: "wix.devcenter.components.overrides.v1.ComponentsOverride",
        };
    }

    /**
     Consume App DomainEvents in order to update secret changes
    */
    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",
        };
    }

    /**
     Consume PricingModelChanged Events in order to populate the Pricing Model Cache
    */
    rpc ConsumePricingModelChangedEvent (com.wixpress.appservice.api.pricing.PricingModelChangedEvent) returns (google.protobuf.Empty) {
        option (wix.api.exposure) = PRIVATE;
        option (wix.api.subscription) = {
            topic: "app-market-pricing-model-changed"
            segment: USERS
        };
    }

    /**
     Consume Plan DomainEvents in order to populate the Pricing Model Cache
    */
    rpc ConsumePlanDomainEvent(wix.common.domainevents.DomainEvent) returns (google.protobuf.Empty) {
        option (.wix.api.exposure) = PRIVATE;
        option (.wix.api.subscription) = {
            entity: "wix.devcenter.plans.v1.Plan"
        };
    }

    //Insert a Draft App Runtime Data entry by App entity
    rpc InsertApp(InsertAppRequest) returns (InsertAppResponse) {
        option (wix.api.exposure) = PRIVATE;
        option (wix.api.permission).name = "DEV_CENTER.APP_RUNTIME_WRITE";
    }

    // BackOffice endpoint to delete app runtime data
    rpc DeleteAppRuntimeData (DeleteAppRuntimeDataRequest) returns (DeleteAppRuntimeDataResponse) {
        option (wix.api.exposure) = PRIVATE;
        option (wix.api.permission).name = "DEV_CENTER.BACK_OFFICE";
    }

    // Creates the runtime override data
    rpc CreateOverrideData (CreateOverrideDataRequest) returns (CreateOverrideDataResponse) {
        option (wix.api.exposure) = PRIVATE;
        option (wix.api.permission).name = "DEV_CENTER.RUNTIME_OVERRIDE_WRITE";
    }

    // Updates the runtime override data
    rpc UpdateOverrideData (UpdateOverrideDataRequest) returns (UpdateOverrideDataResponse) {
        option (wix.api.exposure) = PRIVATE;
        option (wix.api.permission).name = "DEV_CENTER.RUNTIME_OVERRIDE_WRITE";
    }

    // Deletes the runtime override data
    rpc DeleteOverrideData (DeleteOverrideDataRequest) returns (DeleteOverrideDataResponse) {
        option (wix.api.exposure) = PRIVATE;
        option (wix.api.permission).name = "DEV_CENTER.RUNTIME_OVERRIDE_WRITE";
    }

    rpc UpsertProductionAppVersion(UpsertProductionAppVersionRequest) returns (UpsertProductionAppVersionResponse) {
        option (wix.api.exposure) = PRIVATE;
        option (wix.api.permission).name = "DEV_CENTER.RUNTIME_APP_VERSION_WRITE";
    }

    option (wix.api.event) = {
        topic: "app-runtime-data-event"
        segment: USERS
        payload: "wix.devcenter.appsruntimewriter.v1.AppRuntimeDataEvent"
    };
}

message DeleteAppRuntimeDataRequest {
    string app_runtime_data_id = 1;
}

message InsertAppRequest {
    wix.devcenter.apps.v1.App app = 1;//The App entity to create the runtime data from
}

message InsertAppResponse {}

message DeleteAppRuntimeDataResponse {}

message CreateOverrideDataRequest {
    wix.devcenter.components.overrides.v1.ComponentsOverride components_override = 1;
}

message CreateOverrideDataResponse {
    wix.devcenter.components.overrides.v1.ComponentsOverride components_override = 1;
}

message UpdateOverrideDataRequest {
    wix.devcenter.components.overrides.v1.ComponentsOverride components_override = 1;
}

message UpdateOverrideDataResponse {
    wix.devcenter.components.overrides.v1.ComponentsOverride components_override = 1;
}

message DeleteOverrideDataRequest {
    string components_override_id = 1 [(wix.api.maxLength) = 50];
}

message DeleteOverrideDataResponse {
    string components_override_id = 1 [(wix.api.maxLength) = 50];
}

message UpsertProductionAppVersionRequest {
    wix.devcenter.apps.versions.v1.AppVersion app_version = 1; // AppVersion to be upserted
}

message UpsertProductionAppVersionResponse{}
