syntax = "proto3";

import "google/api/annotations.proto";
import "google/protobuf/wrappers.proto";
import "wix/api/annotations.proto";
import "wix/common/query.proto";
import "wix/api/entity.proto";
import "wix/api/sla.proto";
import "wix/api/action_payload.proto";
import "wix/api/callback.proto";
import "wix/api/validations.proto";
import "wix/coreservices/businessschema/v1/entity.proto";
import "wix/coreservices/businessschema/v1/service.proto";

package wix.coreservices.businessschema.v1;

service BusinessSchemaService {

  option (.wix.api.service_entity).message = "wix.coreservices.businessschema.v1.Entity";
  option (.wix.api.service_maturity) = BETA;
  option (.wix.api.service_exposure) = INTERNAL;

  // Validate a given fqdn
  //flynt-disable missing-permission
  rpc ValidateFqdn (ValidateFqdnRequest) returns (ValidateFqdnResponse) {
    option (google.api.http).post = "/v1/{fqdn}/validation";
  }

  // Query business-entities using Wix-Query-Language, this method is private as it is not fully compatible with the
  // wix V2 query language and does not support many of its features. (for instance $or/$and operators should be used at your own risk)
  rpc QueryEntities (QueryEntitiesRequest) returns (QueryEntitiesResponse) {
    option (.wix.api.exposure) = PRIVATE;

    option (google.api.http).post = "/v1/entities";
  }

  // List all business-entities using FQDN search filter
  rpc ListEntities (ListEntitiesRequest) returns (ListEntitiesResponse) {
    option (google.api.http).get = "/v1/entities";
  }

  rpc GetEntity (GetEntityRequest) returns (GetEntityResponse) {
    option (google.api.http).get = "/v1/entities/{fqdn}";
  }

  // List all implementing services using service name search filter
  rpc ListServices (ListServicesRequest) returns (ListServicesResponse) {
    option (google.api.http).get = "/v1/services";
  }

  // List all existing schemas using schema path search filter
  rpc ListSchemas (ListSchemasRequest) returns (ListSchemasResponse) {
    option (google.api.http).get = "/v1/schemas";
  }

  // for internal/debug use only
  rpc TriggerProtoArtifact (TriggerProtoArtifactRequest) returns (TriggerProtoArtifactResponse) {
    option (.wix.api.exposure) = PRIVATE;

    option (google.api.http).post = "/v1/trigger-proto-artifact";
  }

  rpc TriggerFqdn (TriggerFqdnRequest) returns (TriggerFqdnResponse) {
    option (.wix.api.exposure) = PRIVATE;

    option (google.api.http).post = "/v1/trigger-fqdn";
  }

  // Set the primary
  rpc SetPrimaryArtifact (SetPrimaryArtifactRequest) returns (SetPrimaryArtifactResponse) {
    option (.wix.api.exposure) = INTERNAL;
    option (wix.api.required) = "SetPrimaryArtifactRequest.service_name";
  }

  // for internal/debug use only
  rpc RegenerateAll (RegenerateAllRequest) returns (RegenerateAllResponse) {
    option (.wix.api.exposure) = PRIVATE;
    option (.wix.api.sla).response_time_ms = 60000;
  }

  // for internal/debug use only
  rpc DeleteFqdn (DeleteFqdnRequest) returns (DeleteFqdnResponse) {
    option (.wix.api.exposure) = PRIVATE;
  }

  // Event that fires when an API schema is changed (added field, added action, added event, changed mapping, etc..)
  option (.wix.api.event) = {
    event_type: UPDATED
  };

  // Event that fires when an API schema is created
  option (.wix.api.event) = {
    event_type: CREATED
  };

  // Event that fires when an API schema is deprecated (newer version released)
  option (.wix.api.event) = {
    event_type: ACTION
    payload: "wix.coreservices.businessschema.v1.DeprecatedEvent"
  };
}

message ValidateFqdnRequest {
  string fqdn = 1 [(wix.api.minLength) = 0, (wix.api.maxLength) = 1000];
}

message ValidateFqdnResponse {
  bool valid = 1;
}

message QueryEntitiesRequest {
  wix.common.QueryV2 query = 1; // Wix-Query-Language expression
}

message ListEntitiesRequest {
  enum Fieldset {
    UNKNOWN = 0;
    APPENDIX = 1; // include appendix files raw content
  }

  string fqdn_filter = 1; // FQDN string "contains" filter: "wix.ecom"
  repeated Fieldset fieldset = 2; // include extra data
  CursorPaging cursor_paging = 3; //cursor paging data
}

message CursorPaging {
  // The number of items to load
  google.protobuf.Int32Value limit = 1 [(.wix.api.min) = 0];
  // Cursor returned in last query response. Should not be provided on first page request
  google.protobuf.StringValue cursor = 2;
}

message QueryEntitiesResponse {
  map<string, Entity> entities = 1; // A map of all entities. key=fqdn
}

message ListEntitiesResponse {
  // A map of all entities. key=fqdn
  map<string, Entity> entities = 1; 
  // Cursor to use in the next query
  google.protobuf.StringValue cursor = 2;
}

message ListServicesRequest {
  string name_filter = 1; // Service term to match search
}

message ListServicesResponse {
  map<string, Service> services = 1; // A map of all services. key=serviceName
}

message ListSchemasRequest {
  string name_filter = 1; // Schema term to match search
}

message ListSchemasResponse {
  map<string, Entity> entities = 1; // Key = service name
}

message TriggerProtoArtifactRequest {
  string proto_artifact_id = 1;
  repeated string fqdns = 2 [(wix.api.minSize) = 0, (wix.api.maxSize) = 10, (.wix.api.field_deprecated) = {
    target_removal_date: "2023-01-31"
  }];
}

message TriggerProtoArtifactResponse {}


message TriggerFqdnRequest {
  string fqdn = 1 [(wix.api.minLength) = 4, (wix.api.maxLength) = 256];
}

message TriggerFqdnResponse {}

message DeleteFqdnRequest {
  string fqdn = 1 [(wix.api.minLength) = 4, (wix.api.maxLength) = 256];
}

message DeleteFqdnResponse {}

message GetEntityRequest {
  string fqdn = 1 [(wix.api.minLength) = 4, (wix.api.maxLength) = 256];
}

message GetEntityResponse {
  Entity entity = 1;
}

message RegenerateAllRequest {}

message RegenerateAllResponse {}

message DeprecatedEvent {
  option (wix.api.action_payload).custom_slug = "deprecated";
  string fqdn = 1; // Parent entity's FQDN
}

message SetPrimaryArtifactRequest {
  string service_name = 1 [(wix.api.minLength) = 4, (wix.api.maxLength) = 256]; // proto service fqn
  string service_artifact_id = 2 [(wix.api.minLength) = 4, (wix.api.maxLength) = 256];
}

message SetPrimaryArtifactResponse {
  Entity entity = 1;
}

// TODO :: change list endpoints to return array (BREAKING! | v2?)  :~(
// TODO :: errors - missing validation model + structured data  :~(
// TODO :: remove httpInfo. use fields directly in req and res model  :~(
// TODO :: server.destinationPath: is It really needed?  :~(
// TODO :: add @notImplemented on missing fields  :~(
