syntax = "proto3";

package wix.personalize.targeting.v1;
option java_package = "com.wixpress.personalize.targeting.v1";

import "wix/api/validations.proto";
import "wix/api/entity.proto";
import "google/protobuf/wrappers.proto";
import "google/protobuf/struct.proto";

/**
  A Targeting represents a decision tree of conditions.
  It uses to decide for each user if he eligible to a targeting.
  Read more about Targeting
  in this [Backoffice Offer Object](https://dev.wix.com/docs/rest/internal-only/internal-tools/dealer-backoffice/offer/backoffice-offer-object).
 */
message Targeting {
  // flynt-disable missing-extensible
  option (wix.api.entity) = {
    fqdn: "wix.personalize.targeting.v1.targeting"
    appendix_files: {
      type: DOCS_CONF
      path: "appendices/docs"
    }
    segment: USERS
    // The app in dev-center that owns this entity
    app_def_id: "1b0474db-8624-4c90-a853-c624cbcfa118"
  };

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

  /**
    Revision number, which increments by 1 each time the Targeting is updated.
    To prevent conflicting changes, the current revision must be passed when updating the Targeting.
    Ignored when creating a Targeting.
  */
  google.protobuf.Int64Value revision = 2 [(wix.api.readOnly) = true];

  // Targeting Tree - holds all targeting criteria
  TargetingTree targeting_tree = 3;
}

// Entity, holding criteria(s) (rules or filters) for dealer runtime rule engine
// The rule engine decides if targeting eligible to a user or not based on executing these rules
message TargetingTree {
  // optional current targeting filter execution result (may be set in debug response)
  ExecutionResult execution_result = 1;

  // Type of the criteria (criteria filter)
  CriteriaType type = 2;

  // Client use case specific current targeting filter label. For example "v2" for root level targeting of
  // V2 targeting offers. Or "basic.geo" for V2 targeting GEO filter
  google.protobuf.StringValue label = 3 [(wix.api.maxLength) = 100];

  // If `true` this targeting result is NOT(criteria result)
  google.protobuf.BoolValue negate = 4;

  // Specific criteria message, according to CriteriaType.
  // Each criteria including top level criteria is a rule for dealer rule engine
  // to execute and get result (true — request does satisfy the criteria, false — does not).
  //
  // Different criteria types represent different kind of rules.
  // For example UserRoleCriteria checks if user is owner of metasite or not,
  // geo criteria checks user location geo etc.
  oneof criteria {
    option (.wix.api.aligned_with_enum) = {
      field: "type";
      suffix: "criteria";
    };
    // The compound of a user
    CompoundCriteria compound_criteria = 11;
    // A custom filter for targeting
    CustomCriteria custom_criteria = 12;
    // Country of the user
    ListCriteria geo_criteria = 13;
    // Language of the user
    ListCriteria language_criteria = 14;
    // Filter based on "owner" vs "contributor"
    UserRoleCriteria user_role_criteria = 15;
    // User color
    ListCriteria user_colors_criteria = 16;
    // List of user guids
    ListCriteria user_guids_criteria = 17;
    // List of connected domains
    ConnectedDomainCriteria connected_domain_criteria = 18;
    // Premium plan status of the user
    PremiumPlanCriteria premium_plan_criteria = 19;
    // Whether this site has added a mailbox
    MailboxCriteria mailbox_criteria = 20;
    // Whether a specific app is currently installed on this site
    ListCriteria installed_apps_criteria = 21;
    // Sites connected to a family of products such as combo, VIP, or unlimited
    ListCriteria product_groups_criteria = 22;
    // Users eligible for mailbox discounts
    ListCriteria mailbox_sale_criteria = 23;
    // Users who have 1 or more sites that are not connected to a premium plan
    FreeSiteCriteria free_site_criteria = 24;
    // Criteria brand
    BrandsCriteria brand_criteria = 25;
    // Criteria custom params
    CustomParamCriteria custom_param_criteria = 26;
    // List of criteria audiences
    ListCriteria audience_criteria = 27;
    // Whether the default Package Picker tab is "Business" or "Website"
    PremiumLandingTabCriteria premium_landing_tab_criteria = 28;
    // Users who have 1 or more sites that are connected to premium
    PremiumAccountCriteria premium_account_criteria = 29;
    // Eligible for sale
    EligibleForSaleCriteria eligible_for_sale_criteria = 30;
    // External_criteria
    ExternalCriteria external_criteria = 31;
    // SiteType criteria
    SiteTypeCriteria site_type_criteria = 32;
  }
}

// CriteriaType - the type of criteria.
enum CriteriaType {
  // Unknown criteria type.
  UNKNOWN_CRITERIA_TYPE = 0;
  // Compound criteria type - contain criteria array in children property.
  COMPOUND = 1;
  // Custom criteria type - Used for comparing user/site BI Profile values.
  CUSTOM = 2;
  // Geo criteria type - Used for geo targeting.
  GEO = 3;
  // User language criteria type - Used for language targeting.
  LANGUAGE = 4;
  // User role criteria type - Used for role targeting.
  USER_ROLE = 5;
  USER_COLORS = 6;
  USER_GUIDS = 7;
  CONNECTED_DOMAIN = 8;
  PREMIUM_PLAN = 9;
  MAILBOX = 10;
  INSTALLED_APPS = 11;
  PRODUCT_GROUPS = 12;
  MAILBOX_SALE = 13;
  FREE_SITE = 14;
  BRAND = 15;
  CUSTOM_PARAM = 16;
  AUDIENCE = 17;
  PREMIUM_LANDING_TAB = 18;
  PREMIUM_ACCOUNT = 19;
  ELIGIBLE_FOR_SALE = 20;
  EXTERNAL = 21;
  SITE_TYPE = 22;
}

// Generic message for debug execution results.
// Used for specific targeting criteria
message ExecutionResult {
  // if current criteria is applicable/eligible/valid
  bool success = 1;
  // if set - it's JSON with either string, date utc string, boolean, array or object
  // depends on specific filter/criteria the result is calculated
  google.protobuf.StringValue current_value = 2 [(wix.api.maxLength) = 100];
}

// Aggregate criteria for AND/OR tree like structures
message CompoundCriteria {
  enum Operator {
    UNKNOWN_OPERATOR = 0;
    OR = 1;
    AND = 2;
  }
  // Operator
  Operator operator = 1;
  // List of targeting
  repeated TargetingTree children = 2 [(.wix.api.maxSize) = 10000];
}

// Criteria for targeting rules based on profile service data
message CustomCriteria {
  enum CustomDataSource {
    UNKNOWN_CUSTOM_DATA_SOURCE = 0;
    USER_PROFILE = 1;
    SITE_PROFILE = 2;
    ACCOUNT_PROFILE = 3;
  }

  // BI profile filter operator
  message CustomOperator {
    enum FieldType {
      UNKNOWN_FIELD_TYPE = 0;
      NUMBER = 1;
      STRING = 2;
      LIST = 3;
      TIMESTAMP = 4;
      BOOLEAN = 5;
    }

    enum Comparator {
      UNKNOWN_COMPARATOR = 0;
      EQUAL = 1;
      CONTAINED_IN = 2;
      CONTAINS_SOME = 3;
      CONTAINS_NONE = 4;
      NOT_CONTAINS = 5;
      NOT_CONTAINED_IN = 6;
      BE_NOTHING = 7;
      EXISTS = 8;
      MORE_THAN_DAYS = 9;
      WITHIN_DAYS = 10;
      LESS = 11;
      LESS_EQUAL = 12;
      GREATER = 13;
      GREATER_EQUAL = 14;
      SIZE_LESS_EQUAL = 15;
      SIZE_GREATER_EQUAL = 16;
      NOT_EQUAL = 17;
    }

    // BI field data type
    FieldType field_type = 1;

    // Expected value to be found in BI profile
    google.protobuf.StringValue expected_value = 2 [(wix.api.maxLength) = 2000];

    // Comparator to apply to value (i.e. if BI value EQUALS, LESS, GREATER than expected value etc.)
    Comparator comparator = 3;
  }

  // Id field - string
  string field_id = 1 [(wix.api.maxLength) = 100];
  // Data_source
  CustomDataSource data_source = 2;
  // Operator
  CustomOperator operator = 3;
  // List of restrictions
  CustomOperator list_restriction = 4;
}

// Generic filter for list of included/excluded values
message ListCriteria {
  // List of criteria values
  repeated string values = 1 [(.wix.api.maxSize) = 1000, (wix.api.maxLength) = 100];
  // Is included - boolean
  bool included = 2;
}

// Filter for targeting based on premium plan eligibility
message PremiumPlanCriteria {
  enum Plan {
    UNKNOWN_PLAN = 0;
    PREMIUM = 1;
    NONE = 2;
  }
  // List of products number
  repeated int32 products = 1 [(.wix.api.maxSize) = 10000];
  // The user plan
  Plan plan = 2;
}

// Filter for MetaSite ownership rules
message UserRoleCriteria {
  enum Value {
    UNKNOWN_VALUE = 0;
    OWNER = 1;
    NOT_OWNER = 2;
  }
  // User role criteria
  Value value = 1;
}

// Filter for Premium Landing Tab types
message PremiumLandingTabCriteria {
  enum Value {
    UNKNOWN_VALUE = 0;
    WEBSITE = 1;
    BUSINESS = 2;
  }
  // Landing tab criteria
  Value value = 1;
}

// Filter for MetaSite domain validations
message ConnectedDomainCriteria {
  enum Value {
    UNKNOWN_VALUE = 0;
    CONNECTED = 1;
    NOT_CONNECTED = 2;
  }
  // Connected domain criteria
  Value value = 1;
}

// Filter for MetaSite mailbox validations
message MailboxCriteria {
  enum Value {
    UNKNOWN_VALUE = 0;
    HAS_MAILBOX = 1;
    NO_MAILBOX = 2;
  }
  // Mailbox criteria
  Value value = 1;
}

// Site level filter - is free site or not
message FreeSiteCriteria {
  enum Value {
    UNKNOWN_VALUE = 0;
    HAS_FREE = 1;
    NO_FREE = 2;
  }
  // FreeSite criteria
  Value value = 1;
}

// Filter for list of brands
message BrandsCriteria {
  enum Brand {
    UNKNOWN_BRAND = 0;
    WIX = 1;
    EDITORX = 2;
    DAYFUL = 3;
    STUDIO = 4;
    WIXEL = 5;
    RISE = 6;
    WIXVIBE = 7;
  }
  // List of brands
  repeated Brand values = 1 [(.wix.api.maxSize) = 10];
  // Brand included status
  bool included = 2;
}

// Filter for list of site types.
message SiteTypeCriteria {
  // SiteType options
  enum SiteType {
    UNKNOWN_SITE_TYPE = 0;
    REGULAR = 1;
    HEADLESS = 2;
  }
  // Array of siteType options
  repeated SiteType values = 1 [(.wix.api.minSize) = 1, (.wix.api.maxSize) = 10];
  // isIncluded bool
  bool included = 2;
}

// Filter for list of custom params - comes from the request.
message CustomParamCriteria {
  enum CustomParamOperator {
    UNKNOWN_CUSTOM_PARAM_OPERATOR = 0;
    EQUALS = 1;
    CONTAINS_ALL = 2;
    CONTAINS_ANY = 3;
    EXISTS = 4;
  }
  // Custom param operator
  CustomParamOperator operator = 1;
  // Key of criteria
  google.protobuf.StringValue key = 2 [(wix.api.maxLength) = 100];
  // List of criteria values
  repeated google.protobuf.StringValue values = 3 [(.wix.api.maxSize) = 100, (wix.api.maxLength) = 100];
}

message PremiumAccountCriteria {
  enum Value {
    UNKNOWN_VALUE = 0;
    IS_PREMIUM = 1;
    IS_NOT_PREMIUM = 2;
  }
  // Premium account criteria
  Value value = 1;
}

// Filter for external criteria
// implemented by external services
message ExternalCriteria {
  enum ConditionType {
    UNKNOWN_CONDITION_TYPE = 0;
    EQUAL = 1;
    GREATER_THAN = 2;
    SMALLER_THAN = 3;
  }
  // Artifact name
  google.protobuf.StringValue artifact_name = 1 [(wix.api.maxLength) = 100];
  // External criteria - condition type
  ConditionType condition_type = 2;
  // External criteria - expected value
  google.protobuf.Value expected_value = 3; // This should be validated against ResponseType and ConditionType
  // External criteria - additional params
  google.protobuf.Struct additional_params = 4;
}

// Filter for targeting based on product groups
message EligibleForSaleCriteria {
  enum Value {
    UNKNOWN_VALUE = 0;
    NONE = 1;
    SEASONAL = 2;
    PERSONAL = 3;
    CUSTOM = 4;
  }
  // EligibleForSale criteria value
  Value value = 1;
  // CustomSale value
  google.protobuf.StringValue custom_sale = 2 [(wix.api.maxLength) = 100];
  // Product id - guid
  string product_type_id = 3 [(.wix.api.format) = GUID];
}
;
