syntax = "proto3";

package identity;

service OAuthCoreService {
  rpc Authorize (AuthorizeRequest) returns (AuthorizeResponse);
  rpc Token (TokenRequest) returns (TokenResponse);
  rpc UserInfo (UserInfoRequest) returns (UserInfoResponse);
  rpc CheckOAuthConsent (CheckOAuthConsentRequest) returns (CheckOAuthConsentResponse);
  rpc GrantOAuthConsent (GrantOAuthConsentRequest) returns (GrantOAuthConsentResponse);
  rpc ListUserOAuthConsents (UserIdRequest) returns (ListUserOAuthConsentsResponse);
  rpc RevokeOAuthConsent (RevokeOAuthConsentRequest) returns (MutationResponse);
  rpc GetOAuthClientPublicInfo (GetOAuthClientPublicInfoRequest) returns (OAuthClientPublicInfo);
}

service OtpService {
  rpc SendOtp (SendOtpRequest) returns (OtpResponse);
  rpc VerifyOtp (VerifyOtpRequest) returns (OtpVerifyResponse);
}

service AdvancedAuthService {
  rpc CreateWebAuthnRegistrationChallenge (WebAuthnRequest) returns (ChallengeResponse);
  rpc CreateWebAuthnLoginChallenge (WebAuthnRequest) returns (ChallengeResponse);
  rpc CreateQrSession (QrSessionRequest) returns (QrSessionResponse);
  rpc PollQrSession (QrPollRequest) returns (QrSessionResponse);
  rpc ApproveQrSession (ApproveQrSessionRequest) returns (QrSessionResponse);
}

service FamilyService {
  rpc CreateFamilyGroup (CreateFamilyGroupRequest) returns (FamilyGroupResponse);
  rpc ListFamilyGroups (UserIdRequest) returns (ListFamilyGroupsResponse);
  rpc GetFamilyGroup (GetFamilyGroupRequest) returns (FamilyGroupResponse);
  rpc UpdateFamilyGroup (UpdateFamilyGroupRequest) returns (FamilyGroupResponse);
  rpc DeleteFamilyGroup (DeleteFamilyGroupRequest) returns (MutationResponse);
  rpc AddFamilyMember (FamilyMemberRequest) returns (FamilyMemberResponse);
  rpc RemoveFamilyMember (RemoveFamilyMemberRequest) returns (MutationResponse);
  rpc SendFamilyInvite (SendFamilyInviteRequest) returns (FamilyInviteResponse);
  rpc SearchFamilyInviteUsers (SearchFamilyInviteUsersRequest) returns (SearchFamilyInviteUsersResponse);
  rpc RespondFamilyInvite (RespondFamilyInviteRequest) returns (FamilyInviteResponse);
  rpc ListFamilyInvites (ListFamilyInvitesRequest) returns (ListFamilyInvitesResponse);
  rpc GetFamilyPresence (GetFamilyGroupRequest) returns (FamilyPresenceResponse);
}

message AuthorizeRequest {
  string userId = 1;
  string clientId = 2;
  string redirectUri = 3;
  string scope = 4;
  optional string state = 5;
  optional string nonce = 6;
  optional bool grantConsent = 7;
}

message AuthorizeResponse {
  string redirectUrl = 1;
  string code = 2;
  optional string state = 3;
}

message TokenRequest {
  string grantType = 1;
  optional string code = 2;
  optional string refreshToken = 3;
  string clientId = 4;
  optional string clientSecret = 5;
  optional string redirectUri = 6;
}

message TokenResponse {
  string accessToken = 1;
  string tokenType = 2;
  int32 expiresIn = 3;
  string refreshToken = 4;
  string idToken = 5;
}

message UserInfoRequest {
  string accessToken = 1;
}

message UserInfoResponse {
  string sub = 1;
  optional string email = 2;
  optional string phone = 3;
  string name = 4;
  optional string picture = 5;
  optional bool email_verified = 6;
  optional string preferred_username = 7;
  optional string phone_number = 8;
  optional bool phone_number_verified = 9;
}

message OAuthScopeInfo {
  string slug = 1;
  string name = 2;
  optional string description = 3;
}

message OAuthClientPublicInfo {
  string clientId = 1;
  string name = 2;
}

message GetOAuthClientPublicInfoRequest {
  string clientId = 1;
}

message CheckOAuthConsentRequest {
  string userId = 1;
  string clientId = 2;
  string scope = 3;
}

message CheckOAuthConsentResponse {
  bool granted = 1;
  OAuthClientPublicInfo client = 2;
  repeated OAuthScopeInfo requestedScopes = 3;
}

message GrantOAuthConsentRequest {
  string userId = 1;
  string clientId = 2;
  string scope = 3;
}

message GrantOAuthConsentResponse {
  bool granted = 1;
}

message UserOAuthConsentItem {
  string id = 1;
  string clientId = 2;
  string clientName = 3;
  repeated OAuthScopeInfo scopes = 4;
  string grantedAt = 5;
  string updatedAt = 6;
}

message ListUserOAuthConsentsResponse {
  repeated UserOAuthConsentItem consents = 1;
}

message RevokeOAuthConsentRequest {
  string userId = 1;
  string consentId = 2;
}

message SendOtpRequest {
  string target = 1;
  string channel = 2;
  string purpose = 3;
  optional string userId = 4;
  optional string ipAddress = 5;
}

message VerifyOtpRequest {
  string target = 1;
  string code = 2;
  string purpose = 3;
}

message OtpResponse {
  bool sent = 1;
  string expiresAt = 2;
}

message OtpVerifyResponse {
  bool verified = 1;
}

message WebAuthnRequest {
  string userId = 1;
}

message ChallengeResponse {
  string challengeId = 1;
  string challenge = 2;
  string expiresAt = 3;
}

message QrSessionRequest {
  string deviceName = 1;
  optional string fingerprint = 2;
  optional string deviceType = 3;
  optional string ipAddress = 4;
  optional string userAgent = 5;
}

message QrPollRequest {
  string sessionId = 1;
}

message QrSessionResponse {
  string sessionId = 1;
  string status = 2;
  string expiresAt = 3;
  optional string qrPayload = 4;
  optional QrAuthPayload auth = 5;
}

message ApproveQrSessionRequest {
  string sessionId = 1;
  string userId = 2;
}

message QrAuthPayload {
  string accessToken = 1;
  string refreshToken = 2;
  string sessionId = 3;
  bool pinVerified = 4;
  string expiresAt = 5;
}

message UserIdRequest {
  string userId = 1;
}

message CreateFamilyGroupRequest {
  string ownerId = 1;
  string name = 2;
}

message UpdateFamilyGroupRequest {
  string requesterId = 1;
  string groupId = 2;
  string name = 3;
}

message DeleteFamilyGroupRequest {
  string requesterId = 1;
  string groupId = 2;
}

message GetFamilyGroupRequest {
  string requesterId = 1;
  string groupId = 2;
}

message RemoveFamilyMemberRequest {
  string requesterId = 1;
  string memberId = 2;
}

message SendFamilyInviteRequest {
  string requesterId = 1;
  string groupId = 2;
  optional string target = 3;
  optional string inviteeUserId = 4;
}

message SearchFamilyInviteUsersRequest {
  string requesterId = 1;
  string groupId = 2;
  string query = 3;
}

message FamilyInviteUserCandidate {
  string id = 1;
  string displayName = 2;
  optional string email = 3;
  optional string phone = 4;
  optional string username = 5;
  bool hasAvatar = 6;
  bool isBot = 7;
  optional string botUsername = 8;
  optional string botId = 9;
  optional string ownerDisplayName = 10;
}

message SearchFamilyInviteUsersResponse {
  repeated FamilyInviteUserCandidate users = 1;
}

message RespondFamilyInviteRequest {
  string userId = 1;
  string inviteId = 2;
  bool accept = 3;
}

message ListFamilyInvitesRequest {
  string userId = 1;
  optional string groupId = 2;
}

message FamilyInviteResponse {
  string id = 1;
  string groupId = 2;
  string groupName = 3;
  string inviterId = 4;
  string inviterName = 5;
  optional string inviteeUserId = 6;
  optional string targetEmail = 7;
  optional string targetPhone = 8;
  string status = 9;
  string expiresAt = 10;
  string createdAt = 11;
}

message ListFamilyInvitesResponse {
  repeated FamilyInviteResponse invites = 1;
}

message FamilyGroupIdRequest {
  string groupId = 1;
}

message FamilyMemberRequest {
  string groupId = 1;
  string userId = 2;
  string role = 3;
}

message FamilyMemberIdRequest {
  string memberId = 1;
}

message FamilyMemberResponse {
  string id = 1;
  string groupId = 2;
  string userId = 3;
  string role = 4;
  string displayName = 5;
  bool hasAvatar = 6;
  string createdAt = 7;
  bool isVerified = 8;
  optional string verificationIcon = 9;
  bool isBot = 10;
  optional string botUsername = 11;
}

message FamilyGroupResponse {
  string id = 1;
  string ownerId = 2;
  string name = 3;
  bool hasAvatar = 4;
  string createdAt = 5;
  string updatedAt = 6;
  repeated FamilyMemberResponse members = 7;
  bool botFatherAvailable = 8;
  bool getMyIdBotAvailable = 9;
}

message ListFamilyGroupsResponse {
  repeated FamilyGroupResponse groups = 1;
}

message MutationResponse {
  int32 count = 1;
}

message FamilyPresenceMember {
  string userId = 1;
  string displayName = 2;
  bool online = 3;
  optional string lastSeenAt = 4;
}

message FamilyPresenceResponse {
  repeated FamilyPresenceMember members = 1;
  int32 onlineCount = 2;
}
