syntax = "proto3";

package clubmembership;

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.ClubMembership";

service ClubMembershipContract
{
    rpc ActivateClubMembership(ActivateClubMembershipRequest) returns (google.protobuf.Empty){
        option (google.api.http) = {
            post: "/ClubMembership/Activate"
            body: "*"
        };
    };
    rpc DeactivateClubMembership(DeactivateClubMembershipRequest) returns (google.protobuf.Empty){
        option (google.api.http) = {
            post: "/ClubMembership/Deactivate"
            body: "*"
        };
    };
    rpc AssignFeatureToMembership(AssignFeatureToMembershipRequest) returns (google.protobuf.Empty){
        option (google.api.http) = {
            post: "/ClubMembership/AssignFeature"
            body: "*"
        };
    };
    rpc GetClubMembership(GetClubMembershipRequest) returns (GetClubMembershipResponse){
        option (google.api.http) = {
            get: "/ClubMembership/Get"
        };
    };
    rpc GetAllClubMemberships(GetAllClubMembershipsRequest) returns (GetAllClubMembershipsResponse){
        option (google.api.http) = {
            get: "/ClubMembership/GetAll"
        };
    };
    rpc GetClubMembershipHistory(GetClubMembershipHistoryRequest) returns (GetClubMembershipHistoryResponse){
        option (google.api.http) = {
            get: "/ClubMembership/GetHistory"
        };
    };
    rpc GetClubStatistics(GetClubStatisticsRequest) returns (GetClubStatisticsResponse){
        option (google.api.http) = {
            get: "/ClubMembership/GetStatistics"
        };
    };
    
    // New: Admin Club Feature Management
    rpc GetUserClubFeatures(GetUserClubFeaturesRequest) returns (GetUserClubFeaturesResponse){
        option (google.api.http) = {
            get: "/ClubFeature/GetUserFeatures"
        };
    };
    rpc ToggleUserClubFeature(ToggleUserClubFeatureRequest) returns (ToggleUserClubFeatureResponse){
        option (google.api.http) = {
            post: "/ClubFeature/ToggleFeature"
            body: "*"
        };
    };
    
    // امضای قرارداد باشگاه مشتریان
    rpc AcceptClubMembershipContract(AcceptClubMembershipContractRequest) returns (AcceptClubMembershipContractResponse){
        option (google.api.http) = {
            post: "/ClubMembership/AcceptContract"
            body: "*"
        };
    };
}

// Activate Command
message ActivateClubMembershipRequest
{
    int64 user_id = 1;
    int64 package_id = 2;
    google.protobuf.StringValue activation_code = 3;
    int32 duration_months = 4;
    bool force_activation = 5;
}

// Deactivate Command
message DeactivateClubMembershipRequest
{
    int64 user_id = 1;
    string reason = 2;
}

// AssignFeature Command
message AssignFeatureToMembershipRequest
{
    int64 user_id = 1;
    int64 product_id = 2;
    google.protobuf.Int32Value quantity = 3;
    google.protobuf.Int32Value duration_days = 4;
}

// Get Query
message GetClubMembershipRequest
{
    int64 user_id = 1;
}

message GetClubMembershipResponse
{
    int64 id = 1;
    int64 user_id = 2;
    int64 package_id = 3;
    string package_name = 4;
    string activation_code = 5;
    google.protobuf.Timestamp activated_at = 6;
    google.protobuf.Timestamp expires_at = 7;
    bool is_active = 8;
    google.protobuf.Timestamp created = 9;
    repeated MembershipFeatureModel features = 10;
    string status = 11; // Trial/Active/Expired/Inactive - for frontend compatibility
    int32 days_remaining = 12; // Days until expiration - calculated field
}

message MembershipFeatureModel
{
    int64 product_id = 1;
    string product_name = 2;
    int32 quantity = 3;
    google.protobuf.Timestamp expires_at = 4;
    bool is_active = 5;
}

// GetAll Query
message GetAllClubMembershipsRequest
{
    google.protobuf.Int64Value user_id = 1;
    google.protobuf.Int64Value package_id = 2;
    google.protobuf.BoolValue is_active = 3;
    google.protobuf.BoolValue is_expired = 4;
    int32 page_index = 5;
    int32 page_size = 6;
}

message GetAllClubMembershipsResponse
{
    messages.MetaData meta_data = 1;
    repeated ClubMembershipModel models = 2;
}

message ClubMembershipModel
{
    int64 id = 1;
    int64 user_id = 2;
    string user_name = 3;
    int64 package_id = 4;
    string package_name = 5;
    string activation_code = 6;
    google.protobuf.Timestamp activated_at = 7;
    google.protobuf.Timestamp expires_at = 8;
    bool is_active = 9;
    bool is_expired = 10;
    google.protobuf.Timestamp created = 11;
}

// GetHistory Query
message GetClubMembershipHistoryRequest
{
    messages.PaginationState pagination_state = 1;
    google.protobuf.StringValue sort_by = 2;
    GetClubMembershipHistoryFilter filter = 3;
}

message GetClubMembershipHistoryFilter
{
    google.protobuf.Int64Value user_id = 1;
    google.protobuf.Int64Value club_membership_id = 2;
    google.protobuf.Int32Value action = 3;
    google.protobuf.Timestamp created_from = 4;
    google.protobuf.Timestamp created_to = 5;
}

message GetClubMembershipHistoryResponse
{
    messages.MetaData meta_data = 1;
    repeated ClubMembershipHistoryModel models = 2;
}

message ClubMembershipHistoryModel
{
    int64 id = 1;
    int64 club_membership_id = 2;
    int64 user_id = 3;
    string user_name = 4;
    bool old_is_active = 5;
    bool new_is_active = 6;
    google.protobuf.Int64Value old_initial_contribution = 7;
    google.protobuf.Int64Value new_initial_contribution = 8;
    int32 action = 9; // ClubMembershipAction enum
    string performed_by = 10;
    string reason = 11;
    google.protobuf.Timestamp created_at = 12;
}

// GetClubStatistics Query
message GetClubStatisticsRequest
{
    // Empty - returns overall club statistics
}

message GetClubStatisticsResponse
{
    int32 total_members = 1;
    int32 active_members = 2;
    int32 inactive_members = 3;
    int32 expired_members = 4;
    double active_percentage = 5;
    repeated PackageLevelDistribution package_distribution = 6;
    repeated MonthlyMembershipTrend monthly_trend = 7;
    int64 total_revenue = 8;
    double average_membership_duration_days = 9;
    int32 expiring_soon_count = 10; // Expiring in next 30 days
}

message PackageLevelDistribution
{
    int64 package_id = 1;
    string package_name = 2;
    int32 member_count = 3;
    double percentage = 4;
}

message MonthlyMembershipTrend
{
    string month = 1; // Format: "2025-11"
    int32 activations = 2;
    int32 expirations = 3;
    int32 net_change = 4;
}

// GetUserClubFeatures Query
message GetUserClubFeaturesRequest
{
    int64 user_id = 1;
}

message GetUserClubFeaturesResponse
{
    repeated UserClubFeatureModel features = 1;
}

message UserClubFeatureModel
{
    int64 id = 1;
    int64 user_id = 2;
    int64 club_membership_id = 3;
    int64 club_feature_id = 4;
    string feature_title = 5;
    string feature_description = 6;
    bool is_active = 7;
    google.protobuf.Timestamp granted_at = 8;
    string notes = 9;
    int32 sort_order = 10;
    google.protobuf.Timestamp created_at = 11;
}

// ToggleUserClubFeature Command
message ToggleUserClubFeatureRequest
{
    int64 user_id = 1;
    int64 club_feature_id = 2;
    bool is_active = 3;
}

message ToggleUserClubFeatureResponse
{
    bool success = 1;
    string message = 2;
    google.protobuf.Int64Value user_club_feature_id = 3;
    google.protobuf.BoolValue is_active = 4;
}

// AcceptClubMembershipContract Command - امضای قرارداد باشگاه مشتریان
message AcceptClubMembershipContractRequest
{
    int64 user_id = 1;
    string otp_code = 2;
    string sign_guid = 3;
    string contract_html = 4;
}

message AcceptClubMembershipContractResponse
{
    bool success = 1;
    string message = 2;
    int64 contract_id = 3;
}
