syntax = "proto2";

package maps_adv.geosmb.doorman.proto.clients;

import "google/protobuf/timestamp.proto";
import "maps_adv/geosmb/doorman/proto/common.proto";
import "maps_adv/geosmb/doorman/proto/segments.proto";
import "maps_adv/geosmb/doorman/proto/statistics.proto";

message SourceMetadata {
    required common.Source source = 1;
    optional string extra = 2;   // json-string

    optional string url = 3;
    optional string device_id = 4;
    optional string uuid = 5;
}

message ClientSetupData {
    required uint64 biz_id = 1;
    required SourceMetadata metadata = 2;
    optional uint64 phone = 3;
    optional string email = 4;
    optional uint64 passport_uid = 5;
    // source specific fields
    optional string first_name = 6;
    optional string last_name = 7;
    optional common.ClientGender gender = 8;
    optional string comment = 9;
    optional uint64 initiator_id = 10;
}

message ClientData {
    required uint64 id = 1;
    required uint64 biz_id = 2;
    optional uint64 phone = 3;
    optional string email = 4;
    optional uint64 passport_uid = 5;
    optional string first_name = 6;
    optional string last_name = 7;
    optional common.ClientGender gender = 8;
    optional string comment = 9;
    repeated segments.SegmentType segments = 10;
    optional statistics.ClientStatistics statistics = 11;
    required common.Source source = 12;
    required google.protobuf.Timestamp registration_timestamp = 13;
    optional bool cleared_for_gdpr = 14;
    repeated string labels = 15;
}

message ClientUpdateData {
    required uint64 id = 1;
    required ClientSetupData data = 2;
}

message ClientRetrieveInput {
    required uint64 biz_id = 1;
    required uint64 id = 2;
}

message ClientsOrderBy {
    enum OrderField {
        PHONE = 1;
        EMAIL = 2;
        FIRST_AND_LAST_NAME = 3;
        COMMENT = 4;
        // orders stat
        STAT_ORDERS_TOTAL = 100;
        STAT_ORDERS_SUCCESSFUL = 101;
        STAT_ORDERS_UNSUCCESSFUL = 102;
        STAT_ORDERS_LAST_ORDER_TS = 103;
    }
    enum OrderDirection {
        ASC = 1;
        DESC = 2;
    }
    required OrderField field = 1;
    required OrderDirection direction = 2;
}

message ClientsListInput {
    required uint64 biz_id = 1;
    optional string search_string = 2;
    required common.Pagination pagination = 3;
    oneof segments {
        segments.SegmentType segment = 4;
        string label = 7;
    }
    repeated uint64 client_ids = 5;
    optional ClientsOrderBy order_by = 6;
}

message ClientsListOutput {
    repeated ClientData clients = 1;
    required uint32 total_count = 2;
}

message ClientsListBySegmentInput {
    required uint64 biz_id = 1;
    oneof segments {
        segments.SegmentType segment = 2;
        string label = 3;
    }
}

message ClientContactsList {
    repeated ClientContacts clients = 1;
}

message ClientContacts {
    required uint64 id = 1;
    required uint64 biz_id = 2;
    optional uint64 phone = 3;
    optional string email = 4;
    optional uint64 passport_uid = 5;
    optional string first_name = 6;
    optional string last_name = 7;
    optional bool cleared_for_gdpr = 8;
}

message ListSuggestClientsInput {
    enum ClientSearchField {
        PHONE = 1;
        EMAIL = 2;
    }

    required uint64 biz_id = 1;
    required ClientSearchField search_field = 2;
    required string search_string = 3;
    optional uint32 limit = 4 [default=5];
}

message ListContactsInput {
    repeated uint64 client_ids = 1;
}

message ClearClientsForGdprInput {
    required uint64 passport_uid = 1;
}

message ClearClientsForGdprOutput {
    message ClearedClient {
        required uint64 biz_id = 1;
        required uint64 client_id = 2;
    }

    repeated ClearedClient cleared_clients = 1;
}

message SearchClientsForGdprInput {
    required uint64 passport_uid = 1;
}

message SearchClientsForGdprOutput {
    required bool clients_exist = 1;
}

message BulkCreateClientsInput {
    message BulkClient {
        optional string first_name = 1;
        optional string last_name = 2;
        optional uint64 phone = 3;
        optional string email = 4;
        optional string comment = 5;
    }

    required uint64 biz_id = 1;
    required common.Source source = 2;
    optional string label = 3;
    repeated BulkClient clients = 4;
}

message BulkCreateClientsOutput {
    required uint32 total_created = 1;
    required uint32 total_merged = 2;
}
