package parser

import (
	"testing"

	"git.mchus.pro/mchus/logpile/internal/models"
)

func TestNormalizeManufacturedYearWeek(t *testing.T) {
	tests := []struct {
		in   string
		want string
	}{
		{"2024-W07", "2024-W07"},
		{"2024-02-13", "2024-W07"},
		{"02/13/2024", "2024-W07"},
		{"Tue Feb 13 12:00:00 2024", "2024-W07"},
		{"", ""},
		{"not-a-date", ""},
	}

	for _, tt := range tests {
		if got := NormalizeManufacturedYearWeek(tt.in); got != tt.want {
			t.Fatalf("NormalizeManufacturedYearWeek(%q) = %q, want %q", tt.in, got, tt.want)
		}
	}
}

func TestApplyManufacturedYearWeekFromFRU_AttachesByExactSerial(t *testing.T) {
	hw := &models.HardwareConfig{
		PowerSupply: []models.PSU{
			{
				Slot:         "PSU0",
				SerialNumber: "PSU-SN-001",
			},
		},
		Storage: []models.Storage{
			{
				Slot:         "OB01",
				SerialNumber: "DISK-SN-001",
			},
		},
	}
	fru := []models.FRUInfo{
		{
			Description:  "PSU0_FRU (ID 30)",
			SerialNumber: "PSU-SN-001",
			MfgDate:      "2024-02-13",
		},
		{
			Description:  "Builtin FRU Device (ID 0)",
			SerialNumber: "BOARD-SN-001",
			MfgDate:      "2024-02-01",
		},
	}

	ApplyManufacturedYearWeekFromFRU(fru, hw)

	if got := hw.PowerSupply[0].Details["manufactured_year_week"]; got != "2024-W07" {
		t.Fatalf("expected PSU year week 2024-W07, got %#v", hw.PowerSupply[0].Details)
	}
	if hw.Storage[0].Details != nil {
		t.Fatalf("expected unmatched storage serial to stay untouched, got %#v", hw.Storage[0].Details)
	}
}
