package grpc

import (
	"context"

	walletv1 "hyapp.local/api/proto/wallet/v1"
	"hyapp/pkg/appcode"
	"hyapp/pkg/xerr"
	"hyapp/services/wallet-service/internal/domain/ledger"
)

// GetActiveHostSalaryPolicy 读取当前生效的主播工资政策，供 gateway 按当前主播区域展示平台规则。
func (s *Server) GetActiveHostSalaryPolicy(ctx context.Context, req *walletv1.GetActiveHostSalaryPolicyRequest) (*walletv1.GetActiveHostSalaryPolicyResponse, error) {
	ctx = appcode.WithContext(ctx, req.GetAppCode())
	policy, found, err := s.svc.GetActiveHostSalaryPolicy(ctx, req.GetAppCode(), req.GetRegionId(), req.GetSettlementMode(), req.GetSettlementTriggerMode(), req.GetNowMs())
	if err != nil {
		return nil, xerr.ToGRPCError(err)
	}
	response := &walletv1.GetActiveHostSalaryPolicyResponse{Found: found}
	if found {
		response.Policy = hostSalaryPolicyToProto(policy)
	}
	return response, nil
}

// GetHostSalaryProgress 读取主播当前工资周期钻石累计，gateway 用它计算工资政策等级进度。
func (s *Server) GetHostSalaryProgress(ctx context.Context, req *walletv1.GetHostSalaryProgressRequest) (*walletv1.GetHostSalaryProgressResponse, error) {
	ctx = appcode.WithContext(ctx, req.GetAppCode())
	progress, err := s.svc.GetHostSalaryProgress(ctx, req.GetAppCode(), req.GetHostUserId(), req.GetCycleKey(), req.GetNowMs())
	if err != nil {
		return nil, xerr.ToGRPCError(err)
	}
	return &walletv1.GetHostSalaryProgressResponse{Progress: hostSalaryProgressToProto(progress)}, nil
}

func hostSalaryProgressToProto(progress ledger.HostSalaryProgress) *walletv1.HostSalaryProgress {
	return &walletv1.HostSalaryProgress{
		HostUserId:        progress.HostUserID,
		CycleKey:          progress.CycleKey,
		RegionId:          progress.RegionID,
		AgencyOwnerUserId: progress.AgencyOwnerUserID,
		TotalDiamonds:     progress.TotalDiamonds,
		GiftDiamondTotal:  progress.GiftDiamondTotal,
		UpdatedAtMs:       progress.UpdatedAtMS,
	}
}

func hostSalaryPolicyToProto(policy ledger.HostSalaryPolicy) *walletv1.HostSalaryPolicy {
	levels := make([]*walletv1.HostSalaryPolicyLevel, 0, len(policy.Levels))
	for _, level := range policy.Levels {
		levels = append(levels, hostSalaryPolicyLevelToProto(level))
	}
	return &walletv1.HostSalaryPolicy{
		PolicyId:                 policy.PolicyID,
		Name:                     policy.Name,
		RegionId:                 policy.RegionID,
		Status:                   policy.Status,
		SettlementMode:           policy.SettlementMode,
		SettlementTriggerMode:    policy.SettlementTriggerMode,
		GiftCoinToDiamondRatio:   policy.GiftCoinToDiamondRatio,
		ResidualDiamondToUsdRate: policy.ResidualDiamondToUSDRate,
		EffectiveFromMs:          policy.EffectiveFromMs,
		EffectiveToMs:            policy.EffectiveToMs,
		Levels:                   levels,
	}
}

func hostSalaryPolicyLevelToProto(level ledger.HostSalaryPolicyLevel) *walletv1.HostSalaryPolicyLevel {
	return &walletv1.HostSalaryPolicyLevel{
		LevelNo:              int32(level.LevelNo),
		RequiredDiamonds:     level.RequiredDiamonds,
		HostSalaryUsdMinor:   level.HostSalaryUSDMinor,
		HostCoinReward:       level.HostCoinReward,
		AgencySalaryUsdMinor: level.AgencySalaryUSDMinor,
		Status:               level.Status,
		SortOrder:            int32(level.SortOrder),
	}
}
