package xigmanas

import (
	"os"
	"path/filepath"
	"strings"
	"testing"

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

func TestParserDetect(t *testing.T) {
	p := &Parser{}

	files := []parser.ExtractedFile{
		{
			Path: "xigmanas",
			Content: []byte(`Version:
--------
14.3.0.5
loader_brand="XigmaNAS"`),
		},
	}

	if got := p.Detect(files); got < 70 {
		t.Fatalf("expected high confidence, got %d", got)
	}

	files2 := []parser.ExtractedFile{
		{
			Path:    "random_file.txt",
			Content: []byte("Some random content"),
		},
	}

	if got := p.Detect(files2); got != 0 {
		t.Fatalf("expected zero confidence, got %d", got)
	}
}

func TestParserParseExample(t *testing.T) {
	p := &Parser{}

	examplePath := filepath.Join("..", "..", "..", "..", "example", "xigmanas.txt")
	raw, err := os.ReadFile(examplePath)
	if err != nil {
		if os.IsNotExist(err) {
			t.Skipf("example file %s not present", examplePath)
		}
		t.Fatalf("read example file: %v", err)
	}

	files := []parser.ExtractedFile{
		{Path: "xigmanas", Content: raw},
	}

	result, err := p.Parse(files)
	if err != nil {
		t.Fatalf("parse failed: %v", err)
	}
	if result == nil || result.Hardware == nil {
		t.Fatal("expected non-nil result with hardware")
	}

	if len(result.Hardware.Firmware) == 0 {
		t.Fatal("expected firmware data")
	}
	foundXigmaVersion := false
	for _, fw := range result.Hardware.Firmware {
		if fw.DeviceName == "XigmaNAS" && fw.Version == "14.3.0.5" {
			foundXigmaVersion = true
		}
	}
	if !foundXigmaVersion {
		t.Fatalf("expected XigmaNAS firmware version 14.3.0.5, got %+v", result.Hardware.Firmware)
	}

	if result.Hardware.BoardInfo.Manufacturer != "HP" {
		t.Fatalf("expected board manufacturer HP, got %q", result.Hardware.BoardInfo.Manufacturer)
	}
	if len(result.Hardware.CPUs) == 0 {
		t.Fatal("expected at least one CPU")
	}
	if !strings.Contains(strings.ToLower(result.Hardware.CPUs[0].Model), "athlon") {
		t.Fatalf("expected CPU model to contain athlon, got %q", result.Hardware.CPUs[0].Model)
	}

	if len(result.Hardware.Storage) < 4 {
		t.Fatalf("expected at least 4 storage devices, got %d", len(result.Hardware.Storage))
	}
	if len(result.Sensors) == 0 {
		t.Fatal("expected SMART temperature sensors")
	}
	if len(result.Events) == 0 {
		t.Fatal("expected events from uptime/zfs sections")
	}

	var hasSystemLog, hasSmartdLog, hasDaemonLog, hasLoginFailure bool
	for _, ev := range result.Events {
		if ev.EventType == "System Log" {
			hasSystemLog = true
		}
		if ev.EventType == "SMARTD Log" {
			hasSmartdLog = true
		}
		if ev.EventType == "Daemon Log" {
			hasDaemonLog = true
		}
		if strings.Contains(strings.ToLower(ev.Description), "login failure") {
			hasLoginFailure = true
		}
	}
	if !hasSystemLog || !hasSmartdLog || !hasDaemonLog {
		t.Fatalf("expected events from System/SMARTD/Daemon sections, got system=%v smartd=%v daemon=%v", hasSystemLog, hasSmartdLog, hasDaemonLog)
	}
	if !hasLoginFailure {
		t.Fatal("expected to parse login failure event from system log section")
	}
}
