package udpsprotocol

import (
	"encoding/binary"
	"math"
	"testing"
	"time"
)

// TestParseConfig_HugeNumSigs_NoOOM — a CONFIG payload claiming 0xFFFFFFFF signals
// must return an error, not panic/OOM.
func TestParseConfig_HugeNumSigs_NoOOM(t *testing.T) {
	// 4 bytes: numSigs = 0xFFFFFFFF, then nothing else
	payload := make([]byte, 4)
	binary.LittleEndian.PutUint32(payload[0:4], 0xFFFFFFFF)
	sigs, _, err := ParseConfig(payload)
	if err == nil {
		t.Fatal("expected error for huge numSigs, got nil")
	}
	if sigs != nil {
		t.Fatalf("expected nil sigs, got %d", len(sigs))
	}
}

// TestParseConfig_ValidSmallConfig — a minimal valid CONFIG parses correctly.
func TestParseConfig_ValidSmallConfig(t *testing.T) {
	// 1 signal, then publish mode
	payload := make([]byte, 4+SigDescSize+1)
	binary.LittleEndian.PutUint32(payload[0:4], 1)
	// Set typeCode to float32 (8) at offset 64
	payload[4+64] = 8
	// numRows=1, numCols=1 at offsets 67, 71
	binary.LittleEndian.PutUint32(payload[4+67:4+71], 1)
	binary.LittleEndian.PutUint32(payload[4+71:4+75], 1)
	// publish mode = 0 (Strict)
	payload[4+SigDescSize] = 0
	sigs, pm, err := ParseConfig(payload)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(sigs) != 1 {
		t.Fatalf("expected 1 signal, got %d", len(sigs))
	}
	if pm != PublishModeStrict {
		t.Fatalf("expected Strict mode, got %d", pm)
	}
}

// TestNumElements_OverflowCapped — huge numRows*numCols is capped, no panic.
func TestNumElements_OverflowCapped(t *testing.T) {
	s := SignalInfo{NumRows: 0xFFFFFFFF, NumCols: 0xFFFFFFFF}
	n := s.NumElements()
	if n <= 0 || n > 1024*1024 {
		t.Fatalf("expected capped value 1M, got %d", n)
	}
}

// TestNumElements_Normal — normal values work correctly.
func TestNumElements_Normal(t *testing.T) {
	s := SignalInfo{NumRows: 3, NumCols: 4}
	if n := s.NumElements(); n != 12 {
		t.Fatalf("expected 12, got %d", n)
	}
}

// TestParseData_HugeNumSamples_NoOOM — an Accumulate DATA packet with
// numSamples=0xFFFFFFFF must return an error, not OOM.
func TestParseData_HugeNumSamples_NoOOM(t *testing.T) {
	sigs := []SignalInfo{
		{Name: "test", TypeCode: 8, NumRows: 1, NumCols: 1, QuantType: QuantNone},
	}
	payload := make([]byte, 12)
	binary.LittleEndian.PutUint64(payload[0:8], 0) // HRT
	binary.LittleEndian.PutUint32(payload[8:12], 0xFFFFFFFF)
	_, err := ParseData(payload, sigs, PublishModeAccumulate, time.Now())
	if err == nil {
		t.Fatal("expected error for huge numSamples, got nil")
	}
}

// TestParseData_ValidStrict — a valid Strict DATA packet parses without error.
func TestParseData_ValidStrict(t *testing.T) {
	sigs := []SignalInfo{
		{Name: "test", TypeCode: 8, NumRows: 1, NumCols: 1, QuantType: QuantNone},
	}
	// 8 HRT + 4 bytes float32
	payload := make([]byte, 12)
	binary.LittleEndian.PutUint64(payload[0:8], 1000)
	binary.LittleEndian.PutUint32(payload[8:12], math.Float32bits(3.14))
	samples, err := ParseData(payload, sigs, PublishModeStrict, time.Now())
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(samples) != 1 {
		t.Fatalf("expected 1 sample, got %d", len(samples))
	}
}
