package grpc

import (
	walletv1 "hyapp.local/api/proto/wallet/v1"
	resourcedomain "hyapp/services/wallet-service/internal/domain/resource"
)

func resourceCommandFromCreate(req *walletv1.CreateResourceRequest) resourcedomain.ResourceCommand {
	return resourcedomain.ResourceCommand{
		AppCode:             req.GetAppCode(),
		ResourceCode:        req.GetResourceCode(),
		ResourceType:        req.GetResourceType(),
		Name:                req.GetName(),
		Status:              req.GetStatus(),
		Grantable:           req.GetGrantable(),
		GrantStrategy:       req.GetGrantStrategy(),
		WalletAssetType:     req.GetWalletAssetType(),
		WalletAssetAmount:   req.GetWalletAssetAmount(),
		PriceType:           req.GetPriceType(),
		CoinPrice:           req.GetCoinPrice(),
		GiftPointAmount:     0,
		UsageScopes:         req.GetUsageScopes(),
		AssetURL:            req.GetAssetUrl(),
		PreviewURL:          req.GetPreviewUrl(),
		AnimationURL:        req.GetAnimationUrl(),
		MetadataJSON:        req.GetMetadataJson(),
		SortOrder:           req.GetSortOrder(),
		OperatorUserID:      req.GetOperatorUserId(),
		ManagerGrantEnabled: managerGrantEnabledOrDefault(req.ManagerGrantEnabled),
	}
}

func resourceCommandFromUpdate(req *walletv1.UpdateResourceRequest) resourcedomain.ResourceCommand {
	command := resourceCommandFromCreate(&walletv1.CreateResourceRequest{
		AppCode:             req.GetAppCode(),
		ResourceCode:        req.GetResourceCode(),
		ResourceType:        req.GetResourceType(),
		Name:                req.GetName(),
		Status:              req.GetStatus(),
		Grantable:           req.GetGrantable(),
		GrantStrategy:       req.GetGrantStrategy(),
		WalletAssetType:     req.GetWalletAssetType(),
		WalletAssetAmount:   req.GetWalletAssetAmount(),
		PriceType:           req.GetPriceType(),
		CoinPrice:           req.GetCoinPrice(),
		GiftPointAmount:     0,
		UsageScopes:         req.GetUsageScopes(),
		AssetUrl:            req.GetAssetUrl(),
		PreviewUrl:          req.GetPreviewUrl(),
		AnimationUrl:        req.GetAnimationUrl(),
		MetadataJson:        req.GetMetadataJson(),
		SortOrder:           req.GetSortOrder(),
		OperatorUserId:      req.GetOperatorUserId(),
		ManagerGrantEnabled: req.ManagerGrantEnabled,
	})
	command.ResourceID = req.GetResourceId()
	return command
}

func resourceGroupCommandFromCreate(req *walletv1.CreateResourceGroupRequest) resourcedomain.ResourceGroupCommand {
	return resourcedomain.ResourceGroupCommand{
		AppCode:        req.GetAppCode(),
		GroupCode:      req.GetGroupCode(),
		Name:           req.GetName(),
		Status:         req.GetStatus(),
		Description:    req.GetDescription(),
		SortOrder:      req.GetSortOrder(),
		Items:          groupItemInputs(req.GetItems()),
		OperatorUserID: req.GetOperatorUserId(),
	}
}

func resourceGroupCommandFromUpdate(req *walletv1.UpdateResourceGroupRequest) resourcedomain.ResourceGroupCommand {
	return resourcedomain.ResourceGroupCommand{
		AppCode:        req.GetAppCode(),
		GroupID:        req.GetGroupId(),
		GroupCode:      req.GetGroupCode(),
		Name:           req.GetName(),
		Status:         req.GetStatus(),
		Description:    req.GetDescription(),
		SortOrder:      req.GetSortOrder(),
		Items:          groupItemInputs(req.GetItems()),
		OperatorUserID: req.GetOperatorUserId(),
	}
}

func giftConfigCommandFromCreate(req *walletv1.CreateGiftConfigRequest) resourcedomain.GiftConfigCommand {
	return resourcedomain.GiftConfigCommand{
		AppCode:          req.GetAppCode(),
		GiftID:           req.GetGiftId(),
		ResourceID:       req.GetResourceId(),
		Status:           req.GetStatus(),
		Name:             req.GetName(),
		SortOrder:        req.GetSortOrder(),
		PresentationJSON: req.GetPresentationJson(),
		PriceVersion:     req.GetPriceVersion(),
		CoinPrice:        req.GetCoinPrice(),
		GiftPointAmount:  0,
		HeatValue:        req.GetHeatValue(),
		EffectiveAtMS:    req.GetEffectiveAtMs(),
		GiftTypeCode:     req.GetGiftTypeCode(),
		VIPLevel:         req.GetVipLevel(),
		CPRelationType:   req.GetCpRelationType(),
		ChargeAssetType:  req.GetChargeAssetType(),
		EffectiveFromMS:  req.GetEffectiveFromMs(),
		EffectiveToMS:    req.GetEffectiveToMs(),
		EffectTypes:      req.GetEffectTypes(),
		OperatorUserID:   req.GetOperatorUserId(),
		RegionIDs:        req.GetRegionIds(),
	}
}

func giftConfigCommandFromUpdate(req *walletv1.UpdateGiftConfigRequest) resourcedomain.GiftConfigCommand {
	return resourcedomain.GiftConfigCommand{
		AppCode:          req.GetAppCode(),
		GiftID:           req.GetGiftId(),
		ResourceID:       req.GetResourceId(),
		Status:           req.GetStatus(),
		Name:             req.GetName(),
		SortOrder:        req.GetSortOrder(),
		PresentationJSON: req.GetPresentationJson(),
		PriceVersion:     req.GetPriceVersion(),
		CoinPrice:        req.GetCoinPrice(),
		GiftPointAmount:  0,
		HeatValue:        req.GetHeatValue(),
		EffectiveAtMS:    req.GetEffectiveAtMs(),
		GiftTypeCode:     req.GetGiftTypeCode(),
		VIPLevel:         req.GetVipLevel(),
		CPRelationType:   req.GetCpRelationType(),
		ChargeAssetType:  req.GetChargeAssetType(),
		EffectiveFromMS:  req.GetEffectiveFromMs(),
		EffectiveToMS:    req.GetEffectiveToMs(),
		EffectTypes:      req.GetEffectTypes(),
		OperatorUserID:   req.GetOperatorUserId(),
		RegionIDs:        req.GetRegionIds(),
	}
}

func groupItemInputs(items []*walletv1.ResourceGroupItemInput) []resourcedomain.ResourceGroupItemInput {
	out := make([]resourcedomain.ResourceGroupItemInput, 0, len(items))
	for _, item := range items {
		if item == nil {
			continue
		}
		out = append(out, resourcedomain.ResourceGroupItemInput{
			ItemType:          item.GetItemType(),
			ResourceID:        item.GetResourceId(),
			WalletAssetType:   item.GetWalletAssetType(),
			WalletAssetAmount: item.GetWalletAssetAmount(),
			Quantity:          item.GetQuantity(),
			DurationMS:        item.GetDurationMs(),
			SortOrder:         item.GetSortOrder(),
		})
	}
	return out
}

func resourceShopItemInputs(items []*walletv1.ResourceShopItemInput) []resourcedomain.ResourceShopItemInput {
	out := make([]resourcedomain.ResourceShopItemInput, 0, len(items))
	for _, item := range items {
		if item == nil {
			continue
		}
		out = append(out, resourcedomain.ResourceShopItemInput{
			ShopItemID:      item.GetShopItemId(),
			ResourceID:      item.GetResourceId(),
			Status:          item.GetStatus(),
			DurationDays:    item.GetDurationDays(),
			CoinPrice:       item.GetCoinPrice(),
			EffectiveFromMS: item.GetEffectiveFromMs(),
			EffectiveToMS:   item.GetEffectiveToMs(),
			SortOrder:       item.GetSortOrder(),
		})
	}
	return out
}
