package nvidia_bug_report

import (
	"bufio"
	"strings"

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

// parseSystemInfo extracts System Information from dmidecode output
func parseSystemInfo(content string, result *models.AnalysisResult) {
	scanner := bufio.NewScanner(strings.NewReader(content))

	inSystemInfo := false

	for scanner.Scan() {
		line := scanner.Text()
		trimmed := strings.TrimSpace(line)

		// Start of System Information section
		if trimmed == "System Information" {
			inSystemInfo = true
			continue
		}

		// End of section (empty line or new Handle)
		if inSystemInfo && (trimmed == "" || strings.HasPrefix(trimmed, "Handle ")) {
			inSystemInfo = false
			continue
		}

		// Parse fields within System Information section
		if inSystemInfo && strings.Contains(line, ":") {
			parts := strings.SplitN(trimmed, ":", 2)
			if len(parts) != 2 {
				continue
			}

			field := strings.TrimSpace(parts[0])
			value := strings.TrimSpace(parts[1])

			// Skip empty, NULL, or "Not specified" values
			if value == "" || value == "NULL" || value == "Not specified" || value == "Not Specified" {
				continue
			}

			switch field {
			case "Manufacturer":
				result.Hardware.BoardInfo.Manufacturer = value
			case "Product Name":
				result.Hardware.BoardInfo.ProductName = value
			case "Version":
				result.Hardware.BoardInfo.Version = value
			case "Serial Number":
				result.Hardware.BoardInfo.SerialNumber = value
			case "UUID":
				result.Hardware.BoardInfo.UUID = value
			}
		}
	}
}
