syntax = "proto3";

import "gogo/protobuf/gogoproto/gogo.proto";

package mesg.types;
option go_package = "github.com/mesg-foundation/engine/service";

option (gogoproto.goproto_getters_all) = false;

// Service represents the service's type.
message Service {

  // Events are emitted by the service whenever the service wants.
  message Event {
    // Event's key.
    string key = 4 [
      (gogoproto.moretags) = 'hash:"name:4" validate:"printascii"'
    ];

    // Event's name.
    string name = 1 [
      (gogoproto.moretags) = 'hash:"name:1" validate:"printascii"'
    ];

    // Event's description.
    string description = 2 [
      (gogoproto.moretags) = 'hash:"name:2" validate:"printascii"'
    ];

    // List of data of this event.
    repeated Parameter data = 3 [
      (gogoproto.moretags) = 'hash:"name:3" validate:"dive,required"'
    ];
  }

  // Task is a function that requires inputs and returns output.
  message Task {
    // Task's key.
    string key = 8 [
      (gogoproto.moretags) = 'hash:"name:8" validate:"printascii"'
    ];

    // Task's name.
    string name = 1 [
      (gogoproto.moretags) = 'hash:"name:1" validate:"printascii"'
    ];

    // Task's description.
    string description = 2 [
      (gogoproto.moretags) = 'hash:"name:2" validate:"printascii"'
    ];

    // List inputs of this task.
    repeated Parameter inputs = 6 [
      (gogoproto.moretags) = 'hash:"name:6" validate:"dive,required"'
    ];

    // List of tasks outputs.
    repeated Parameter outputs = 7 [
      (gogoproto.moretags) = 'hash:"name:7" validate:"dive,required"'
    ];
  }

  // Parameter describes the task's inputs, the task's outputs, and the event's data.
  message Parameter {

    // Parameter's key.
    string key = 8 [
      (gogoproto.moretags) = 'hash:"name:8" validate:"printascii"'
    ];

    // Parameter's name.
    string name = 1 [
      (gogoproto.moretags) = 'hash:"name:1" validate:"printascii"'
    ];

    // Parameter's description.
    string description = 2 [
      (gogoproto.moretags) = 'hash:"name:2" validate:"printascii"'
    ];

    // Parameter's type: `String`, `Number`, `Boolean`, `Object` or `Any`.
    string type = 3 [
      (gogoproto.moretags) = 'hash:"name:3" validate:"required,printascii,oneof=String Number Boolean Object Any"'
    ];

    // Set the parameter as optional.
    bool optional = 4 [
      (gogoproto.moretags) = 'hash:"name:4"'
    ];

    // Mark a parameter as an array of the defined type.
    bool repeated = 9 [
      (gogoproto.moretags) = 'hash:"name:9"'
    ];

    // Optional object structure type when type is set to `Object`.
    repeated Parameter object = 10 [
      (gogoproto.moretags) = 'hash:"name:10" validate:"unique,dive,required"'
    ];
  }

  // A configuration is the configuration of the main container of the service's instance.
  message Configuration {
    // List of volumes.
    repeated string volumes = 1 [
      (gogoproto.moretags) = 'hash:"name:1" validate:"unique,dive,printascii"'
    ];

    // List of volumes mounted from other dependencies.
    repeated string volumesFrom = 2 [
      (gogoproto.moretags) = 'hash:"name:2" validate:"unique,dive,printascii"'
    ];

    // List of ports the container exposes.
    repeated string ports = 3 [
      (gogoproto.moretags) = 'hash:"name:3" validate:"unique,dive,portmap"'
    ];

    // Args to pass to the container.
    repeated string args = 4 [
      (gogoproto.moretags) = 'hash:"name:5" validate:"dive,printascii"'
    ];

    // Command to run the container.
    string command = 5 [
      (gogoproto.moretags) = 'hash:"name:4" validate:"printascii"'
    ];

    // Default env vars to apply to service's instance on runtime.
    repeated string env = 6 [
      (gogoproto.moretags) = 'hash:"name:6" validate:"unique,dive,env"'
    ];
  }

  // A dependency is a configuration of an other container that runs separately from the service.
  message Dependency {
    // Dependency's key.
    string key = 8 [
      (gogoproto.moretags) = 'hash:"name:8" validate:"printascii"'
    ];

    // Image's name of the container.
    string image = 1 [
      (gogoproto.moretags) = 'hash:"name:1" validate:"printascii"'
    ];

    // List of volumes.
    repeated string volumes = 2 [
      (gogoproto.moretags) = 'hash:"name:2" validate:"unique,dive,printascii"'
    ];

    // List of volumes mounted from other dependencies.
    repeated string volumesFrom = 3 [
      (gogoproto.moretags) = 'hash:"name:3" validate:"unique,dive,printascii"'
    ];

    // List of ports the container exposes.
    repeated string ports = 4 [
      (gogoproto.moretags) = 'hash:"name:4" validate:"unique,dive,portmap"'
    ];

    // Args to pass to the container.
    repeated string args = 6 [
      (gogoproto.moretags) = 'hash:"name:6" validate:"dive,printascii"'
    ];

    // Command to run the container.
    string command = 5 [
      (gogoproto.moretags) = 'hash:"name:5" validate:"printascii"'
    ];

    // Default env vars to apply to service's instance on runtime.
    repeated string env = 9 [
      (gogoproto.moretags) = 'hash:"name:9" validate:"unique,dive,env"'
    ];
  }

  // Service's hash.
  bytes hash = 10 [
    (gogoproto.moretags) = 'hash:"-" validate:"required"',
    (gogoproto.customtype) = "github.com/mesg-foundation/engine/hash.Hash",
    (gogoproto.nullable) = false
  ];

  // Service's sid.
  string sid = 12  [
    (gogoproto.moretags) = 'hash:"name:12" validate:"required,printascii,max=63,domain"'
  ];

  // Service's name.
  string name = 1 [
    (gogoproto.moretags) = 'hash:"name:1" validate:"required,printascii"'
  ];

  // Service's description.
  string description = 2 [
    (gogoproto.moretags) = 'hash:"name:2" validate:"printascii"'
  ];

  // Configurations related to the service
  Configuration configuration = 8 [
    (gogoproto.moretags) = 'hash:"name:8" validate:"required"',
    (gogoproto.nullable) = false
  ];

  // The list of tasks this service can execute.
  repeated Task tasks = 5 [
    (gogoproto.moretags) = 'hash:"name:5" validate:"dive,required"'
  ];

  // The list of events this service can emit.
  repeated Event events = 6 [
    (gogoproto.moretags) = 'hash:"name:6" validate:"dive,required"'
  ];

  // The container dependencies this service requires.
  repeated Dependency dependencies = 7 [
    (gogoproto.moretags) = 'hash:"name:7" validate:"dive,required"'
  ];

  // Service's repository url.
  string repository = 9 [
    (gogoproto.moretags) = 'hash:"name:9" validate:"omitempty,uri"'
  ];

  // The hash id of service's source code on IPFS.
  string source = 13 [
    (gogoproto.moretags) = 'hash:"name:13" validate:"required,printascii"'
  ];
}
