package wallet

import (
	"context"
	"time"

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

const maxAgencyHostGiftStatsRange = 366 * 24 * time.Hour

func (s *Service) GetAgencyHostGiftStats(ctx context.Context, appCode string, query ledger.AgencyHostGiftStatsQuery) (ledger.AgencyHostGiftStats, error) {
	if s.repository == nil {
		return ledger.AgencyHostGiftStats{}, xerr.New(xerr.Unavailable, "wallet repository is not configured")
	}
	if len(query.HostUserIDs) > 1000 || query.AgencyUserID < 0 || query.StartAtMS <= 0 || query.EndAtMS <= query.StartAtMS {
		return ledger.AgencyHostGiftStats{}, xerr.New(xerr.InvalidArgument, "agency host gift stats range is invalid")
	}
	for _, userID := range query.HostUserIDs {
		if userID <= 0 {
			return ledger.AgencyHostGiftStats{}, xerr.New(xerr.InvalidArgument, "host_user_ids is invalid")
		}
	}
	if query.EndAtMS-query.StartAtMS > int64(maxAgencyHostGiftStatsRange/time.Millisecond) {
		return ledger.AgencyHostGiftStats{}, xerr.New(xerr.InvalidArgument, "agency host gift stats range is too large")
	}
	ctx = appcode.WithContext(ctx, appcode.Normalize(appCode))
	return s.repository.GetAgencyHostGiftStats(ctx, query)
}
