package app

import (
	"testing"

	cfgpkg "github.com/jan/fm-rds-tx/internal/config"
	"github.com/jan/fm-rds-tx/internal/output"
	"github.com/jan/fm-rds-tx/internal/platform"
)

func TestFaultSeverityString(t *testing.T) {
	cases := []struct {
		severity FaultSeverity
		want     string
	}{
		{FaultSeverityWarn, "warn"},
		{FaultSeverityDegraded, "degraded"},
		{FaultSeverityMuted, "muted"},
		{FaultSeverityFaulted, "faulted"},
		{FaultSeverity(99), "unknown"},
	}
	for _, tc := range cases {
		t.Run(tc.want, func(t *testing.T) {
			if got := tc.severity.String(); got != tc.want {
				t.Fatalf("expected %s, got %s", tc.want, got)
			}
			if txt, _ := tc.severity.MarshalText(); string(txt) != tc.want {
				t.Fatalf("MarshalText mismatch: want %s, got %s", tc.want, txt)
			}
		})
	}
}

func TestEngineRecordsQueueCriticalFault(t *testing.T) {
	e := NewEngine(cfgpkg.Default(), platform.NewSimulatedDriver(nil))
	e.setRuntimeState(RuntimeStateRunning)

	queue := output.QueueStats{Depth: 3, Health: output.QueueHealthCritical}
	for i := 0; i < queueCriticalStreakThreshold; i++ {
		e.evaluateRuntimeState(queue, false)
	}

	last := e.LastFault()
	if last == nil {
		t.Fatal("expected fault recorded, got nil")
	}
	if last.Reason != FaultReasonQueueCritical {
		t.Fatalf("expected queue critical reason, got %s", last.Reason)
	}
	if last.Severity != FaultSeverityDegraded {
		t.Fatalf("expected degraded severity, got %s", last.Severity)
	}
}

func TestEngineRecordsLateBufferFault(t *testing.T) {
	e := NewEngine(cfgpkg.Default(), platform.NewSimulatedDriver(nil))
	e.setRuntimeState(RuntimeStateRunning)

	queue := output.QueueStats{Depth: 5, Health: output.QueueHealthNormal}
	e.evaluateRuntimeState(queue, true)

	last := e.LastFault()
	if last == nil {
		t.Fatal("expected fault recorded for late buffers")
	}
	if last.Reason != FaultReasonLateBuffers {
		t.Fatalf("expected late buffer reason, got %s", last.Reason)
	}
	if last.Severity != FaultSeverityWarn {
		t.Fatalf("expected warn severity, got %s", last.Severity)
	}
}
