syntax = "proto3";

package networkmembership;

import "public_messages.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/wrappers.proto";
import "google/protobuf/timestamp.proto";
import "google/api/annotations.proto";

option csharp_namespace = "CMSMicroservice.Protobuf.Protos.NetworkMembership";

service NetworkMembershipContract
{
    rpc JoinNetwork(JoinNetworkRequest) returns (google.protobuf.Empty){
        option (google.api.http) = {
            post: "/NetworkMembership/Join"
            body: "*"
        };
    };
    rpc ChangeNetworkParent(ChangeNetworkParentRequest) returns (google.protobuf.Empty){
        option (google.api.http) = {
            post: "/NetworkMembership/ChangeParent"
            body: "*"
        };
    };
    rpc RemoveFromNetwork(RemoveFromNetworkRequest) returns (google.protobuf.Empty){
        option (google.api.http) = {
            post: "/NetworkMembership/Remove"
            body: "*"
        };
    };
    rpc GetUserNetwork(GetUserNetworkRequest) returns (GetUserNetworkResponse){
        option (google.api.http) = {
            get: "/NetworkMembership/GetUserNetwork"
        };
    };
    rpc GetNetworkTree(GetNetworkTreeRequest) returns (GetNetworkTreeResponse){
        option (google.api.http) = {
            get: "/NetworkMembership/GetNetworkTree"
        };
    };
    rpc GetNetworkMembershipHistory(GetNetworkMembershipHistoryRequest) returns (GetNetworkMembershipHistoryResponse){
        option (google.api.http) = {
            get: "/NetworkMembership/GetHistory"
        };
    };
    rpc GetNetworkStatistics(GetNetworkStatisticsRequest) returns (GetNetworkStatisticsResponse){
        option (google.api.http) = {
            get: "/NetworkMembership/GetStatistics"
        };
    };
}

// JoinNetwork Command
message JoinNetworkRequest
{
    int64 user_id = 1;
    int64 parent_id = 2;
    int32 leg = 3; // NetworkLeg enum: Left=0, Right=1
    google.protobuf.StringValue referral_code = 4;
}

// ChangeParent Command
message ChangeNetworkParentRequest
{
    int64 user_id = 1;
    int64 new_parent_id = 2;
    int32 new_leg = 3; // NetworkLeg enum
    string reason = 4;
}

// Remove Command
message RemoveFromNetworkRequest
{
    int64 user_id = 1;
    string reason = 2;
}

// GetUserNetwork Query
message GetUserNetworkRequest
{
    int64 user_id = 1;
}

message GetUserNetworkResponse
{
    int64 id = 1;
    int64 user_id = 2;
    string user_name = 3;
    google.protobuf.Int64Value parent_id = 4;
    string parent_name = 5;
    int32 network_leg = 6; // NetworkLeg enum
    google.protobuf.Int64Value left_child_id = 7;
    string left_child_name = 8;
    google.protobuf.Int64Value right_child_id = 9;
    string right_child_name = 10;
    int32 network_level = 11;
    string referral_code = 12;
    google.protobuf.Timestamp joined_at = 13;
    google.protobuf.Timestamp created = 14;
}

// GetNetworkTree Query
message GetNetworkTreeRequest
{
    int64 root_user_id = 1;
    google.protobuf.Int32Value max_depth = 2;
    google.protobuf.BoolValue only_active = 3;
}

message GetNetworkTreeResponse
{
    repeated NetworkTreeNodeModel nodes = 1;
}

message NetworkTreeNodeModel
{
    int64 user_id = 1;
    string user_name = 2;
    google.protobuf.Int64Value parent_id = 3;
    int32 network_leg = 4;
    int32 network_level = 5;
    bool is_active = 6;
    google.protobuf.Timestamp joined_at = 7;
}

// GetHistory Query
message GetNetworkMembershipHistoryRequest
{
    google.protobuf.Int64Value user_id = 1;
    google.protobuf.Int64Value parent_id = 2;
    int32 page_index = 3;
    int32 page_size = 4;
}

message GetNetworkMembershipHistoryResponse
{
    messages.MetaData meta_data = 1;
    repeated NetworkMembershipHistoryModel models = 2;
}

message NetworkMembershipHistoryModel
{
    int64 id = 1;
    int64 user_id = 2;
    google.protobuf.Int64Value old_parent_id = 3;
    google.protobuf.Int64Value new_parent_id = 4;
    google.protobuf.Int32Value old_network_leg = 5;
    google.protobuf.Int32Value new_network_leg = 6;
    google.protobuf.Int32Value old_network_level = 7;
    google.protobuf.Int32Value new_network_level = 8;
    int32 action = 9; // NetworkMembershipAction enum
    string performed_by = 10;
    string reason = 11;
    google.protobuf.Timestamp created = 12;
}

// GetNetworkStatistics Query
message GetNetworkStatisticsRequest
{
    // Empty - returns overall network statistics
}

message GetNetworkStatisticsResponse
{
    int32 total_members = 1;
    int32 active_members = 2;
    int32 left_leg_count = 3;
    int32 right_leg_count = 4;
    double left_percentage = 5;
    double right_percentage = 6;
    double average_depth = 7;
    int32 max_depth = 8;
    repeated LevelDistribution level_distribution = 9;
    repeated MonthlyGrowth monthly_growth = 10;
    repeated TopNetworkUser top_users = 11;
}

message LevelDistribution
{
    int32 level = 1;
    int32 count = 2;
}

message MonthlyGrowth
{
    string month = 1; // Format: "2025-11" or Persian month name
    int32 new_members = 2;
}

message TopNetworkUser
{
    int32 rank = 1;
    int64 user_id = 2;
    string user_name = 3;
    int32 total_children = 4;
    int32 left_count = 5;
    int32 right_count = 6;
}
