package quic

import (
	"bytes"
	"testing"
)

func TestExtractCryptoFrames_Interleaved(t *testing.T) {
	// PADDING, CRYPTO(offset=0,"abc"), PING, STREAM(len=2), CRYPTO(offset=3,"de")
	payload := []byte{
		0x00,
		0x06, 0x00, 0x03, 'a', 'b', 'c',
		0x01,
		0x0a, 0x01, 0x02, 'z', 'z',
		0x06, 0x03, 0x02, 'd', 'e',
	}

	frames, err := extractCryptoFrames(bytes.NewReader(payload))
	if err != nil {
		t.Fatalf("extractCryptoFrames() error = %v", err)
	}
	if len(frames) != 2 {
		t.Fatalf("extractCryptoFrames() len = %d, want 2", len(frames))
	}
	if frames[0].Offset != 0 || string(frames[0].Data) != "abc" {
		t.Fatalf("frame0 = %+v, want offset=0 data=abc", frames[0])
	}
	if frames[1].Offset != 3 || string(frames[1].Data) != "de" {
		t.Fatalf("frame1 = %+v, want offset=3 data=de", frames[1])
	}
}

func TestExtractCryptoFrames_UnsupportedFrame(t *testing.T) {
	// 0x20 is currently unsupported in this parser.
	_, err := extractCryptoFrames(bytes.NewReader([]byte{0x20}))
	if err == nil {
		t.Fatal("extractCryptoFrames() error = nil, want non-nil")
	}
}

func TestExtractCryptoFrames_UnknownAfterCrypto(t *testing.T) {
	// CRYPTO(offset=0,"abc"), then unknown frame type 0x20.
	payload := []byte{
		0x06, 0x00, 0x03, 'a', 'b', 'c',
		0x20,
	}
	frames, err := extractCryptoFrames(bytes.NewReader(payload))
	if err != nil {
		t.Fatalf("extractCryptoFrames() error = %v", err)
	}
	if len(frames) != 1 {
		t.Fatalf("extractCryptoFrames() len = %d, want 1", len(frames))
	}
	if frames[0].Offset != 0 || string(frames[0].Data) != "abc" {
		t.Fatalf("frame0 = %+v, want offset=0 data=abc", frames[0])
	}
}

func TestReadCryptoFrames_NonInitialHeader(t *testing.T) {
	// Short header packet marker should be rejected as non-initial.
	_, err := ReadCryptoFrames([]byte{0x40, 0x01, 0x02, 0x03, 0x04})
	if err == nil {
		t.Fatal("ReadCryptoFrames() error = nil, want non-nil")
	}
	if err.Error() != ErrNotInitialPacket.Error() {
		t.Fatalf("ReadCryptoFrames() error = %v, want %v", err, ErrNotInitialPacket)
	}
}

func TestCollectPacketNumberMaxGuesses_Prefer(t *testing.T) {
	got := collectPacketNumberMaxGuesses(&ReadCryptoFramesOptions{
		PacketNumberMaxGuesses: []int64{1, 2, 3, 2},
		PreferredPNMax:         2,
		HasPreferredPNMax:      true,
	})
	want := []int64{2, 1, 3}
	if len(got) != len(want) {
		t.Fatalf("collectPacketNumberMaxGuesses() len=%d, want=%d", len(got), len(want))
	}
	for i := range want {
		if got[i] != want[i] {
			t.Fatalf("collectPacketNumberMaxGuesses()[%d]=%d, want=%d (got=%v)", i, got[i], want[i], got)
		}
	}
}

func TestReadCryptoFramesWithOptions_HintRoundTrip(t *testing.T) {
	// Example packet from quic.xargs.org client Initial, padded to 1200 bytes.
	packet := make([]byte, 1200)
	clientInitial := []byte{
		0xcd, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
		0x06, 0x07, 0x05, 0x63, 0x5f, 0x63, 0x69, 0x64, 0x00, 0x41, 0x03, 0x98,
		0x1c, 0x36, 0xa7, 0xed, 0x78, 0x71, 0x6b, 0xe9, 0x71, 0x1b, 0xa4, 0x98,
		0xb7, 0xed, 0x86, 0x84, 0x43, 0xbb, 0x2e, 0x0c, 0x51, 0x4d, 0x4d, 0x84,
		0x8e, 0xad, 0xcc, 0x7a, 0x00, 0xd2, 0x5c, 0xe9, 0xf9, 0xaf, 0xa4, 0x83,
		0x97, 0x80, 0x88, 0xde, 0x83, 0x6b, 0xe6, 0x8c, 0x0b, 0x32, 0xa2, 0x45,
		0x95, 0xd7, 0x81, 0x3e, 0xa5, 0x41, 0x4a, 0x91, 0x99, 0x32, 0x9a, 0x6d,
		0x9f, 0x7f, 0x76, 0x0d, 0xd8, 0xbb, 0x24, 0x9b, 0xf3, 0xf5, 0x3d, 0x9a,
		0x77, 0xfb, 0xb7, 0x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0, 0x99,
	}
	copy(packet, clientInitial)

	firstHint := &DecryptSuccessHint{}
	frames, err := ReadCryptoFramesWithOptions(packet, &ReadCryptoFramesOptions{
		TryServerSecret: true,
		SuccessHint:     firstHint,
	})
	if err != nil {
		t.Fatalf("ReadCryptoFramesWithOptions(first) error=%v", err)
	}
	if len(frames) == 0 {
		t.Fatal("ReadCryptoFramesWithOptions(first) got no frames, want > 0")
	}
	if firstHint.SecretLabel != initialSecretLabelClientIn {
		t.Fatalf("firstHint.SecretLabel=%q, want=%q", firstHint.SecretLabel, initialSecretLabelClientIn)
	}
	if len(firstHint.ConnectionID) == 0 {
		t.Fatal("firstHint.ConnectionID empty, want non-empty")
	}

	secondHint := &DecryptSuccessHint{}
	frames, err = ReadCryptoFramesWithOptions(packet, &ReadCryptoFramesOptions{
		TryServerSecret:       true,
		PreferredConnectionID: firstHint.ConnectionID,
		PreferredSecretLabel:  firstHint.SecretLabel,
		PreferredPNMax:        firstHint.PacketNumberMax,
		HasPreferredPNMax:     true,
		SuccessHint:           secondHint,
	})
	if err != nil {
		t.Fatalf("ReadCryptoFramesWithOptions(second) error=%v", err)
	}
	if len(frames) == 0 {
		t.Fatal("ReadCryptoFramesWithOptions(second) got no frames, want > 0")
	}
	if secondHint.PacketNumberMax != firstHint.PacketNumberMax {
		t.Fatalf("secondHint.PacketNumberMax=%d, want=%d", secondHint.PacketNumberMax, firstHint.PacketNumberMax)
	}
}

func TestGetOrCreateInitialPacketProtector_CacheReuse(t *testing.T) {
	packet := mustHexDecodeString(`
		c7ff0000200008f067a5502a4262b500 4075fb12ff07823a5d24534d906ce4c7
		6782a2167e3479c0f7f6395dc2c91676 302fe6d70bb7cbeb117b4ddb7d173498
		44fd61dae200b8338e1b932976b61d91 e64a02e9e0ee72e3a6f63aba4ceeeec5
		be2f24f2d86027572943533846caa13e 6f163fb257473d0eda5047360fd4a47e
		fd8142fafc0f76
	`)
	hdr, _, err := ParseInitialHeader(packet)
	if err != nil {
		t.Fatalf("ParseInitialHeader() error=%v", err)
	}
	got1, err := getOrCreateInitialPacketProtector(hdr.Version, hdr.DestConnectionID, initialSecretLabelServerIn)
	if err != nil {
		t.Fatalf("getOrCreateInitialPacketProtector() #1 error=%v", err)
	}
	got2, err := getOrCreateInitialPacketProtector(hdr.Version, hdr.DestConnectionID, initialSecretLabelServerIn)
	if err != nil {
		t.Fatalf("getOrCreateInitialPacketProtector() #2 error=%v", err)
	}
	if got1 != got2 {
		t.Fatal("expected cache hit to return same protector pointer")
	}
}
