package grpc

import (
	"testing"

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

func TestResourceCommandDefaultsManagerGrantDisabled(t *testing.T) {
	command := resourceCommandFromCreate(&walletv1.CreateResourceRequest{
		ResourceCode: "badge_default_manager_disabled",
		ResourceType: "badge",
		Name:         "Default Manager Disabled Badge",
		Status:       "active",
		Grantable:    true,
	})

	if command.ManagerGrantEnabled {
		t.Fatalf("manager grant should default to disabled")
	}
}

func TestUserResourceMapperExposesVerifiedPinnedGiftConfigs(t *testing.T) {
	item := userResourceToProto(resourcedomain.UserResourceEntitlement{
		EntitlementID: "ent-pinned", SourceSnapshotID: "rgs-pinned", ResourceID: 501,
		PinnedGiftConfigs: []resourcedomain.GiftConfig{{
			GiftID: "pinned-star", ResourceID: 501, Name: "Pinned Star V1", PriceVersion: "v1", CoinPrice: 7,
			RegionIDs: []int64{1}, Resource: resourcedomain.Resource{ResourceID: 501, ResourceType: resourcedomain.TypeGift},
		}},
	})
	if len(item.GetPinnedGiftConfigs()) != 1 || item.GetPinnedGiftConfigs()[0].GetGiftId() != "pinned-star" ||
		item.GetPinnedGiftConfigs()[0].GetPriceVersion() != "v1" || item.GetPinnedGiftConfigs()[0].GetRegionIds()[0] != 1 {
		t.Fatalf("pinned gift configs missing from entitlement proto: %+v", item)
	}
}

func TestResourceCommandKeepsExplicitManagerGrantEnabled(t *testing.T) {
	enabled := true
	command := resourceCommandFromCreate(&walletv1.CreateResourceRequest{
		ResourceCode:        "badge_manager_enabled",
		ResourceType:        "badge",
		Name:                "Manager Enabled Badge",
		Status:              "active",
		Grantable:           true,
		ManagerGrantEnabled: &enabled,
	})

	if !command.ManagerGrantEnabled {
		t.Fatalf("explicit manager grant enabled should be preserved")
	}
}
