syntax = "proto3";

package identity;

service OAuthCoreService {
  rpc Authorize (AuthorizeRequest) returns (AuthorizeResponse);
  rpc Token (TokenRequest) returns (TokenResponse);
  rpc UserInfo (UserInfoRequest) returns (UserInfoResponse);
}

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);
}

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 RespondFamilyInvite (RespondFamilyInviteRequest) returns (FamilyInviteResponse);
  rpc ListFamilyInvites (ListFamilyInvitesRequest) returns (ListFamilyInvitesResponse);
}

message AuthorizeRequest {
  string userId = 1;
  string clientId = 2;
  string redirectUri = 3;
  string scope = 4;
  optional string state = 5;
}

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;
}

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

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;
}

message QrPollRequest {
  string sessionId = 1;
}

message QrSessionResponse {
  string sessionId = 1;
  string status = 2;
  string expiresAt = 3;
}

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;
  string target = 3;
}

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;
}

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

message ListFamilyGroupsResponse {
  repeated FamilyGroupResponse groups = 1;
}

message MutationResponse {
  int32 count = 1;
}
