syntax = "proto3";
package crm_manager;

service CrmManagerAdminService {
  rpc ListManagers (ListManagersRequest) returns (ListManagersResponse);
  rpc GetManagersReport (GetManagersReportRequest) returns (GetManagersReportResponse);
  rpc BatchGetManagersByIds (BatchGetManagersByIdsRequest) returns (BatchGetManagersByIdsResponse);
  rpc GetManager (GetManagerRequest) returns (GetManagerResponse);
  rpc GetNotificationRecipients (GetNotificationRecipientsRequest) returns (GetNotificationRecipientsResponse);
  rpc CreateManager (CreateManagerRequest) returns (CreateManagerResponse);
  rpc UpdateManager (UpdateManagerRequest) returns (UpdateManagerResponse);
  rpc DeleteManager (DeleteManagerRequest) returns (DeleteManagerResponse);
  rpc ListRoles (ListRolesRequest) returns (ListRolesResponse);
  rpc ListOffices (ListOfficesRequest) returns (ListOfficesResponse);
  rpc CreateOffice (CreateOfficeRequest) returns (CreateOfficeResponse);
  rpc UpdateOffice (UpdateOfficeRequest) returns (UpdateOfficeResponse);
  rpc DeactivateOffice (DeactivateOfficeRequest) returns (DeactivateOfficeResponse);
  // Поиск менеджеров по username/email: возвращает до limit совпадений.
  rpc SearchManager (SearchManagerRequest) returns (SearchManagerResponse);
  // Прелоад выбранного менеджера по id для поиска: возвращает один элемент.
  rpc PrealoadManagerForSearch (PrealoadManagerForSearchRequest) returns (PrealoadManagerForSearchResponse);
}

enum CrmManagerRecordStatus {
  CRM_MANAGER_RECORD_STATUS_UNSPECIFIED = 0;
  CRM_MANAGER_RECORD_STATUS_ACTIVE = 1;
  CRM_MANAGER_RECORD_STATUS_DISABLED = 2;
}

message SectionPermission {
  string section = 1;
  bool view = 2;
  bool edit = 3;
}

message ManagerRecord {
  string id = 1;
  string email = 2;
  string username = 3;
  string first_name = 4;
  string last_name = 5;
  string display_name = 6;
  string avatar_url = 7;
  CrmManagerRecordStatus status = 8;
  int64 created_at_unix_ms = 9;
  repeated string roles = 10;
  repeated string permissions = 11;
  bool is_super_admin = 12;
  repeated SectionPermission section_permissions = 13;
  repeated string office_ids = 14;
  string full_name = 15;
  bool is_active = 16;
  int64 updated_at_unix_ms = 17;
  string role = 18;
  int32 permissions_version = 19;
  bool can_cross_office = 20;
  string telegram_username = 21;
  string telegram_id = 22;
  bool is_telegram_activated = 23;
  bool notify_all_exchanges = 24;
  bool notify_all_payments = 25;
  bool notify_all_payouts = 26;
}

message RoleRecord {
  string id = 1;
  string name = 2;
  bool is_system = 3;
  repeated string permissions = 4;
}

message ListManagersRequest {
  int32 page = 1;
  int32 limit = 2;
  string search = 3;
  optional bool is_active = 4;
}

message ListManagersResponse {
  repeated ManagerRecord managers = 1;
  int32 page = 2;
  int32 limit = 3;
  int32 total_count = 4;
  int32 pages = 5;
}

message GetManagersReportRequest {
  string start_date = 1;
  string end_date = 2;
}

message ManagersReportRow {
  string manager_id = 1;
  string manager_name = 2;
  int32 all_exchanges = 3;
  int32 completed_exchanges = 4;
  double avg_check = 5;
  double total_amount = 6;
  double avg_profit_percent = 7;
  double profit = 8;
}

message GetManagersReportResponse {
  repeated ManagersReportRow results = 1;
}

message BatchGetManagersByIdsRequest {
  repeated string ids = 1;
}

message BatchGetManagersByIdsResponseRecord {
  string id = 1;
  string email = 2;
  string username = 3;
  string first_name = 4;
  string last_name = 5;
  string display_name = 6;
  string avatar_url = 7;
  CrmManagerRecordStatus status = 8;
  int64 created_at_unix_ms = 9;
  int64 updated_at_unix_ms = 10;
  repeated SectionPermission section_permissions = 11;
}

message BatchGetManagersByIdsResponse {
  repeated BatchGetManagersByIdsResponseRecord managers = 1;
}

message GetManagerRequest {
  string manager_id = 1;
}

message GetManagerResponse {
  bool exist = 1;
  optional ManagerRecord manager = 2;
}

message CreateManagerRequest {
  string email = 1;
  string username = 2;
  string first_name = 3;
  string last_name = 4;
  string display_name = 5;
  string avatar_url = 6;
  CrmManagerRecordStatus status = 7;
  repeated string role_names = 8;
  repeated SectionPermission section_permissions = 9;
  repeated string office_ids = 10;
  string password = 11;
  string full_name = 12;
  bool can_cross_office = 13;
  repeated string custom_permissions = 14;
  string telegram_username = 15;
  bool notify_all_exchanges = 16;
  bool notify_all_payments = 17;
  bool notify_all_payouts = 18;
}

message CreateManagerResponse {
  ManagerRecord manager = 1;
  string otp_secret = 2;
  string otp_auth_uri = 3;
}

message UpdateManagerRequest {
  string manager_id = 1;
  string email = 2;
  string username = 3;
  string first_name = 4;
  string last_name = 5;
  string display_name = 6;
  string avatar_url = 7;
  CrmManagerRecordStatus status = 8;
  bool replace_roles = 9;
  repeated string role_names = 10;
  repeated SectionPermission section_permissions = 11;
  bool replace_section_permissions = 12;
  bool replace_office_ids = 13;
  repeated string office_ids = 14;
  string password = 15;
  string full_name = 16;
  optional bool can_cross_office = 17;
  bool replace_custom_permissions = 18;
  repeated string custom_permissions = 19;
  string telegram_username = 20;
  optional bool notify_all_exchanges = 21;
  optional bool notify_all_payments = 22;
  optional bool notify_all_payouts = 23;
}

message UpdateManagerResponse {
  ManagerRecord manager = 1;
}

message DeleteManagerRequest {
  string manager_id = 1;
  string actor_manager_id = 2;
}

message DeleteManagerResponse {
  bool success = 1;
}

message ListRolesRequest {}

message ListRolesResponse {
  repeated RoleRecord roles = 1;
}

message OfficeRecord {
  string id = 1;
  string code = 2;
  string name = 3;
  string address = 4;
  string city = 5;
  string country = 6;
  bool is_active = 7;
  int64 created_at_unix_ms = 8;
  int64 updated_at_unix_ms = 9;
}

message ListOfficesRequest {
  int32 page = 1;
  int32 limit = 2;
  string search = 3;
  bool include_inactive = 4;
}

message ListOfficesResponse {
  repeated OfficeRecord offices = 1;
  int32 page = 2;
  int32 limit = 3;
  int32 total_count = 4;
  int32 pages = 5;
}

message CreateOfficeRequest {
  string code = 1;
  string name = 2;
  string address = 3;
  string city = 4;
  string country = 5;
}

message CreateOfficeResponse {
  OfficeRecord office = 1;
}

message UpdateOfficeRequest {
  string office_id = 1;
  string code = 2;
  string name = 3;
  string address = 4;
  string city = 5;
  string country = 6;
  optional bool is_active = 7;
}

message UpdateOfficeResponse {
  OfficeRecord office = 1;
}

message DeactivateOfficeRequest {
  string office_id = 1;
}

message DeactivateOfficeResponse {
  bool success = 1;
}

message SearchManagerRequest {
  string search = 1;
  // По умолчанию 50, максимум 50.
  int32 limit = 2;
}

message SearchManagerItem {
  string id = 1;
  string username = 2;
  string email = 3;
}

message SearchManagerResponse {
  repeated SearchManagerItem items = 1;
}

message PrealoadManagerForSearchRequest {
  string manager_id = 1;
}

message PrealoadManagerForSearchResponse {
  optional SearchManagerItem item = 1;
}

message GetNotificationRecipientsRequest {
  string event_type = 1;
  string assigned_manager_id = 2;
  string actor_manager_id = 3;
}

message NotificationRecipient {
  string manager_id = 1;
  string email = 2;
  string telegram_id = 3;
  string telegram_username = 4;
  bool is_telegram_activated = 5;
  string reason = 6;
}

message GetNotificationRecipientsResponse {
  repeated NotificationRecipient recipients = 1;
}

message GetManagerForServiceRequest {
  string manager_id = 1;
}

message GetManagerForServiceResponseRecord {
  string id = 1;
  string email = 2;
  string username = 3;
  string first_name = 4;
  string last_name = 5;
  string display_name = 6;
  string avatar_url = 7;
  CrmManagerRecordStatus status = 8;
}

message GetManagerForServiceResponse {
  bool exist = 1;
  optional GetManagerForServiceResponseRecord record = 2;
}

message JsonResponse {
  int32 status_code = 1;
  string json = 2;
}
