/*
 * Copyright 2021 Google LLC.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

syntax = "proto2";

package temporian.proto;

// A graph defines a set of processing operations and holds all EventSetNode,
// Feature and Sampling instances.
message Graph {
  repeated Operator operators = 1;

  repeated EventSetNode nodes = 2;

  repeated EventSetNode.Feature features = 3;

  repeated EventSetNode.Sampling samplings = 4;

  repeated IOSignature inputs = 5;

  repeated IOSignature outputs = 6;
}

// The input or output of a graph.
message IOSignature {
    optional string key = 1;
    optional string node_id = 2;
}

// Instantiation of an operator in a graph.
message Operator {
  // Unique identifier of the operator instance.
  optional string id = 1;

  // Key of the operator definition.
  optional string operator_def_key = 2;

  // Input node ids by key.
  repeated EventSetNodeArgument inputs = 3;

  // Output node ids by key.
  repeated EventSetNodeArgument outputs = 4;

  // Constructor attributes.
  repeated Attribute attributes = 5;

  message EventSetNodeArgument {
    // Name of the argument.
    optional string key = 1;

    optional string node_id = 2;
  }

  message Attribute {
    // Name of the attribute.
    optional string key = 1;

    oneof type {
      int64 integer_64 = 2;
      string str = 3;
      double float_64 = 4;
      ListString list_str = 5;
      bool boolean = 6;
      MapStrStr map_str_str = 7;
      ListDType list_dtype = 8;
      bytes bytes_ = 9;
      ListIndexKeys list_index_keys = 10;
    }
    message ListString{
      repeated string values = 1;
    }
    message MapStrStr{
      map<string, string> values = 1;
    }
    message ListDType{
      repeated DType values = 1 [packed = true];
    }
    message ListIndexKeys{
      repeated IndexKey values = 1;

      message IndexKey {
        repeated IndexKeyItem values = 1;

        message IndexKeyItem {
          oneof types {
            bytes bytes_ = 1;
            int64 integer_64 = 2;
          }
        }
      }
    }
  }
}

// Data types
enum DType {
  DTYPE_UNDEFINED = 0;
  DTYPE_FLOAT64 = 1;
  DTYPE_INT64 = 2;
  DTYPE_FLOAT32 = 3;
  DTYPE_INT32 = 4;
  DTYPE_BOOLEAN = 5;
  DTYPE_STRING = 6;
}

// Connections between operators
message EventSetNode {
  // Identifier of the node. Should be unique across all the nodes of the
  // graph.
  optional string id = 1;

  // Identifier of the sampling ref.
  optional string sampling_id = 2;

  // Identifiers of the feature refs.
  repeated string feature_ids = 3;

  optional string name = 4;

  optional string creator_operator_id = 5;

  // Description of the data transiting in the node.
  optional Schema schema = 6;

// A unique feature in the compute graph.
message Feature {
  // Identifier of the feature.
  optional string id = 1;

  // Index of the node id who's operator creator also created this feature.
  optional string creator_operator_id = 2;
}

// A unique sampling in the compute graph.
message Sampling {
  // Identifier of the sampling.
  optional string id = 1;

  // Index of the node id who's operator creator also created this feature.
  optional string creator_operator_id = 2;
}

}

// Definition of the data (feature, index, etc.) in a node.
message Schema {

repeated Feature features = 1;
repeated Index indexes = 2;
optional bool is_unix_timestamp = 3;

message Feature {
  optional string name = 2;
  optional DType dtype = 3;
}

message Index {
  optional string name = 2;
  optional DType dtype = 3;
}
}

// Definition of an operator. No two operators definition can have the same key.
message OperatorDef {
  // Unique identifier of the operator.
  optional string key = 1;

  // Inputs of the operator.
  repeated Input inputs = 2;

  // Outputs of the operator.
  repeated Output outputs = 3;

  // Arguments of the operator.
  repeated Attribute attributes = 4;

  // Whether this operator cannot be serialized.
  optional bool is_serializable = 5 [default = true];

  message Input {
    oneof type {
      // String identifier of the input. Should be unique.
      string key = 1;

      // String prefix to identify multiple inputs. An input should only match
      // one "key_prefix". If set, "is_optional" is ignored.
      string key_prefix = 2;
    }

    // If true, the input is optional.
    optional bool is_optional = 3;
  }

  message Output {
    // String identifier of the output. Should be unique.
    optional string key = 1;
  }

  message Attribute {
    // String identifier of the attribute. Should be unique.
    optional string key = 1;

    // What type of data is expected.
    optional Type type = 2;

    // If true, the attribute is optional.
    optional bool is_optional = 3;

    enum Type {
      UNDEFINED = 0;
      INTEGER_64 = 1;
      STRING = 2;
      LIST_STRING=3;
      FLOAT_64 = 4;
      BOOL = 5;
      ANY = 6;
      MAP_STR_STR = 7;
      DTYPE = 8;
      LIST_DTYPE = 9;
      BYTES = 10;
      LIST_INDEX_KEYS = 11;
      CALLABLE = 12; // Non serializable
    }
  }
}
