syntax = "proto3";

package com.wixpress.devcenter.appmarketlisting;

import "google/api/annotations.proto";
import "wix/api/permissions.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/validations.proto";
import "wix/common/query.proto";
import "wix/http/field_mask.proto";
import "com/wixpress/devcenter/appmarketlisting/market_listing.proto";
import "wix/api/subscription.proto";
import "versions_messages.proto";
import "google/protobuf/empty.proto";
import "marketListing_messages.proto";
import "wix/common/paging.proto";
import "wix/common/domainevents/domainevents.proto";

service MarketListingService {
  option (.wix.api.service_maturity) = BETA;
  option (.wix.api.service_exposure) = PUBLIC;
  option (.wix.api.service_entity).message = "com.wixpress.devcenter.appmarketlisting.MarketListing";

  // TODO: add status as required in the request. (do we need status here? because PUBLISHED listing is not created straight away)
  /*
    create a new market listing
   */
  // flynt-disable missing-permission, crud-corresponding-emits-validation
  rpc CreateMarketListing (CreateMarketListingRequest) returns (CreateMarketListingResponse) {
    option (google.api.http).post = "/v1/marketListings";
    option (.wix.api.required) = "CreateMarketListingRequest.market_listing";
    option (.wix.api.required) = "CreateMarketListingRequest.market_listing.app_id";
    option (.wix.api.required) = "CreateMarketListingRequest.market_listing.language_code";
    option (.wix.api.callback) = {
      event_type: CREATED
    };
    option (.wix.api.crud) = {
      method: CREATE
    };
  }

  /*
      retrieve an existing market listing
  */
  // flynt-disable missing-permission
  rpc GetMarketListing (GetMarketListingRequest) returns (GetMarketListingResponse) {
    option (google.api.http).get = "/v1/marketListings/{id}";
    option (.wix.api.required) = "GetMarketListingRequest.id";
    option (.wix.api.crud) = {
      method: GET_ITEM
    };
  }

  // TODO: add status as required in the request.
  /*
    update an existing market listing
  */
  // flynt-disable missing-permission
  rpc UpdateMarketListing (UpdateMarketListingRequest) returns (UpdateMarketListingResponse) {
    option (google.api.http).put = "/v1/marketListings/{market_listing.id}";
    option (.wix.api.required) = "UpdateMarketListingRequest.market_listing";
    option (.wix.api.required) = "UpdateMarketListingRequest.market_listing.id";
    option (.wix.api.required) = "UpdateMarketListingRequest.market_listing.revision";
    option (.wix.api.required) = "UpdateMarketListingRequest.market_listing.app_id";
    option (.wix.api.required) = "UpdateMarketListingRequest.market_listing.language_code";
    option (.wix.api.callback) = {
      event_type: UPDATED
    };
    option (wix.http.infer_fieldmask) = {
      from: "market_listing"
      fieldmask: "mask"
    };
  }

  // consume old listing changed event
  rpc ConsumeOldListingChanged (com.wixpress.appservice.api.OldListingChanged) returns (google.protobuf.Empty) {
    option (wix.api.subscription) = {
      topic: "old-listing-info-changed-event"
      segment: USERS
    };
    option (.wix.api.callback) = {
      event_type: UPDATED
    };
  }

  // consume app version state changed event
  rpc ConsumeAppVersionStateChanged (com.wixpress.appservice.api.AppVersionStateChanged) returns (google.protobuf.Empty) {
    option (wix.api.subscription) = {
      topic: "app-version-state-changed"
      segment: PUBLIC
    };
    option (.wix.api.callback) = {
      event_type: UPDATED
    };
  }

  /*
    query market listings
   */
  // flynt-disable invalid-query-option, missing-permission, rest-path-kebab-case, crud-add-query-annotation
  rpc QueryMarketListing (QueryMarketListingRequest) returns (QueryMarketListingResponse) {
    option (google.api.http).post = "/v1/marketListings/query";
    option (.wix.api.required) = "QueryMarketListingRequest.query";
    option (.wix.api.crud) = {
      method: QUERY
      query_options: {
        query_field: "query"
        items_field: "market_listing"
      }
    };
  }

  // consume plan domain event
  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"
    };
  }

  // consume app domain event

  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"
    };
  }

  // promote version
  rpc Promote(PromoteRequest) returns (PromoteResponse) {
    option (.wix.api.exposure) = PRIVATE;
    option (.wix.api.permission).name = "APP_MARKET.PROMOTE_LISTINGS";
  }

  // compare versions
  // flynt-disable missing-permission
  rpc Compare(CompareRequest) returns (CompareResponse) {
    option (google.api.http).post = "/v1/market-listings/compare";
  }
}


message CompareResponse {
  // if equals
  bool equals = 1;
}


message CompareRequest {
  // the id of the app
  string app_id = 1 [(.wix.api.format) = GUID];

  // which status to compare from
  Status from = 2;

  // which status to compare to
  Status to = 3;
}

message PromoteRequest {
  // the id of the app
  string app_id = 1 [(.wix.api.format) = GUID];

  // which status to promote from
  Status from = 2;

  // which status to promote to
  Status to = 3;
}

message PromoteResponse {
  // id of the app
  string app_id = 1 [(.wix.api.format) = GUID];

  // the new status of the app
  Status status = 2;
}

message CreateMarketListingRequest {
  // the market listing to create
  MarketListing market_listing = 1;
}

message CreateMarketListingResponse {
  // the created market listing
  MarketListing market_listing = 1;
}

// flynt-disable crud-get-item-message-validation
message GetMarketListingRequest {
  // the id of the market listing to get
  string id = 1 [(.wix.api.format) = GUID];
}

message GetMarketListingResponse {
  // the requested market listing
  MarketListing market_listing = 1;
}

message UpdateMarketListingRequest {
  // the update listing to update
  MarketListing market_listing = 1;

  // the mask to apply the update to
  google.protobuf.FieldMask mask = 2;
}

message UpdateMarketListingResponse {
  // the updated market listing
  MarketListing market_listing = 1;
}

//message DeleteMarketListingRequest {
//    string appid = 1 [(.wix.api.format) = GUID];
//    uint64 revision = 2;
//}
//
//message DeleteMarketListingResponse {
//}

message QueryMarketListingRequest {
  // wql query
  .wix.common.QueryV2 query = 1;
}

message QueryMarketListingResponse {
  // the queried market listings
  repeated MarketListing market_listing = 1 [(wix.api.maxSize) = 100];

  // the query metadata
  .wix.common.PagingMetadataV2 metadata = 2;
}
