syntax = "proto3";

option java_multiple_files = true;
option java_package = "<%= packageName %>.web.grpc.entity.<%=entityUnderscoredName%>";

import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";
<%_ if (['sql', 'mongodb', 'couchbase'].includes(databaseType)) { _%>
import "util/_pagination.proto";
<%_ } _%>
import "buf/validate/validate.proto";

package entity;

service UserService {
    <%_ if (authenticationType !== 'oauth2') { _%>
    // Create a new User.
    // request : the UserProto to create
    // response : the created UserProto or a ALREADY_EXISTS error
    rpc CreateUser (UserProto) returns (UserProto);

    // Update or create a User.
    // request : the UserProto to update or create
    // response : the updated UserProto
    rpc UpdateUser (UserProto) returns (UserProto);

    <%_ } _%>
    // Get all the Users.
    // response : the list of UserProtos
    rpc GetAllUsers (<% if (['sql', 'mongodb', 'couchbase'].includes(databaseType)) { %>util.PageRequest<% } else { %>google.protobuf.Empty<% } %>) returns (stream UserProto);

    // Get a User from it's id.
    // request : the id of the UserProto to retrieve
    // response : the UserProto or a NOT_FOUND error
    rpc GetUser (UserLoginProto) returns (UserProto);
    <%_ if (authenticationType !== 'oauth2') { _%>

    // Delete a User from it's id.
    // request : the id of the UserProto to delete
    // response : empty or a NOT_FOUND error
    rpc DeleteUser (UserLoginProto) returns (google.protobuf.Empty);
    <%_ } _%>
    <%_ if (['sql', 'mongodb', 'couchbase'].includes(databaseType)) { _%>

    // Get all authorities
    // request : empty
    // response : the list of existing roles
    rpc GetAllAuthorities (google.protobuf.Empty) returns (stream google.protobuf.StringValue);
    <%_ } _%>
<%_ if (searchEngine === 'elasticsearch') { _%>

    // Search for the user corresponding to the query.
    // request : the query of the user search
    // response : the list of userProtos
    rpc SearchUsers(UserSearchPageRequest) returns (stream UserProto);
}

message UserSearchPageRequest {
    google.protobuf.StringValue query = 1;
    util.PageRequest page_request = 2;
<%_ } _%>
}

message UserProto {
    oneof id_oneof {
        <%=idProtoType%> id = 1;
    }
    string login = 2 [(buf.validate.field).string = {
        min_len: 1,
        max_len: 50
    }];
    <%_ if (authenticationType !== 'oauth2') { _%>
    string password = 3;
    <%_ } _%>
    string first_name = 4 [(buf.validate.field).string.max_len = 50];
    string last_name = 5 [(buf.validate.field).string.max_len = 50];
    string email = 6 [(buf.validate.field).string = {
        email: true,
        min_len: 5,
        max_len: 254
    }];
    bool activated = 7;
    string lang_key = 8 [(buf.validate.field).string = {
        min_len: 2,
        max_len: 10
    }];
    repeated string authorities = 9;
    string reset_key = 10 [(buf.validate.field).string.max_len = 20];
    google.protobuf.Timestamp reset_date = 11;
    <%_ if (databaseType !== 'cassandra') { _%>
    string image_url = 12 [(buf.validate.field).string.max_len = 256];
    string created_by = 13;
    google.protobuf.Timestamp created_date = 14;
    string last_modified_by = 15;
    google.protobuf.Timestamp last_modified_date = 16;
    <%_ } _%>
}
message UserLoginProto {
    string login = 1 [(buf.validate.field).string = {
            min_len: 1,
            max_len: 50
        }];
}
