package collector

import (
	"bee/audit/internal/schema"
	"testing"
)

func TestFinalizeSnapshotFiltersComponentsWithoutRequiredSerials(t *testing.T) {
	collectedAt := "2026-03-15T12:00:00Z"
	present := true
	status := statusOK
	serial := "SN-1"
	virtualModel := "Virtual HDisk1"
	realModel := "PASCARI"
	coProcessorClass := "Co-processor"
	gpuClass := "VideoController"

	snap := schema.HardwareSnapshot{
		Memory: []schema.HardwareMemory{
			{Present: &present, SerialNumber: &serial, HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
			{Present: &present, HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
		},
		Storage: []schema.HardwareStorage{
			{Model: &virtualModel, SerialNumber: &serial, HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
			{SerialNumber: &serial, HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
			{Model: &realModel, SerialNumber: &serial, HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
			{HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
		},
		PCIeDevices: []schema.HardwarePCIeDevice{
			{DeviceClass: &coProcessorClass, HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
			{DeviceClass: &gpuClass, HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
		},
		PowerSupplies: []schema.HardwarePowerSupply{
			{SerialNumber: &serial, HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
			{HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
		},
	}

	finalizeSnapshot(&snap, collectedAt)

	if len(snap.Memory) != 1 || snap.Memory[0].StatusCheckedAt == nil || *snap.Memory[0].StatusCheckedAt != collectedAt {
		t.Fatalf("memory finalize mismatch: %+v", snap.Memory)
	}
	if len(snap.Storage) != 2 || snap.Storage[0].StatusCheckedAt == nil || *snap.Storage[0].StatusCheckedAt != collectedAt {
		t.Fatalf("storage finalize mismatch: %+v", snap.Storage)
	}
	if len(snap.PCIeDevices) != 1 || snap.PCIeDevices[0].DeviceClass == nil || *snap.PCIeDevices[0].DeviceClass != gpuClass {
		t.Fatalf("pcie finalize mismatch: %+v", snap.PCIeDevices)
	}
	if len(snap.PowerSupplies) != 1 || snap.PowerSupplies[0].StatusCheckedAt == nil || *snap.PowerSupplies[0].StatusCheckedAt != collectedAt {
		t.Fatalf("psu finalize mismatch: %+v", snap.PowerSupplies)
	}
}

func TestFinalizeSnapshotPreservesDuplicateSerials(t *testing.T) {
	collectedAt := "2026-03-15T12:00:00Z"
	status := statusOK
	model := "Device"
	serial := "DUPLICATE"

	snap := schema.HardwareSnapshot{
		Storage: []schema.HardwareStorage{
			{Model: &model, SerialNumber: &serial, HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
			{Model: &model, SerialNumber: &serial, HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
		},
	}

	finalizeSnapshot(&snap, collectedAt)

	if got := *snap.Storage[0].SerialNumber; got != serial {
		t.Fatalf("first serial changed: %q", got)
	}
	if got := *snap.Storage[1].SerialNumber; got != serial {
		t.Fatalf("duplicate serial should stay unchanged: %q", got)
	}
}

func TestFilterPSUsKeepsSlotOnlyEntries(t *testing.T) {
	slot := "0"
	status := statusOK

	got := filterPSUs([]schema.HardwarePowerSupply{
		{Slot: &slot, HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
		{HardwareComponentStatus: schema.HardwareComponentStatus{Status: &status}},
	})

	if len(got) != 1 {
		t.Fatalf("len(got)=%d want 1", len(got))
	}
	if got[0].Slot == nil || *got[0].Slot != "0" {
		t.Fatalf("unexpected kept PSU: %+v", got[0])
	}
}
