package exporter

import (
	"encoding/json"
	"os"
	"path/filepath"
	"testing"
	"time"

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

// TestGenerateReanimatorExample generates an example reanimator.json file
// This test is marked as skipped by default - run with: go test -v -run TestGenerateReanimatorExample
func TestGenerateReanimatorExample(t *testing.T) {
	t.Skip("Skip by default - run manually to generate example")

	// Create realistic test data matching import-example-full.json structure
	result := &models.AnalysisResult{
		Filename:    "redfish://10.10.10.103",
		SourceType:  "api",
		Protocol:    "redfish",
		TargetHost:  "10.10.10.103",
		CollectedAt: time.Date(2026, 2, 10, 15, 30, 0, 0, time.UTC),
		Hardware: &models.HardwareConfig{
			BoardInfo: models.BoardInfo{
				Manufacturer: "Supermicro",
				ProductName:  "X12DPG-QT6",
				SerialNumber: "21D634101",
				PartNumber:   "X12DPG-QT6-REV1.01",
				UUID:         "d7ef2fe5-2fd0-11f0-910a-346f11040868",
			},
			Firmware: []models.FirmwareInfo{
				{DeviceName: "BIOS", Version: "06.08.05"},
				{DeviceName: "BMC", Version: "5.17.00"},
				{DeviceName: "CPLD", Version: "01.02.03"},
			},
			CPUs: []models.CPU{
				{
					Socket:       0,
					Model:        "INTEL(R) XEON(R) GOLD 6530",
					Cores:        32,
					Threads:      64,
					FrequencyMHz: 2100,
					MaxFreqMHz:   4000,
				},
				{
					Socket:       1,
					Model:        "INTEL(R) XEON(R) GOLD 6530",
					Cores:        32,
					Threads:      64,
					FrequencyMHz: 2100,
					MaxFreqMHz:   4000,
				},
			},
			Memory: []models.MemoryDIMM{
				{
					Slot:            "CPU0_C0D0",
					Location:        "CPU0_C0D0",
					Present:         true,
					SizeMB:          32768,
					Type:            "DDR5",
					MaxSpeedMHz:     4800,
					CurrentSpeedMHz: 4800,
					Manufacturer:    "Hynix",
					SerialNumber:    "80AD032419E17CEEC1",
					PartNumber:      "HMCG88AGBRA191N",
					Status:          "OK",
				},
				{
					Slot:            "CPU1_C0D0",
					Location:        "CPU1_C0D0",
					Present:         true,
					SizeMB:          32768,
					Type:            "DDR5",
					MaxSpeedMHz:     4800,
					CurrentSpeedMHz: 4800,
					Manufacturer:    "Hynix",
					SerialNumber:    "80AD032419E17D6FBA",
					PartNumber:      "HMCG88AGBRA191N",
					Status:          "OK",
				},
			},
			Storage: []models.Storage{
				{
					Slot:         "OB01",
					Type:         "NVMe",
					Model:        "INTEL SSDPF2KX076T1",
					SizeGB:       7680,
					SerialNumber: "BTAX41900GF87P6DGN",
					Manufacturer: "Intel",
					Firmware:     "9CV10510",
					Interface:    "NVMe",
					Present:      true,
				},
				{
					Slot:         "OB02",
					Type:         "NVMe",
					Model:        "INTEL SSDPF2KX076T1",
					SizeGB:       7680,
					SerialNumber: "BTAX41900BEG7P6DGN",
					Manufacturer: "Intel",
					Firmware:     "9CV10510",
					Interface:    "NVMe",
					Present:      true,
				},
			},
			PCIeDevices: []models.PCIeDevice{
				{
					Slot:         "PCIeCard1",
					VendorID:     32902,
					DeviceID:     2912,
					BDF:          "0000:18:00.0",
					DeviceClass:  "MassStorageController",
					Manufacturer: "Intel",
					PartNumber:   "RAID Controller",
					SerialNumber: "RAID-001-12345",
					LinkWidth:    8,
					LinkSpeed:    "Gen3",
					MaxLinkWidth: 8,
					MaxLinkSpeed: "Gen3",
				},
			},
			PowerSupply: []models.PSU{
				{
					Slot:         "0",
					Present:      true,
					Model:        "GW-CRPS3000LW",
					Vendor:       "Great Wall",
					WattageW:     3000,
					SerialNumber: "2P06C102610",
					PartNumber:   "V0310C9000000000",
					Firmware:     "00.03.05",
					Status:       "OK",
					InputType:    "ACWideRange",
					InputPowerW:  137,
					OutputPowerW: 104,
					InputVoltage: 215.25,
				},
			},
		},
	}

	// Convert to Reanimator format
	reanimator, err := ConvertToReanimator(result)
	if err != nil {
		t.Fatalf("ConvertToReanimator failed: %v", err)
	}

	// Marshal to JSON with indentation
	jsonData, err := json.MarshalIndent(reanimator, "", "  ")
	if err != nil {
		t.Fatalf("Failed to marshal JSON: %v", err)
	}

	// Write to example file
	examplePath := filepath.Join("../../example/docs", "export-example-logpile.json")
	if err := os.WriteFile(examplePath, jsonData, 0644); err != nil {
		t.Fatalf("Failed to write example file: %v", err)
	}

	t.Logf("Generated example file: %s", examplePath)
	t.Logf("JSON length: %d bytes", len(jsonData))
}
