syntax = "proto3";

package wix.os.v1;
option java_package = "com.wixpress.os.v1";

import "google/protobuf/field_mask.proto";
import "google/api/annotations.proto";
import "wix/api/annotations.proto";
import "wix/api/validations.proto";
import "wix/api/entity.proto";
import "wix/api/permissions.proto";
import "wix/api/callback.proto";
import "wix/http/field_mask.proto";
import "wix/os/v1/tag.proto";
import "wix/os/v1/tag_exposure.proto";
import "wix/api/errors.proto";
import "wix/api/api_errors.proto";
import "wix/os/v1/upstream/wix/common/query.proto";
import "wix/os/v1/upstream/wix/common/paging.proto";

/**
  Manages Tags
 */
service TagsService {
  option (wix.api.service_maturity) = GA;
  option (wix.api.service_exposure) = PUBLIC;
  option (wix.api.service_entity).message = "wix.os.v1.Tag";

  /**
    Creates a tag.
   */
  //flynt-disable removing-required-annotation-must-sdk-signature
  rpc CreateTag (CreateTagRequest) returns (CreateTagResponse) {
    option (google.api.http).post = "/v1/tags";
    option (wix.api.permission).name = "TAGS.TAG_CREATE";
    option (wix.api.required) = "CreateTagRequest.tag";
    option (wix.api.required) = "CreateTagRequest.tag.fqdn";
    option (wix.api.required) = "CreateTagRequest.tag.name";

    option (wix.api.crud) = {
      method: CREATE
      create_options: {
        item_field: "tag"
      }
    };

    option (wix.api.callback) = {
      event_type: CREATED
    };

    // Couldn't find the FQDN.
    option (.wix.api.error) = {
      http_code: NOT_FOUND
      application_code: "FQDN_NOT_FOUND"
    };

    // There is already a tag with this name.
    option (.wix.api.error) = {
      http_code: ALREADY_EXISTS
      application_code: "TAG_NAME_ALREADY_EXISTS"
    };

    // Number of tags exceeds the server's limit.
    option (.wix.api.error) = {
      http_code: RESOURCE_EXHAUSTED
      application_code: "TAGS_REACHED_LIMIT"
    };

    // Failed to generate a unique tag ID after multiple attempts.
    option (.wix.api.error) = {
      http_code: INTERNAL
      application_code: "CREATE_ID_EXHAUSTED_RETRY_ATTEMPTS"
    };

    // The specified `exposure` value isn't supported for this FQDN.
    option (.wix.api.error) = {
      http_code: INVALID_ARGUMENT
      application_code: "EXPOSURE_NOT_SUPPORTED"
    };
  }

  /**
    Retrieves a tag.
   */
  rpc GetTag (GetTagRequest) returns (GetTagResponse) {
    option (google.api.http).get = "/v1/tags/{tag_id}";
    option (wix.api.permission).name = "TAGS.TAG_READ";
    option (wix.api.required) = "GetTagRequest.tag_id";

    option (wix.api.crud) = {
      method: GET_ITEM
      get_options: {
        id_field: "tag_id"
      }
    };
  }

  /**
  Updates a tag.
   */
  rpc UpdateTag (UpdateTagRequest) returns (UpdateTagResponse) {
    option (google.api.http).patch = "/v1/tags/{tag.id}";
    option (wix.api.permission).name = "TAGS.TAG_UPDATE";
    option (wix.api.required) = "UpdateTagRequest.tag";
    option (wix.api.required) = "UpdateTagRequest.tag.id";
    option (wix.api.required) = "UpdateTagRequest.tag.revision";
    option (wix.api.required) = "UpdateTagRequest.tag.name";

    option (wix.api.callback) = {
      event_type: UPDATED
    };

    option (wix.api.crud) = {
      method: UPDATE
      update_options: {
        item_field: "tag"
      }
    };
  }

  /**
    Deletes a tag.
   */
  rpc DeleteTag (DeleteTagRequest) returns (DeleteTagResponse) {
    option (google.api.http).delete = "/v1/tags/{tag_id}";
    option (wix.api.permission).name = "TAGS.TAG_DELETE";
    option (wix.api.required) = "DeleteTagRequest.tag_id";

    option (wix.api.callback) = {
      event_type: DELETED
    };

    option (wix.api.crud) = {
      method: DELETE
      delete_options: {
        id_field: "tag_id"
      }
    };
  }

  /**
    Retrieves a list of tags.
   */
  rpc ListTags (ListTagsRequest) returns (ListTagsResponse) {
    option (google.api.http).get = "/v1/tags";
    option (wix.api.permission).name = "TAGS.TAG_READ";
    option (wix.api.required) = "ListTagsRequest.fqdn";

    option (wix.api.crud) = {
      method: LIST
      list_options: {
        items_field: "tags"
      }
    };
  }

  /**
  Retrieves a list of tags.
 */
  rpc QueryTags (QueryTagsRequest) returns (QueryTagsResponse) {
    option (google.api.http) = {
      post: "/v1/tags/query"
      additional_bindings : {
        get: "/v1/tags/query";
      }
    };
    option (wix.api.permission).name = "TAGS.TAG_READ";
    option (wix.api.required) = "QueryTagsRequest.fqdn";
    option (wix.api.required) = "QueryTagsRequest.query";

    option (.wix.api.crud) = {
      method: QUERY
      query_options: {
        items_field: 'tags'
      }
    };
  }

  /**
    Creates a tag with a custom createdDate for migration purposes.
   */
  rpc MigrateTag (MigrateTagRequest) returns (MigrateTagResponse) {
    option (wix.api.permission).name = "TAGS.TAG_MIGRATE";
    option (wix.api.exposure) = PRIVATE;
    option (wix.api.required) = "MigrateTagRequest.tag";
    option (wix.api.required) = "MigrateTagRequest.tag.fqdn";
    option (wix.api.required) = "MigrateTagRequest.tag.name";
    option (wix.api.required) = "MigrateTagRequest.tag.created_date";
  }

}

message CreateTagRequest {
  // The tag to be created.
  Tag tag = 1;
}

message CreateTagResponse {
  // The created tag.
  Tag tag = 1;
}

message GetTagRequest {
  // The ID of the tag to retrieve.
  string tag_id = 1 [(wix.api.maxLength) = 5];
}

message GetTagResponse {
  // The requested tag.
  Tag tag = 1;
}

message UpdateTagRequest {
  // The tag to be updated, which may be partial.
  Tag tag = 1;
}

message UpdateTagResponse {
  // The updated tag.
  Tag tag = 1;
}

message DeleteTagRequest {
  // ID of the tag to delete.
  string tag_id = 1 [(wix.api.maxLength) = 5];
}

message DeleteTagResponse {
}

message ListTagsRequest {
  // The Fully Qualified Domain Name (FQDN) to which the tags are associated. Currently supports only eCommerce Orders: `wix.ecom.v1.order`.
  string fqdn = 1 [(wix.api.maxLength) = 255];

  // The tag exposure.
  TagExposure exposure = 2 [(wix.api.field_exposure) = INTERNAL];
}

message ListTagsResponse {
  // A list of tags.
  repeated Tag tags = 1;
}

message QueryTagsRequest {
  // The Fully Qualified Domain Name (FQDN) to which the tags are associated. Currently supports only eCommerce Orders: `wix.ecom.v1.order`.
  string fqdn = 1 [(wix.api.maxLength) = 255];

  // The tag exposure.
  TagExposure exposure = 2 [(wix.api.field_exposure) = INTERNAL];

  // WQL expression.
  wix.os.v1.upstream.wix.common.CursorQuery query = 3;
}

message QueryTagsResponse {
  // A list of tags.
  repeated Tag tags = 1;

  // Paging metadata
  wix.os.v1.upstream.wix.common.CursorPagingMetadata paging_metadata = 2;
}

message MigrateTagRequest {
  // The tag to be migrated with custom createdDate.
  Tag tag = 1;
}

message MigrateTagResponse {
  // The migrated tag.
  Tag tag = 1;
}
