package grpc

import (
	"context"

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

func (s *Server) GetAgencyHostGiftStats(ctx context.Context, req *walletv1.GetAgencyHostGiftStatsRequest) (*walletv1.GetAgencyHostGiftStatsResponse, error) {
	ctx = appcode.WithContext(ctx, req.GetAppCode())
	stats, err := s.svc.GetAgencyHostGiftStats(ctx, req.GetAppCode(), ledger.AgencyHostGiftStatsQuery{
		HostUserIDs: req.GetHostUserIds(), AgencyUserID: req.GetAgencyUserId(), StartAtMS: req.GetStartAtMs(), EndAtMS: req.GetEndAtMs(),
	})
	if err != nil {
		return nil, xerr.ToGRPCError(err)
	}
	hostStats := make([]*walletv1.AgencyHostRevenueStats, 0, len(stats.HostStats))
	for _, host := range stats.HostStats {
		hostStats = append(hostStats, &walletv1.AgencyHostRevenueStats{
			HostUserId: host.HostUserID,
			Stats: &walletv1.HostRevenueStats{
				DiamondEarnings:       host.Stats.DiamondEarnings,
				DiamondExchanged:      host.Stats.DiamondExchanged,
				GiftSenders:           host.Stats.GiftSenders,
				PointDiamondEarnings:  host.Stats.PointDiamondEarnings,
				PointDiamondExchanged: host.Stats.PointDiamondExchanged,
			},
		})
	}
	return &walletv1.GetAgencyHostGiftStatsResponse{Stats: &walletv1.AgencyHostGiftStats{
		GiftIncome:             stats.GiftIncome,
		PointDiamondHostIncome: stats.PointDiamondHostIncome, PointDiamondAgencyShareIncome: stats.PointDiamondAgencyShareIncome,
		GiftedHostCount: stats.GiftedHostCount,
	}, HostStats: hostStats}, nil
}
