// Package activityclient adapts activity-service registration reward RPCs for user-service.
package activityclient

import (
	"context"
	"time"

	"google.golang.org/grpc"
	activityv1 "hyapp.local/api/proto/activity/v1"
	userdomain "hyapp/services/user-service/internal/domain/user"
	authservice "hyapp/services/user-service/internal/service/auth"
)

type Client struct {
	registrationRewardClient activityv1.RegistrationRewardServiceClient
	growthLevelClient        activityv1.GrowthLevelServiceClient
}

func NewGRPC(conn grpc.ClientConnInterface) *Client {
	return &Client{
		registrationRewardClient: activityv1.NewRegistrationRewardServiceClient(conn),
		growthLevelClient:        activityv1.NewGrowthLevelServiceClient(conn),
	}
}

// IssueRegistrationReward 只发起注册奖励幂等命令；可领取判断和实际发放都在 activity-service。
func (c *Client) IssueRegistrationReward(ctx context.Context, command authservice.RegistrationRewardCommand) error {
	if c == nil || c.registrationRewardClient == nil {
		return nil
	}
	_, err := c.registrationRewardClient.IssueRegistrationReward(ctx, &activityv1.IssueRegistrationRewardRequest{
		Meta: &activityv1.RequestMeta{
			RequestId: command.RequestID,
			Caller:    "user-service",
			AppCode:   command.AppCode,
			SentAtMs:  time.Now().UnixMilli(),
		},
		UserId:    command.UserID,
		CommandId: command.CommandID,
	})
	return err
}

// IssueRegistrationLevelBadges 发起注册等级徽章发放；资源选择和钱包发放都在 activity-service。
func (c *Client) IssueRegistrationLevelBadges(ctx context.Context, command authservice.RegistrationLevelBadgeCommand) error {
	if c == nil || c.growthLevelClient == nil {
		return nil
	}
	_, err := c.growthLevelClient.IssueRegistrationLevelBadges(ctx, &activityv1.IssueRegistrationLevelBadgesRequest{
		Meta: &activityv1.RequestMeta{
			RequestId: command.RequestID,
			Caller:    "user-service",
			AppCode:   command.AppCode,
			SentAtMs:  time.Now().UnixMilli(),
		},
		UserId:    command.UserID,
		CommandId: command.CommandID,
	})
	return err
}

// GetUserLevelProfile 读取用户当前三条等级轨道，供 user-service 靓号池申请做准入校验。
func (c *Client) GetUserLevelProfile(ctx context.Context, appCode string, userID int64, requestID string) (userdomain.UserLevelProfile, error) {
	if c == nil || c.growthLevelClient == nil {
		return userdomain.UserLevelProfile{}, nil
	}
	resp, err := c.growthLevelClient.BatchGetUserLevelDisplayProfiles(ctx, &activityv1.BatchGetUserLevelDisplayProfilesRequest{
		Meta: &activityv1.RequestMeta{
			RequestId: requestID,
			Caller:    "user-service",
			AppCode:   appCode,
			SentAtMs:  time.Now().UnixMilli(),
		},
		UserIds: []int64{userID},
	})
	if err != nil {
		return userdomain.UserLevelProfile{}, err
	}
	profile := userdomain.UserLevelProfile{UserID: userID}
	for _, item := range resp.GetProfiles() {
		if item.GetUserId() != userID {
			continue
		}
		profile.WealthLevel = item.GetWealth().GetLevel()
		profile.GameLevel = item.GetGame().GetLevel()
		profile.CharmLevel = item.GetCharm().GetLevel()
		return profile, nil
	}
	return profile, nil
}
