syntax = "proto3";

package wix.devcenter.apps.dependencies.v2;
option java_package = "com.wixpress.devcenter.apps.dependencies.v2";

import "wix/api/validations.proto";
import "wix/api/entity.proto";
import "wix/api/annotations.proto";
import "google/protobuf/wrappers.proto";
import "google/protobuf/timestamp.proto";
import "wix/common/data/dataextensions/extended-fields.proto";
import "wix/devcenter/apps/dependencies/v2/upstream/wix/common/tags.proto";
import "wix/api/action_payload.proto";

/**
  A AppDependency defines dependency relationships between apps in the Wix ecosystem.
  It allows you to specify which apps are required for a primary app to function correctly.
  You can use it to manage app dependencies, ensuring that all necessary apps are installed and configured
  before the primary app is used.
  Terminology: If App A depends on App B, we call App A the "dependent" and App B the "dependency".
  Read more about AppDependencies in this [article](<LINK_TO_KB_ARTICLE>).
 */
message AppDependency {
  option (.wix.api.entity) = {
    fqdn: "wix.devcenter.apps.v2.app_dependency"
    segment: USERS
    taggable: {}
    domain_events: {
      deleted_include_entity: true,
      event_sequence_number: true,
      updated_include_modified_fields: true
    }
    app_def_id: "1e866388-a268-4053-a6ea-e7f61cb063ea"
    appendix_files: {
      type: DOCS_CONF,
      path: "appendices/docs"
    }
    query_options: {
      wql: {
        pattern: {
          operator: ALL_APPLICABLE_OPERATORS
          field: "id"
          field: "tags"
          field: "app_id"
          sort: BOTH
        }
      }
    }
    domain_events_read_permission: "DEV_CENTER.APP_DEPENDENCIES_READ"
    events_exposure: PRIVATE
    events_maturity: ALPHA
  };
  option (.wix.api.domain_event) = {
    event_type: CREATED
  };
  option (.wix.api.domain_event) = {
    event_type: UPDATED
  };

  option (.wix.api.domain_event) = {
    event_type: ACTION
    action_message_fqn: "wix.devcenter.apps.dependencies.v2.TagsModified"
  };

  // AppDependency ID.
  google.protobuf.StringValue id = 1 [(.wix.api.format) = GUID, (.wix.api.readOnly) = true];

  // The version of AppDependency entity.
  int64 version = 2 [(.wix.api.readOnly) = true];

  // The ID of the dependent app for which we specify its dependencies.
  string app_id = 3 [(.wix.api.format) = GUID];

  // The dependencies of the dependent app.
  DependencyNode dependencies = 4;

  // Revision number, which increments by 1 each time the AppDependency is updated.
  google.protobuf.Int64Value revision = 5 [(.wix.api.readOnly) = true];

  // Date and time the AppDependency was created.
  google.protobuf.Timestamp created_date = 100 [(.wix.api.readOnly) = true];

  // Date and time the AppDependency was last updated.
  google.protobuf.Timestamp updated_date = 101 [(.wix.api.readOnly) = true];

  // Tags
  wix.devcenter.apps.dependencies.v2.upstream.wix.common.Tags tags = 2001;
}

message DependencyNode {
  // The type of the dependency node.
  DependencyNodeType node_type = 1;
  // The options for the dependency node.
  oneof options {
    option (.wix.api.aligned_with_enum).field = "node_type";
    // A single app dependency. Leaf node.
    SingleDependency single_dependency_options = 2;
    // Dependencies combined with a logical operator (AND/OR). Inner node.
    DependenciesLogicTree dependencies_logic_tree_options = 3;
    // Resolved dependencies
    ResolvedSingleDependency resolved_single_dependency_options = 4 [(.wix.api.readOnly) = true];
  }
}

message SingleDependency {
  // The ID of the app dependency.
  string app_id = 1 [(.wix.api.format) = GUID];
}

message DependenciesLogicTree {
  // The logical relation between the dependencies.
  Operator operator = 1;
  // The sub-dependencies of this node.
  repeated DependencyNode dependencies = 2 [(.wix.api.minSize) = 2, (.wix.api.maxSize) = 30];
}

message ResolvedSingleDependency {
  // The ID of the app dependency.
  string app_id = 1 [(.wix.api.format) = GUID];

  // The sub-dependencies of this app.
  DependencyNode dependencies = 2;
}

enum Operator {
  UNKNOWN_OPERATOR = 0 [(.wix.api.enum_maturity) = NOT_IMPLEMENTED];
  AND = 1; // All dependencies are required.
  OR = 2; // At least one dependency is required.
}

enum DependencyNodeType {
  UNKNOWN_DEPENDENCY_NODE_TYPE = 0 [(.wix.api.enum_maturity) = NOT_IMPLEMENTED];
  SINGLE_DEPENDENCY = 1;
  DEPENDENCIES_LOGIC_TREE = 2;
  RESOLVED_SINGLE_DEPENDENCY = 3; // A resolved single dependency, which is a leaf node with resolved dependencies.
}

message TagsModified {
  option (wix.api.action_payload).custom_slug = "tags_modified";
  // Updated AppDependency.
  AppDependency app_dependency = 1;

  // Tags that were assigned to the AppDependency.
  wix.devcenter.apps.dependencies.v2.upstream.wix.common.Tags assigned_tags = 2;

  // Tags that were unassigned from the AppDependency.
  wix.devcenter.apps.dependencies.v2.upstream.wix.common.Tags unassigned_tags = 3;
}
