package wallet

import (
	"context"
	"time"

	"hyapp/pkg/appcode"
	"hyapp/pkg/xerr"
	"hyapp/services/wallet-service/internal/domain/ledger"
)

const maxHostRevenueStatsRange = 366 * 24 * time.Hour

// GetHostRevenueStats 聚合钱包 owner 内的 POINT 收益/兑换和去重送礼人，不让 gateway 扫账本。
func (s *Service) GetHostRevenueStats(ctx context.Context, appCode string, query ledger.HostRevenueStatsQuery) (ledger.HostRevenueStats, error) {
	if s.repository == nil {
		return ledger.HostRevenueStats{}, xerr.New(xerr.Unavailable, "wallet repository is not configured")
	}
	if query.HostUserID <= 0 || query.RegionID < 0 || query.StartAtMS <= 0 || query.EndAtMS <= query.StartAtMS {
		return ledger.HostRevenueStats{}, xerr.New(xerr.InvalidArgument, "host revenue stats range is invalid")
	}
	if query.EndAtMS-query.StartAtMS > int64(maxHostRevenueStatsRange/time.Millisecond) {
		return ledger.HostRevenueStats{}, xerr.New(xerr.InvalidArgument, "host revenue stats range is too large")
	}
	ctx = appcode.WithContext(ctx, appcode.Normalize(appCode))
	return s.repository.GetHostRevenueStats(ctx, query)
}
