package roomapi

import walletv1 "hyapp.local/api/proto/wallet/v1"

type resourceData struct {
	ResourceID        int64    `json:"resource_id"`
	ResourceCode      string   `json:"resource_code"`
	ResourceType      string   `json:"resource_type"`
	Name              string   `json:"name"`
	Status            string   `json:"status"`
	Grantable         bool     `json:"grantable"`
	GrantStrategy     string   `json:"grant_strategy"`
	WalletAssetType   string   `json:"wallet_asset_type,omitempty"`
	WalletAssetAmount int64    `json:"wallet_asset_amount,omitempty"`
	PriceType         string   `json:"price_type,omitempty"`
	CoinPrice         int64    `json:"coin_price,omitempty"`
	GiftPointAmount   int64    `json:"gift_point_amount,omitempty"`
	UsageScopes       []string `json:"usage_scopes"`
	AssetURL          string   `json:"asset_url"`
	PreviewURL        string   `json:"preview_url"`
	AnimationURL      string   `json:"animation_url"`
	MetadataJSON      string   `json:"metadata_json"`
	SortOrder         int32    `json:"sort_order"`
	CreatedAtMS       int64    `json:"created_at_ms"`
	UpdatedAtMS       int64    `json:"updated_at_ms"`
}

type giftConfigData struct {
	GiftID           string       `json:"gift_id"`
	ResourceID       int64        `json:"resource_id"`
	Resource         resourceData `json:"resource"`
	Status           string       `json:"status"`
	Name             string       `json:"name"`
	SortOrder        int32        `json:"sort_order"`
	PresentationJSON string       `json:"presentation_json"`
	PriceVersion     string       `json:"price_version"`
	GiftTypeCode     string       `json:"gift_type_code"`
	// VIPLevel 是后台明确绑定的礼物等级，不使用价格、排序或名称在客户端推断。
	VIPLevel          int32    `json:"vip_level"`
	CPRelationType    string   `json:"cp_relation_type,omitempty"`
	ChargeAssetType   string   `json:"charge_asset_type"`
	CoinPrice         int64    `json:"coin_price"`
	GiftPointAmount   int64    `json:"gift_point_amount"`
	HeatValue         int64    `json:"heat_value"`
	EffectiveFromMS   int64    `json:"effective_from_ms"`
	EffectiveToMS     int64    `json:"effective_to_ms"`
	EffectTypes       []string `json:"effect_types"`
	CreatedAtMS       int64    `json:"created_at_ms"`
	UpdatedAtMS       int64    `json:"updated_at_ms"`
	RegionIDs         []int64  `json:"region_ids"`
	Source            string   `json:"source,omitempty"`
	EntitlementID     string   `json:"entitlement_id,omitempty"`
	OwnedQuantity     int64    `json:"owned_quantity,omitempty"`
	RemainingQuantity int64    `json:"remaining_quantity,omitempty"`
}

type giftTypeConfigData struct {
	TypeCode  string `json:"type_code"`
	Name      string `json:"name"`
	TabKey    string `json:"tab_key"`
	Status    string `json:"status"`
	SortOrder int32  `json:"sort_order"`
}

func giftFromProto(gift *walletv1.GiftConfig) giftConfigData {
	if gift == nil {
		return giftConfigData{}
	}
	return giftConfigData{
		GiftID:           gift.GetGiftId(),
		ResourceID:       gift.GetResourceId(),
		Resource:         resourceFromProto(gift.GetResource()),
		Status:           gift.GetStatus(),
		Name:             gift.GetName(),
		SortOrder:        gift.GetSortOrder(),
		PresentationJSON: gift.GetPresentationJson(),
		PriceVersion:     gift.GetPriceVersion(),
		GiftTypeCode:     gift.GetGiftTypeCode(),
		VIPLevel:         gift.GetVipLevel(),
		CPRelationType:   gift.GetCpRelationType(),
		ChargeAssetType:  gift.GetChargeAssetType(),
		CoinPrice:        gift.GetCoinPrice(),
		GiftPointAmount:  gift.GetGiftPointAmount(),
		HeatValue:        gift.GetHeatValue(),
		EffectiveFromMS:  gift.GetEffectiveFromMs(),
		EffectiveToMS:    gift.GetEffectiveToMs(),
		EffectTypes:      gift.GetEffectTypes(),
		CreatedAtMS:      gift.GetCreatedAtMs(),
		UpdatedAtMS:      gift.GetUpdatedAtMs(),
		RegionIDs:        gift.GetRegionIds(),
	}
}

func giftTypeFromProto(item *walletv1.GiftTypeConfig) giftTypeConfigData {
	if item == nil {
		return giftTypeConfigData{}
	}
	return giftTypeConfigData{
		TypeCode:  item.GetTypeCode(),
		Name:      item.GetName(),
		TabKey:    item.GetTabKey(),
		Status:    item.GetStatus(),
		SortOrder: item.GetSortOrder(),
	}
}

func resourceFromProto(item *walletv1.Resource) resourceData {
	if item == nil {
		return resourceData{}
	}
	return resourceData{
		ResourceID:        item.GetResourceId(),
		ResourceCode:      item.GetResourceCode(),
		ResourceType:      item.GetResourceType(),
		Name:              item.GetName(),
		Status:            item.GetStatus(),
		Grantable:         item.GetGrantable(),
		GrantStrategy:     item.GetGrantStrategy(),
		WalletAssetType:   item.GetWalletAssetType(),
		WalletAssetAmount: item.GetWalletAssetAmount(),
		PriceType:         item.GetPriceType(),
		CoinPrice:         item.GetCoinPrice(),
		GiftPointAmount:   item.GetGiftPointAmount(),
		UsageScopes:       item.GetUsageScopes(),
		AssetURL:          item.GetAssetUrl(),
		PreviewURL:        item.GetPreviewUrl(),
		AnimationURL:      item.GetAnimationUrl(),
		MetadataJSON:      item.GetMetadataJson(),
		SortOrder:         item.GetSortOrder(),
		CreatedAtMS:       item.GetCreatedAtMs(),
		UpdatedAtMS:       item.GetUpdatedAtMs(),
	}
}
