package wol

import (
	"testing"
)

func TestParseMAC(t *testing.T) {
	tests := []struct {
		input    string
		wantOK   bool
		wantBytes [6]byte
	}{
		{"AA:BB:CC:DD:EE:FF", true, [6]byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}},
		{"aa:bb:cc:dd:ee:ff", true, [6]byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}},
		{"AA-BB-CC-DD-EE-FF", true, [6]byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}},
		{"11:22:33:44:55:66", true, [6]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}},
		{"not:a:mac:addre", false, [6]byte{}},
		{"GG:HH:II:JJ:KK:LL", false, [6]byte{}},
		{"aa:bb:cc:dd", false, [6]byte{}},
		{"", false, [6]byte{}},
	}

	for _, tt := range tests {
		mac, err := ParseMAC(tt.input)
		if tt.wantOK {
			if err != nil {
				t.Errorf("ParseMAC(%q) unexpected error: %v", tt.input, err)
				continue
			}
			if mac != tt.wantBytes {
				t.Errorf("ParseMAC(%q) = %v, want %v", tt.input, mac, tt.wantBytes)
			}
		} else {
			if err == nil {
				t.Errorf("ParseMAC(%q) expected error, got nil", tt.input)
			}
		}
	}
}

func TestFormatMAC(t *testing.T) {
	mac := [6]byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
	got := FormatMAC(mac)
	want := "aa:bb:cc:dd:ee:ff"
	if got != want {
		t.Errorf("FormatMAC() = %q, want %q", got, want)
	}
}

func TestBuildMagicPacket(t *testing.T) {
	mac := [6]byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
	packet := BuildMagicPacket(mac)

	if len(packet) != 102 {
		t.Errorf("BuildMagicPacket length = %d, want 102", len(packet))
	}

	for i := 0; i < 6; i++ {
		if packet[i] != 0xFF {
			t.Errorf("packet[%d] = %02x, want FF", i, packet[i])
		}
	}

	for i := 0; i < 16; i++ {
		offset := 6 + i*6
		for j := 0; j < 6; j++ {
			if packet[offset+j] != mac[j] {
				t.Errorf("packet[%d] = %02x, want %02x (rep %d)", offset+j, packet[offset+j], mac[j], i)
			}
		}
	}
}
