syntax = "proto3";

package activity;

option go_package = "github.com/echofi-ai/schema/v2/types/activity";


import "activity/listening_history.proto";
import "activity/user_favorite.proto";
import "activity/referral.proto";
import "activity/rate_param.proto";
import "activity/listening_session.proto";
import "activity/user_points.proto";
import "activity/rewards.proto";
import "activity/user_rewards.proto";
import "activity/user_referral_rewards.proto";

import "music/track.proto";
import "user/user.proto";

// ActivityService defines the gRPC service for activity operations
service ActivityService {


  // GetUserFavorite retrieves a user favorite by ID
  rpc GetUserFavorite(GetUserFavoriteRequest) returns (GetUserFavoriteResponse) {}

  // ListUserFavorites retrieves user favorites with pagination
  rpc ListUserFavorites(ListUserFavoritesRequest) returns (ListUserFavoritesResponse) {}

  // AddUserFavorite adds a new favorite for the authenticated user
  rpc AddUserFavorite(AddUserFavoriteRequest) returns (AddUserFavoriteResponse) {}

  // RemoveUserFavorite removes a favorite for the authenticated user
  rpc RemoveUserFavorite(RemoveUserFavoriteRequest) returns (RemoveUserFavoriteResponse) {}

  // GetRateParam retrieves a rate parameter by ID
  rpc GetRateParam(GetRateParamRequest) returns (GetRateParamResponse) {}

  // ListRateParams retrieves rate parameters with pagination
  rpc ListRateParams(ListRateParamsRequest) returns (ListRateParamsResponse) {}

  // GetListeningHistory retrieves listening history by ID
  rpc GetListeningHistory(GetListeningHistoryRequest) returns (GetListeningHistoryResponse) {}

  // ListListeningHistory retrieves listening history with pagination
  rpc ListListeningHistory(ListListeningHistoryRequest) returns (ListListeningHistoryResponse) {}

  // GetListeningSession retrieves a listening session by ID
  rpc GetListeningSession(GetListeningSessionRequest) returns (GetListeningSessionResponse) {}

  // ListListeningSessions retrieves listening sessions with pagination
  rpc ListListeningSessions(ListListeningSessionsRequest) returns (ListListeningSessionsResponse) {}

  // GetUserPoints retrieves user points by user ID
  rpc GetUserPoints(GetUserPointsRequest) returns (GetUserPointsResponse) {}

  // ListUserPoints retrieves user points with pagination
  rpc ListUserPoints(ListUserPointsRequest) returns (ListUserPointsResponse) {}

  // GetUnprocessedListeningRecords retrieves unprocessed listening sessions for authenticated user
  rpc GetUnprocessedListeningRecords(GetUnprocessedListeningRecordsRequest) returns (GetUnprocessedListeningRecordsResponse) {}

  // GetUnprocessedListeningDuration calculates total duration of unprocessed listening sessions for authenticated user
  rpc GetUnprocessedListeningDuration(GetUnprocessedListeningDurationRequest) returns (GetUnprocessedListeningDurationResponse) {}

  // SetReferrer sets another user as the authenticated user's referrer
  rpc SetReferrer(SetReferrerRequest) returns (SetReferrerResponse) {}

  // GetUserReferralRewards retrieves referral rewards earned by the authenticated user
  rpc GetUserReferralRewards(GetUserReferralRewardsRequest) returns (GetUserReferralRewardsResponse) {}

  // ListUserReferees retrieves people referred by the authenticated user
  rpc ListUserReferees(ListUserRefereesRequest) returns (ListUserRefereesResponse) {}

  // GetUserRewards retrieves all rewards for the authenticated user
  rpc GetUserRewards(GetUserRewardsRequest) returns (GetUserRewardsResponse) {}

  // ListRewards retrieves available rewards (public endpoint)
  rpc ListRewards(ListRewardsRequest) returns (ListRewardsResponse) {}

  // GetLeaderboard retrieves the top 100 users with their points and referral points
  rpc GetLeaderboard(GetLeaderboardRequest) returns (GetLeaderboardResponse) {}

  // GetUserRank gets a user's rank by combined points
  rpc GetUserRank(UserRankRequest) returns (UserRankResponse) {}

}



// User Favorites requests and responses
message GetUserFavoriteRequest {
  int32 favorite_id = 1;
}

message GetUserFavoriteResponse {
  UserFavorite favorite = 1;
}

message ListUserFavoritesRequest {
  int32 offset = 1;
  int32 limit = 2;
  // user_id removed - will use authenticated user from context
  optional string item_type = 3; // Filter by item type (track, album, artist)
}

message ListUserFavoritesResponse {
  repeated UserFavorite favorites = 1;
  int64 total = 2;
  int32 offset = 3;
  int32 limit = 4;
}

// AddUserFavorite requests and responses
message AddUserFavoriteRequest {
  // Item type (track, album, artist)
  string item_type = 1;
  // Item ID (references track_id, album_id, or artist_id depending on item_type)
  int32 item_id = 2;
}

message AddUserFavoriteResponse {
  // The created favorite
  UserFavorite favorite = 1;
  // Success indicator
  bool success = 2;
  // Success message
  string message = 3;
}

// RemoveUserFavorite requests and responses
message RemoveUserFavoriteRequest {
  // Item type (track, album, artist)
  string item_type = 1;
  // Item ID (references track_id, album_id, or artist_id depending on item_type)
  int32 item_id = 2;
}

message RemoveUserFavoriteResponse {
  // Success indicator
  bool success = 1;
  // Success message
  string message = 2;
}

// Rate Parameter requests and responses
message GetRateParamRequest {
  int32 param_id = 1;
}

message GetRateParamResponse {
  RateParam param = 1;
}

message ListRateParamsRequest {
  int32 offset = 1;
  int32 limit = 2;
  optional bool is_active = 3; // Filter by active status
}

message ListRateParamsResponse {
  repeated RateParam params = 1;
  int64 total = 2;
  int32 offset = 3;
  int32 limit = 4;
}

// Listening Session requests and responses
message GetListeningSessionRequest {
  string id = 1;
}

message GetListeningSessionResponse {
  ListeningSession session = 1;
}

message ListListeningSessionsRequest {
  int32 offset = 1;
  int32 limit = 2;
  // user_id removed - will use authenticated user from context
  optional string status = 3;  // Filter by status
}

// ListeningSessionWithTrack combines session data with track metadata
message ListeningSessionWithTrack {
  // Listening session data
  ListeningSession session = 1;

  // Track metadata (track_id, title, image_url, source, url, artist_id, artist_name)
  message TrackMetadata {
    int32 track_id = 1;
    string title = 2;
    string image_url = 3;
    string source = 4;
    string url = 5;
    int32 artist_id = 6;
    string artist_name = 7;
  }

  TrackMetadata track = 2;
}

message ListListeningSessionsResponse {
  repeated ListeningSessionWithTrack sessions = 1;
  int64 total = 2;
  int32 offset = 3;
  int32 limit = 4;
}

// Listening History requests and responses
message GetListeningHistoryRequest {
  int32 history_id = 1;
}

message GetListeningHistoryResponse {
  ListeningHistory history = 1;
}

message ListListeningHistoryRequest {
  int32 offset = 1;
  int32 limit = 2;
  // user_id removed - will use authenticated user from context
  optional int32 track_id = 3; // Filter by track
}

// ListeningHistoryWithTrack combines listening history with track metadata
message ListeningHistoryWithTrack {
  // Listening history data
  ListeningHistory history = 1;

  // Track metadata (track_id, title, image_url, source, url, artist_id, artist_name)
  message TrackMetadata {
    int32 track_id = 1;
    string title = 2;
    string image_url = 3;
    string source = 4;
    string url = 5;
    int32 artist_id = 6;
    string artist_name = 7;
  }

  TrackMetadata track = 2;
}

message ListListeningHistoryResponse {
  repeated ListeningHistoryWithTrack histories = 1;
  int64 total = 2;
  int32 offset = 3;
  int32 limit = 4;
}

// User Points requests and responses
message GetUserPointsRequest {
  // No user_id needed - will use authenticated user from context
}

message GetUserPointsResponse {
  UserPoints points = 1;
}

message ListUserPointsRequest {
  int32 offset = 1;
  int32 limit = 2;
  optional string status = 3; // Filter by status
}

message ListUserPointsResponse {
  repeated UserPoints points = 1;
  int64 total = 2;
  int32 offset = 3;
  int32 limit = 4;
}

// Unprocessed Listening Records requests and responses
message GetUnprocessedListeningRecordsRequest {
  int32 offset = 1;
  int32 limit = 2;
}

message GetUnprocessedListeningRecordsResponse {
  repeated ListeningSession sessions = 1;
  int64 total = 2;
  int32 offset = 3;
  int32 limit = 4;
}

// Unprocessed Listening Duration requests and responses
message GetUnprocessedListeningDurationRequest {
  // No parameters needed - uses authenticated user ID from context
}

message GetUnprocessedListeningDurationResponse {
  // Total duration in seconds of all unprocessed listening sessions (excluding active sessions)
  int64 total_duration_seconds = 1;
  // Number of sessions included in the calculation
  int64 session_count = 2;
}

// SetReferrer requests and responses
message SetReferrerRequest {
  // Hex-encoded user ID of the referrer
  string referrer_id = 1;
}

message SetReferrerResponse {
  // Success indicator
  bool success = 1;
  // Success message
  string message = 2;
  // The created referral relationship
  Referral referral = 3;
  // Updated referrer statistics
  UserReferrals referrer_stats = 4;
}

// GetUserReferralRewards requests and responses
message GetUserReferralRewardsRequest {
  // No parameters needed - uses authenticated user ID from context
}

message GetUserReferralRewardsResponse {
  // User's referral rewards summary data from user_rrs table
  UserReferralRewards referral_rewards = 1;
}

// ListUserReferees requests and responses
message ListUserRefereesRequest {
  int32 offset = 1;
  int32 limit = 2;
}

message ListUserRefereesResponse {
  // List of users referred by the authenticated user
  repeated Referral referees = 1;
  int64 total = 2;
  int32 offset = 3;
  int32 limit = 4;
}

// GetUserRewards requests and responses
message GetUserRewardsRequest {
  int32 offset = 1;
  int32 limit = 2;
  optional bool claimed = 3; // Filter by claimed status
}

message GetUserRewardsResponse {
  // All rewards for the authenticated user
  repeated UserRewards rewards = 1;
  int64 total = 2;
  int32 offset = 3;
  int32 limit = 4;
}

// ListRewards requests and responses (public endpoint)
message ListRewardsRequest {
  int32 offset = 1;
  int32 limit = 2;
  optional bool is_active = 3; // Filter by active status
  optional string reward_type = 4; // Filter by reward type
}

message ListRewardsResponse {
  // Available rewards
  repeated Rewards rewards = 1;
  int64 total = 2;
  int32 offset = 3;
  int32 limit = 4;
}

// GetLeaderboard requests and responses
message GetLeaderboardRequest {
  // No parameters needed - returns top 100 users
}

// LeaderboardEntry represents a user's position in the leaderboard
message LeaderboardEntry {
  // User information
  user.User user = 1;

  // User's total points from user_points table
  double total_points = 2;

  // User's referral points from user_rrs table
  double referral_points = 3;

  // User's combined total (total_points + referral_points)
  double combined_total = 4;
}

message GetLeaderboardResponse {
  // Top 100 users ordered by combined total points
  repeated LeaderboardEntry entries = 1;

  // Total number of users in the leaderboard
  int64 total = 2;
}

// GetUserRank request and response
message UserRankRequest {
  // User ID to get rank for
  string user_id = 1;
}

message UserRankResponse {
  // User's rank position (1-based)
  int32 rank = 1;

  // User's total points
  double total_points = 2;

  // User's referral points
  double referral_points = 3;

  // User's combined total
  double combined_total = 4;
}
