syntax = "proto3";

package wix.infra.asyncjobs.v1;
option java_package = "com.wixpress.infra.asyncinfra.asyncjobservice.v1";

import "google/protobuf/timestamp.proto";
import "google/protobuf/struct.proto";
import "google/protobuf/wrappers.proto";

import "wix/api/entity.proto";
import "wix/api/validations.proto";
import "wix/api/annotations.proto";


message AsyncJob {
  option (wix.api.entity) = {
    fqdn: "wix.infra.asyncjobs.v1.async_job",
    segment: USERS
    appendix_files: {
      type: DOCS_CONF
      path: "appendices/docs"
    }
    appendix_files: {
      type: AUTO_VELO
      path: "appendices/velo"
    }
  };

  // Job ID.
  string id = 1 [(wix.api.format) = GUID, (wix.api.readOnly) = true];

  // Optional job metadata, used to store any parameters used during job execution, user-defined statuses, etc.
  google.protobuf.Struct metadata = 2;

  // Current job execution status.
  Status status = 3;

  // Current counters.
  Counters counters = 5 [deprecated = true, (wix.api.field_deprecated) = {
    replaced_by: "wix.infra.asyncjobs.v1.AsyncJob.counts"
    target_removal_date: "2024-12-12",
  }];
  // Date and time the job was created in [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601#combined_date_and_time_representations) format.
  google.protobuf.Timestamp created_date = 6 [(wix.api.readOnly) = true];
  // Date and time the job was last updated in [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601#combined_date_and_time_representations) format.
  google.protobuf.Timestamp updated_date = 7 [(wix.api.readOnly) = true];

  // Job owner.
  Owner owner = 8 [(wix.api.readOnly) = true, deprecated = true, (wix.api.field_deprecated) = {
    replaced_by: "wix.infra.asyncjobs.v1.AsyncJob.created_by"
    target_removal_date: "2024-12-12",
  }];

  // Job creator.
  CreatedBy created_by = 9 [(wix.api.readOnly) = true];

  // Current execution counts.
  Counts counts = 10;

  enum Status {
    UNKNOWN = 0;
    // Job is created, but hasn't started yet.
    INITIALIZED = 1;
    // Job has started and is in progress.
    PROCESSING = 2;
    // Job is finished.
    FINISHED = 3;
    // Job has failed.
    FAILED = 4;
  }

  message Counters {
    // Optional count of the dataset size, as specified during job creation. Can be used for progress bars, etc.
    google.protobuf.Int32Value items_to_process = 1;

    // Amount of items that were successfully processed.
    int32 items_succeeded = 2;

    // Amount of items whose processing failed.
    int32 items_failed = 3;

    // Mapping between an error code and the number of failures associated with it.
    map<string, int32> error_count_by_code = 4;
  }

  message Counts {
    // Optional count of the dataset size, specified during job creation. Can be used for progress bars, etc.
    google.protobuf.Int32Value total = 1;

    // Amount of items that were successfully processed.
    int32 success_count = 2;

    // Amount of items whose processing failed.
    int32 fail_count = 3;

    // Mapping between an error code and the number of failures associated with it.
    map<string, int32> error_by_code_count = 4;
  }

  message Owner {
    oneof id {
      // Visitor ID - when the job is owned by a site visitor that was **not** logged in.
      string anonymous_visitor_id = 2 [(.wix.api.format) = GUID];
      // Member ID - When the job is owned by a by a **logged in** site visitor.
      string member_id = 3 [(.wix.api.format) = GUID, (.wix.api.referenced_entity) = "wix.members.v1.member"];
      // User ID - When the job is owned by a Wix user.
      string wix_user_id = 4 [(.wix.api.format) = GUID];
      // App ID - When the job is owned by an external app or app by Wix.
      string app_id = 5 [(.wix.api.format) = GUID];
      // Service ID.
      string service_id = 6 [(.wix.api.format) = GUID];
    }
  }

  message CreatedBy {
    enum Type {
      UNKNOWN_TYPE = 0;
      ANONYMOUS_VISITOR_ID = 1; // Job was created by a site visitor that was **not** logged in.
      MEMBER_ID = 2; // Job was created by a **logged in** site visitor.
      WIX_USER_ID = 3;  // Job was created by a  Wix user.
      APP_ID = 4;  // Job was created by an external app or app by Wix.
    }

    message AnonymousVisitorIdOptions {
      // Visitor ID - When the job is owned by a site visitor that was **not** logged in.
      string anonymous_visitor_id = 1 [(.wix.api.format) = GUID];
    }
    message MemberIdOptions {
      // Member ID - When the job is owned by a by a **logged in** site visitor.
      string member_id = 1 [(.wix.api.format) = GUID, (.wix.api.referenced_entity) = "wix.members.v1.member"];
    }
    message WixUserIdOptions {
      // User ID - When the job is owned by a site owner or contributor.
      string wix_user_id = 4 [(.wix.api.format) = GUID];
    }
    message AppIdOptions {
      // App ID - When the job is owned by an external app. or app by Wix.
      string app_id = 5 [(.wix.api.format) = GUID];
    }

    // The type of the creator.
    Type type = 1;
    oneof options {
      option (.wix.api.aligned_with_enum).field = "type";
      // Visitor ID - when the job is created by a site visitor that was **not** logged in.
      AnonymousVisitorIdOptions anonymous_visitor_id_options = 2;
      // Member ID - When the job is created by a by a **logged in** site visitor.
      MemberIdOptions member_id_options = 3;
      // User ID - When the job is created by a Wix user.
      WixUserIdOptions wix_user_id_options = 4;
      // App ID - When the job is created by an external app or app by Wix.
      AppIdOptions app_id_options = 5;
    }
  }

}
