syntax = "proto3";

package wix.fedinfra.exportservice.v1;
option java_package = "com.wixpress.fedinfra.exportservice.v1";

import "google/protobuf/wrappers.proto";
import "google/protobuf/struct.proto";
import "google/protobuf/field_mask.proto";
import "google/api/annotations.proto";
import "wix/api/annotations.proto";
import "wix/api/validations.proto";
import "wix/common/query.proto";
import "wix/fedinfra/exportservicespi/v1/export_service_spi.proto";
import "wix/fedinfra/exportservicespi/v1/export_query_v2.proto";
import "wix/api/entity.proto";
import "wix/api/permissions.proto";
import "wix/api/callback.proto";
import "wix/http/field_mask.proto";
import "wix/fedinfra/exportservice/v1/export.proto";
import "wix/fedinfra/exportservice/v1/catalog.proto";
import "wix/common/paging.proto";
import "wix/api/api_errors.proto";

/**
  Manages Exports
 */
service ExportService {
  option (.wix.api.service_maturity) = BETA;
  option (.wix.api.service_exposure) = PUBLIC;
  option (.wix.api.service_entity).message = "wix.fedinfra.exportservice.v1.ExportAsyncJob";

  /**
    Creates a new Export
   */
  rpc CreateExportAsyncJob (CreateExportAsyncJobRequest) returns (CreateExportAsyncJobResponse) {
    option (google.api.http).post = "/v1/export-async-job";
    option (wix.api.permission) = {
      name: "EXPORT_ASYNC_JOB.CREATE",
      type: MANUAL
    };
    option (.wix.api.required) = "CreateExportAsyncJobRequest.method_metadata";
    option (.wix.api.required) = "CreateExportAsyncJobRequest.fields";
    option (.wix.api.required) = "CreateExportAsyncJobRequest.query";
    option (.wix.api.required) = "CreateExportAsyncJobRequest.query.pagingMethod";

    option (wix.api.error) = {
      http_code: NOT_FOUND
      application_code: "method_endpoint_not_found"
    };
  }

  /**
    Get a Export by id
   */
  rpc GetExportAsyncJob (GetExportAsyncJobRequest) returns (GetExportAsyncJobResponse) {
    option (google.api.http).get = "/v1/export-async-job/{job_id}";
    option (wix.api.crud) = {
      method: GET_ITEM
      get_options: {
        id_field: "job_id"
      }
    };
    option (wix.api.permission) = {
      name: "EXPORT_ASYNC_JOB.CREATE",
      type: MANUAL
    };
    option (.wix.api.required) = "GetExportAsyncJobRequest.job_id";

    option (wix.api.error) = {
      http_code: NOT_FOUND
      application_code: "job_not_found"
    };

    option (wix.api.error) = {
      http_code: INTERNAL
      application_code: "get_async_job_internal"
    };

    option (wix.api.error) = {
      http_code: PERMISSION_DENIED
      application_code: "method_endpoint_permission_denied"
    };
  }

  rpc CancelExportAsyncJob (CancelExportAsyncJobRequest) returns (CancelExportAsyncJobResponse) {
    option (wix.api.exposure) = INTERNAL;
    option (wix.api.maturity) = ALPHA;
    option (google.api.http).put = "/v1/export-async-job/{job_id}/cancel";
    option (wix.api.permission) = {
      name: "EXPORT_ASYNC_JOB.CREATE",
      type: MANUAL
    };
    option (.wix.api.required) = "CancelExportAsyncJobRequest.job_id";
  }

  rpc GenerateExportAsyncJobDownloadUrl (GenerateExportAsyncJobDownloadUrlRequest) returns (GenerateExportAsyncJobDownloadUrlResponse) {
    option (wix.api.exposure) = INTERNAL;
    option (wix.api.maturity) = ALPHA;
    option (google.api.http).put = "/v1/export-async-job/{job_id}/generate-download-url";
    option (wix.api.permission) = {
      name: "EXPORT_ASYNC_JOB.CREATE",
      type: MANUAL
    };
    option (.wix.api.required) = "GenerateExportAsyncJobDownloadUrlRequest.job_id";
  }

  rpc QueryVariantsExportSpi (.wix.fedinfra.exportservicespi.v1.QueryRequestLoose) returns (QueryVariantsExportSpiResponse) {
    option (wix.api.exposure) = INTERNAL;
    option (wix.api.maturity) = ALPHA;
    option (.wix.api.required) = "QueryRequestLoose.query.pagingMethod.cursorPaging";
    option (wix.api.permission) = {
      name: "EXPORT_ASYNC_JOB.VIEW",
      type: MANUAL
    };
  }

  rpc QueryProductsExportSpi (.wix.fedinfra.exportservicespi.v1.QueryRequestLoose) returns (QueryProductsExportSpiResponse) {
    option (wix.api.exposure) = INTERNAL;
    option (wix.api.maturity) = ALPHA;
    option (wix.api.permission) = {
      name: "EXPORT_ASYNC_JOB.VIEW",
      type: MANUAL
    };

    option (wix.api.error) = {
      http_code: NOT_FOUND
      application_code: "query_products_test_not_found"
    };

    option (wix.api.error) = {
      http_code: INTERNAL
      application_code: "query_products_test_internal"
    };
  }
}

message CreateExportAsyncJobRequest {
  .wix.fedinfra.exportservicespi.v1.ExportQueryV2 query = 1; // WQL expression
  repeated FieldDescriptor fields = 2; // translated CSV headers
  ArrayFieldDelimiter array_field_delimiter = 6;
  MethodMetadata method_metadata = 3; // The target method to query data from
  MethodSpec method_spec = 4; // The target method to query data from
  .google.protobuf.StringValue save_as = 5 [(.wix.api.maxLength) = 255];
  .google.protobuf.Struct test_params = 10; // Test params
}

message CreateExportAsyncJobRequestCompat {
  .wix.common.QueryV2 query = 1; // WQL expression
  repeated FieldDescriptor fields = 2; // translated CSV headers
  MethodMetadata method_metadata = 3; // The target method to query data from
  MethodSpec method_spec = 4; // The target method to query data from
  .google.protobuf.StringValue save_as = 5 [(.wix.api.maxLength) = 255];
  .google.protobuf.Struct test_params = 10; // Test params
}

message CreateExportAsyncJobResponse {
  ExportAsyncJob job = 1; // The retrieved Exports
}

message GetExportAsyncJobRequest {
  string job_id = 1 [(.wix.api.format) = GUID]; // Id of the Export to retrieve
}

message GetExportAsyncJobResponse {
  ExportAsyncJob job = 1; // The retrieved ExportAsyncJob
}

message CancelExportAsyncJobRequest {
  string job_id = 1 [(.wix.api.format) = GUID]; // Id of the Export to retrieve
}

message CancelExportAsyncJobResponse {
  ExportAsyncJob job = 1; // The retrieved ExportAsyncJob
}

message GenerateExportAsyncJobDownloadUrlRequest {
  string job_id = 1 [(.wix.api.format) = GUID]; // Id of the Export to retrieve
}

message GenerateExportAsyncJobDownloadUrlResponse {
  ExportAsyncJob job = 1; // The retrieved ExportAsyncJob
}
