package token_metadata

import (
	"testing"

	"github.com/blocto/solana-go-sdk/common"
	"github.com/blocto/solana-go-sdk/pkg/pointer"
	"github.com/stretchr/testify/assert"
)

func TokenStandardPtr(v TokenStandard) *TokenStandard {
	return &v
}

func CollectionPtr(v Collection) *Collection {
	return &v
}

func UsesPtr(v Uses) *Uses {
	return &v
}

func CollectionDetailsPtr(v CollectionDetails) *CollectionDetails {
	return &v
}

func TestMetadataDeserialize(t *testing.T) {
	type args struct {
		data []byte
	}
	tests := []struct {
		name string
		args args
		want Metadata
		err  error
	}{
		{
			args: args{
				data: []byte{0x4, 0xb5, 0x1f, 0xa2, 0x9b, 0x1d, 0xdb, 0xc2, 0x64, 0x1f, 0x10, 0xf9, 0xd3, 0xc9, 0x21, 0x39, 0x5a, 0x89, 0xb3, 0x44, 0x4, 0xb4, 0x98, 0xa5, 0xe9, 0x89, 0xd0, 0xc7, 0xa4, 0xe7, 0xb1, 0xf8, 0xc2, 0xeb, 0x17, 0x1a, 0x88, 0x2d, 0x89, 0x5e, 0x4e, 0xe6, 0x5a, 0x27, 0x14, 0xf, 0xd5, 0x76, 0x93, 0xd3, 0x6f, 0x8d, 0x73, 0x54, 0x6f, 0x18, 0xc6, 0x8b, 0x8c, 0xec, 0xe8, 0xc7, 0x8c, 0xfb, 0xef, 0x20, 0x0, 0x0, 0x0, 0x44, 0x65, 0x67, 0x65, 0x6e, 0x20, 0x41, 0x70, 0x65, 0x20, 0x23, 0x31, 0x38, 0x32, 0x39, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x44, 0x41, 0x50, 0x45, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc8, 0x0, 0x0, 0x0, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x61, 0x72, 0x77, 0x65, 0x61, 0x76, 0x65, 0x2e, 0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 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
			},
			want: Metadata{
				Key:             4,
				UpdateAuthority: common.PublicKeyFromString("DC2mkgwhy56w3viNtHDjJQmc7SGu2QX785bS4aexojwX"),
				Mint:            common.PublicKeyFromString("GphF2vTuzhwhLWBWWvD8y5QLCPp1aQC5EnzrWsnbiWPx"),
				Data: Data{
					Name:                 "Degen Ape #1829",
					Symbol:               "DAPE",
					Uri:                  "https://arweave.net/6ePcw3g2wI-kJRFftU_dQDaP70myKpMxSVEDdGlbNe0",
					SellerFeeBasisPoints: 420,
					Creators: &[]Creator{
						{
							Address:  common.PublicKeyFromString("9BKWqDHfHZh9j39xakYVMdr6hXmCLHH5VfCpeq2idU9L"),
							Verified: false,
							Share:    39,
						},
						{
							Address:  common.PublicKeyFromString("9FYsKrNuEweb55Wa2jaj8wTKYDBvuCG3huhakEj96iN9"),
							Verified: false,
							Share:    25,
						},
						{
							Address:  common.PublicKeyFromString("HNGVuL5kqjDehw7KR63w9gxow32sX6xzRNgLb8GkbwCM"),
							Verified: false,
							Share:    25,
						},
						{
							Address:  common.PublicKeyFromString("7FzXBBPjzrNJbm9MrZKZcyvP3ojVeYPUG2XkBPVZvuBu"),
							Verified: false,
							Share:    10,
						},
						{
							Address:  common.PublicKeyFromString("DC2mkgwhy56w3viNtHDjJQmc7SGu2QX785bS4aexojwX"),
							Verified: true,
							Share:    1,
						},
					},
				},
				PrimarySaleHappened: true,
				IsMutable:           false,
				EditionNonce:        pointer.Get[uint8](255),
				TokenStandard:       nil,
				Collection:          nil,
				Uses:                nil,
				CollectionDetails:   nil,
			},
			err: nil,
		},
		{
			args: args{
				data: []byte{
					// key
					4,
					// update authority
					184, 131, 145, 32, 192, 144, 221, 205, 70, 126, 197, 6, 149, 25, 73, 171, 248, 42, 169, 200, 154, 103, 169, 63, 68, 52, 140, 30, 47, 142, 196, 132,
					// mint
					66, 109, 180, 44, 2, 48, 200, 27, 141, 156, 146, 126, 161, 200, 32, 83, 229, 100, 28, 224, 254, 7, 133, 157, 210, 201, 190, 216, 69, 70, 168, 118,
					// name (max length 32)
					32, 0, 0, 0,
					66, 111, 114, 121, 111, 107, 117, 32, 68, 114, 97, 103, 111, 110, 122, 32, 35, 50, 56, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
					// symbol (max length 10)
					10, 0, 0, 0, 66, 79, 82, 89, 79, 75, 85, 0, 0, 0,
					// url (max length 200)
					200, 0, 0, 0,
					104, 116, 116, 112, 115, 58, 47, 47, 97, 114, 119, 101, 97, 118, 101, 46, 110, 101, 116, 47, 95, 45, 77, 87, 107, 78, 68, 122, 79, 86, 65, 103, 117, 89, 119, 121, 106, 57, 99, 107, 89, 56, 70, 84, 89, 71, 99, 73, 79, 101, 117, 110, 110, 111, 87, 99, 106, 115, 56, 50, 79, 69, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
					// seller fee basis (u16)
					244, 1,
					// creators
					// pointer
					1,
					// len
					1, 0, 0, 0,
					// creator1.address
					184, 131, 145, 32, 192, 144, 221, 205, 70, 126, 197, 6, 149, 25, 73, 171, 248, 42, 169, 200, 154, 103, 169, 63, 68, 52, 140, 30, 47, 142, 196, 132,
					// creator1.verified
					1,
					// creator1.share
					100,
					// primary sale
					1,
					// is mutable
					1,
					// pointer of edition nonce
					1, 254,
					// rest
					250, 126, 21, 122, 182, 46, 82, 216, 99, 172, 146, 238, 124, 159, 181, 237, 237, 126, 208, 20, 225, 94, 140, 55, 2, 78, 196, 132, 0, 100, 1, 1, 1, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				},
			},
			want: Metadata{
				Key:             4,
				UpdateAuthority: common.PublicKeyFromString("DRGNjvBvnXNiQz9dTppGk1tAsVxtJsvhEmojEfBU3ezf"),
				Mint:            common.PublicKeyFromString("5UJvyhCsum71CK9ERrH8hcLw6aWzh4SjwWXToquWdWR3"),
				Data: Data{
					Name:                 "Boryoku Dragonz #282",
					Symbol:               "BORYOKU",
					Uri:                  "https://arweave.net/_-MWkNDzOVAguYwyj9ckY8FTYGcIOeunnoWcjs82OEo",
					SellerFeeBasisPoints: 500,
					Creators: &[]Creator{
						{
							Address:  common.PublicKeyFromString("DRGNjvBvnXNiQz9dTppGk1tAsVxtJsvhEmojEfBU3ezf"),
							Verified: true,
							Share:    100,
						},
					},
				},
				PrimarySaleHappened: true,
				IsMutable:           true,
				EditionNonce:        pointer.Get[uint8](254),
				TokenStandard:       nil,
				Collection:          nil,
				Uses:                nil,
				CollectionDetails:   nil,
			},
			err: nil,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := MetadataDeserialize(tt.args.data)
			assert.Equal(t, err, tt.err)
			assert.Equal(t, tt.want, got)
		})
	}
}
