// Package nvidia_bug_report provides parser for NVIDIA bug report files
// Generated by nvidia-bug-report.sh script
package nvidia_bug_report

import (
	"strings"

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

// parserVersion - version of this parser module
const parserVersion = "1.0.0"

func init() {
	parser.Register(&Parser{})
}

// Parser implements VendorParser for NVIDIA bug reports
type Parser struct{}

// Name returns human-readable parser name
func (p *Parser) Name() string {
	return "NVIDIA Bug Report Parser"
}

// Vendor returns vendor identifier
func (p *Parser) Vendor() string {
	return "nvidia_bug_report"
}

// Version returns parser version
func (p *Parser) Version() string {
	return parserVersion
}

// Detect checks if this is an NVIDIA bug report
// Returns confidence 0-100
func (p *Parser) Detect(files []parser.ExtractedFile) int {
	// Only detect if there's exactly one file
	if len(files) != 1 {
		return 0
	}

	file := files[0]

	// Check filename
	if !strings.Contains(strings.ToLower(file.Path), "nvidia-bug-report") {
		return 0
	}

	// Check content markers
	content := string(file.Content)
	if !strings.Contains(content, "nvidia-bug-report.sh") ||
		!strings.Contains(content, "NVIDIA bug report log file") {
		return 0
	}

	// High confidence for nvidia-bug-report files
	return 85
}

// Parse parses NVIDIA bug report file
func (p *Parser) Parse(files []parser.ExtractedFile) (*models.AnalysisResult, error) {
	result := &models.AnalysisResult{
		Events:  make([]models.Event, 0),
		FRU:     make([]models.FRUInfo, 0),
		Sensors: make([]models.SensorReading, 0),
	}

	// Initialize hardware config
	result.Hardware = &models.HardwareConfig{
		CPUs:        make([]models.CPU, 0),
		Memory:      make([]models.MemoryDIMM, 0),
		GPUs:        make([]models.GPU, 0),
		PowerSupply: make([]models.PSU, 0),
	}

	if len(files) == 0 {
		return result, nil
	}

	content := string(files[0].Content)

	// Parse system information
	parseSystemInfo(content, result)

	// Parse CPU information
	parseCPUInfo(content, result)

	// Parse memory modules
	parseMemoryModules(content, result)

	// Parse power supplies
	parsePSUInfo(content, result)

	// Parse GPU information
	parseGPUInfo(content, result)

	// Parse network adapters
	parseNetworkAdapters(content, result)

	// Parse driver version
	parseDriverVersion(content, result)

	return result, nil
}
