package nvidia

import (
	"testing"

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

func TestApplyGPUModelsFromSKU(t *testing.T) {
	files := []parser.ExtractedFile{
		{
			Path: "inventory/fieldiag_summary.json",
			Content: []byte(`{
				"ModsRuns":[
					{"ModsHeader":[
						{"GpuName":"SXM5_SN_1653925025497","BoardInfo":"G520-0280"}
					]}
				]
			}`),
		},
		{
			Path: "testspec.json",
			Content: []byte(`{
				"actions":[
					{
						"virtual_id":"inventory",
						"args":{
							"sku_to_sku_json_file_map":{
								"G520-0280":"sku_hgx-h200-8-gpu_141g_aircooled_field.json"
							}
						}
					}
				]
			}`),
		},
	}

	result := &models.AnalysisResult{
		Hardware: &models.HardwareConfig{
			GPUs: []models.GPU{
				{
					Slot:         "GPUSXM5",
					SerialNumber: "1653925025497",
					Model:        "NVIDIA Device 2335",
				},
			},
		},
	}

	ApplyGPUModelsFromSKU(files, result)

	if got := result.Hardware.GPUs[0].Model; got != "NVIDIA Device 2335" {
		t.Fatalf("expected model NVIDIA Device 2335, got %q", got)
	}
	if got := result.Hardware.GPUs[0].Description; got != "hgx h200 8 gpu 141g aircooled" {
		t.Fatalf("expected description hgx h200 8 gpu 141g aircooled, got %q", got)
	}
}

func TestApplyGPUModelsFromSKU_FromPartNumber(t *testing.T) {
	files := []parser.ExtractedFile{
		{
			Path: "inforom/SXM5/fieldiag.jso",
			Content: []byte(`[
				[
					{
						"__tag__":"inforom",
						"serial_number":"1653925025497",
						"product_part_num":"692-2G520-0280-501"
					}
				]
			]`),
		},
		{
			Path: "testspec.json",
			Content: []byte(`{
				"actions":[
					{
						"virtual_id":"inventory",
						"args":{
							"sku_to_sku_json_file_map":{
								"G520-0280":"sku_hgx-h200-8-gpu_141g_aircooled_field.json"
							}
						}
					}
				]
			}`),
		},
	}

	result := &models.AnalysisResult{
		Hardware: &models.HardwareConfig{
			GPUs: []models.GPU{
				{
					Slot:         "GPUSXM5",
					SerialNumber: "1653925025497",
					Model:        "NVIDIA Device 2335",
				},
			},
		},
	}

	ApplyGPUModelsFromSKU(files, result)

	if got := result.Hardware.GPUs[0].Model; got != "692-2G520-0280-501" {
		t.Fatalf("expected model 692-2G520-0280-501, got %q", got)
	}
	if got := result.Hardware.GPUs[0].PartNumber; got != "692-2G520-0280-501" {
		t.Fatalf("expected part number 692-2G520-0280-501, got %q", got)
	}
	if got := result.Hardware.GPUs[0].Description; got != "hgx h200 8 gpu 141g aircooled" {
		t.Fatalf("expected description hgx h200 8 gpu 141g aircooled, got %q", got)
	}
}

func TestApplyGPUModelsFromSKU_FieldDiagSummaryArrayFormat(t *testing.T) {
	files := []parser.ExtractedFile{
		{
			Path: "inventory/fieldiag_summary.json",
			Content: []byte(`[
				{
					"ModsRuns":[
						{"ModsHeader":[
							{"GpuName":"SXM5_SN_1653925025497","BoardInfo":"G520-0280"}
						]}
					]
				}
			]`),
		},
		{
			Path: "testspec.json",
			Content: []byte(`{
				"actions":[
					{
						"virtual_id":"inventory",
						"args":{
							"sku_to_sku_json_file_map":{
								"G520-0280":"sku_hgx-h200-8-gpu_141g_aircooled_field.json"
							}
						}
					}
				]
			}`),
		},
	}

	result := &models.AnalysisResult{
		Hardware: &models.HardwareConfig{
			GPUs: []models.GPU{
				{
					Slot:         "GPUSXM5",
					SerialNumber: "1653925025497",
					Model:        "NVIDIA Device 2335",
				},
			},
		},
	}

	ApplyGPUModelsFromSKU(files, result)

	if got := result.Hardware.GPUs[0].Model; got != "NVIDIA Device 2335" {
		t.Fatalf("expected model NVIDIA Device 2335, got %q", got)
	}
	if got := result.Hardware.GPUs[0].Description; got != "hgx h200 8 gpu 141g aircooled" {
		t.Fatalf("expected description hgx h200 8 gpu 141g aircooled, got %q", got)
	}
}

func TestApplyGPUModelsFromSKU_FallbackToGenerationFromModsMapping(t *testing.T) {
	files := []parser.ExtractedFile{
		{
			Path: "testspec.json",
			Content: []byte(`{
				"actions":[
					{
						"virtual_id":"gpu_fieldiag",
						"args":{
							"mods_mapping":{
								"#mods.525":"Hopper",
								"525":["0x2335"]
							}
						}
					}
				]
			}`),
		},
	}

	result := &models.AnalysisResult{
		Hardware: &models.HardwareConfig{
			GPUs: []models.GPU{
				{
					Slot:     "GPUSXM5",
					Model:    "NVIDIA Device 2335",
					DeviceID: 0x2335,
				},
			},
		},
	}

	ApplyGPUModelsFromSKU(files, result)

	if got := result.Hardware.GPUs[0].Description; got != "Hopper" {
		t.Fatalf("expected description Hopper, got %q", got)
	}
}
