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"
        };
    };
    
    // Customer-facing APIs
    rpc GetMyNetworkTree(GetMyNetworkTreeRequest) returns (GetMyNetworkTreeResponse){
        option (google.api.http) = {
            get: "/NetworkMembership/Customer/MyTree"
        };
    };
    
    rpc GetSubordinateTree(GetSubordinateTreeRequest) returns (GetMyNetworkTreeResponse){
        option (google.api.http) = {
            get: "/NetworkMembership/Customer/SubordinateTree/{target_user_id}"
        };
    };
    
    rpc GetMyNetworkStatistics(google.protobuf.Empty) returns (GetMyNetworkStatisticsResponse){
        option (google.api.http) = {
            get: "/NetworkMembership/Customer/MyStats"
        };
    };
}

// 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;
    string mobile = 4;
    string email = 5;
    string national_code = 6;
    string referral_code = 7;
    bool is_mobile_verified = 8;
    google.protobuf.Timestamp birth_date = 9;
    google.protobuf.Timestamp joined_at = 10;
    
    // اطلاعات والد
    google.protobuf.Int64Value parent_id = 11;
    string parent_name = 12;
    string parent_mobile = 13;
    
    // موقعیت در شبکه
    int32 network_leg = 14; // NetworkLeg enum
    int32 network_level = 15;
    bool is_in_network = 16;
    
    // اطلاعات فرزند چپ
    google.protobuf.Int64Value left_child_id = 17;
    string left_child_name = 18;
    string left_child_mobile = 19;
    google.protobuf.Timestamp left_child_joined_at = 20;
    
    // اطلاعات فرزند راست
    google.protobuf.Int64Value right_child_id = 21;
    string right_child_name = 22;
    string right_child_mobile = 23;
    google.protobuf.Timestamp right_child_joined_at = 24;
    
    // آمار فرزندان مستقیم
    int32 total_children = 25;
    int32 left_child_count = 26;
    int32 right_child_count = 27;
    
    // آمار کل شبکه
    int32 total_left_leg_members = 28;
    int32 total_right_leg_members = 29;
    int32 total_network_size = 30;
    int32 max_network_depth = 31;
    
    // اطلاعات پکیج و دایا
    bool has_received_daya_credit = 32;
    google.protobuf.Timestamp daya_credit_received_at = 33;
    int32 package_purchase_method = 34;
    bool has_purchased_package = 35;
    
    // آمار مالی
    double total_earned_commission = 36;
    double total_paid_commission = 37;
    double pending_commission = 38;
    int32 total_balances_earned = 39;
    
    // آمار فعالیت
    int32 active_members_in_network = 40;
    int32 inactive_members_in_network = 41;
    
    google.protobuf.Timestamp created = 42;
}

// GetNetworkTree Query
message GetNetworkTreeRequest
{
    int64 user_id = 1;
    google.protobuf.Int32Value max_depth = 2;
    google.protobuf.BoolValue only_active = 3;
    google.protobuf.BoolValue is_club_active = 4;
    google.protobuf.StringValue activation_week_definition_id = 5;
}

message GetNetworkTreeResponse
{
    repeated NetworkTreeNodeModel nodes = 1;
}

message NetworkTreeNodeModel
{
    int64 user_id = 1;
    string user_name = 2;
    string full_name = 20; // Alias for frontend compatibility
    google.protobuf.Int64Value parent_id = 3;
    int32 network_leg = 4;
    int32 network_level = 5;
    int32 level = 21; // Alias for frontend compatibility
    bool is_active = 6;
    google.protobuf.Timestamp joined_at = 7;
    google.protobuf.Timestamp club_activated_at = 8; // تاریخ فعال‌سازی در باشگاه
    bool is_club_active = 9; // فعال بودن در باشگاه
    google.protobuf.Int64Value activation_week_definition_id = 10; // شماره هفته فعال‌سازی
    bool is_activated_in_target_week = 11; // آیا در هفته هدف فعال شده
    google.protobuf.Timestamp user_created = 12; // تاریخ ایجاد کاربر
    string referral_code = 13; // کد معرف کاربر
    string mobile = 14; // Mobile number for display
    google.protobuf.StringValue avatar = 15; // Avatar path
    string position = 16; // Root/Left/Right position indicator
    NetworkTreeNodeModel left_child = 17; // Left child node
    NetworkTreeNodeModel right_child = 18; // Right child node
    // فیلدهای اطلاعات پکیج و نوع فعال‌سازی
    google.protobuf.BoolValue is_new_activation = 22; // true=اولین فعال‌سازی، false=خرید مجدد، null=بدون Cycle
    string package_name = 23; // نام پکیج موثر
    google.protobuf.Int64Value package_id = 24; // شناسه پکیج
    // تعداد اعضای فعال با پکیج جاری در پای چپ/راست (طلایی و نقره‌ای)
    int32 gold_left_leg_total = 25;
    int32 gold_right_leg_total = 26;
    int32 silver_left_leg_total = 27;
    int32 silver_right_leg_total = 28;
}

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

// ============ Customer APIs ============

// GetMyNetworkTree - for frontend customer display
message GetMyNetworkTreeRequest
{
    int32 max_depth = 1; // Default: 3
}

// GetSubordinateTree - for frontend customer display
message GetSubordinateTreeRequest
{
    int64 target_user_id = 1;
    int32 max_depth = 2; // Default: 3
}

message GetMyNetworkTreeResponse
{
    NetworkTreeNodeModel root_node = 1;
    int32 total_members = 2;
    int32 current_depth = 3;
}

// GetMyNetworkStatistics - for frontend customer display
message GetMyNetworkStatisticsResponse
{
    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;
    string weaker_leg = 9;
    int32 my_network_level = 10;
    string my_network_leg = 11;
    string my_referral_code = 12;
    CustomerLastMemberModel last_member = 13;
    // تعداد اعضای فعال با پکیج جاری در پای چپ/راست — نوار آمار پایین شجره (D3)
    int32 gold_left_leg_total = 14;
    int32 gold_right_leg_total = 15;
    int32 silver_left_leg_total = 16;
    int32 silver_right_leg_total = 17;
}

message CustomerLastMemberModel
{
    int64 user_id = 1;
    string full_name = 2;
    string position = 3;
    int32 total_children = 4;
}
