package main

import (
	"testing"

	apppkg "github.com/jan/fm-rds-tx/internal/app"
	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 TestTxBridgeExportsQueueStats(t *testing.T) {
	cfg := cfgpkg.Default()
	driver := platform.NewSimulatedDriver(nil)
	engine := apppkg.NewEngine(cfg, driver)
	bridge := &txBridge{engine: engine}
	stats := bridge.TXStats()

	raw, ok := stats["queue"]
	if !ok {
		t.Fatalf("expected queue stats in tx stats")
	}

	queue, ok := raw.(output.QueueStats)
	if !ok {
		t.Fatalf("queue stats type mismatch: %T", raw)
	}

	if queue.Capacity != cfg.Runtime.FrameQueueCapacity {
		t.Fatalf("unexpected queue capacity: want %d got %d", cfg.Runtime.FrameQueueCapacity, queue.Capacity)
	}

	if queue.Health != output.QueueHealthCritical {
		t.Fatalf("queue health should be critical with empty queue, got %s", queue.Health)
	}

	indicatorRaw, ok := stats["runtimeIndicator"]
	if !ok {
		t.Fatalf("expected runtimeIndicator in tx stats")
	}
	indicator, ok := indicatorRaw.(apppkg.RuntimeIndicator)
	if !ok {
		t.Fatalf("runtimeIndicator type mismatch: %T", indicatorRaw)
	}
	if indicator != apppkg.RuntimeIndicatorQueueCritical {
		t.Fatalf("runtime indicator should be queueCritical, got %s", indicator)
	}
	if historyRaw, ok := stats["faultHistory"]; !ok {
		t.Fatalf("expected faultHistory in tx stats")
	} else if history, ok := historyRaw.([]apppkg.FaultEvent); !ok {
		t.Fatalf("faultHistory type mismatch: %T", historyRaw)
	} else if len(history) != 0 {
		t.Fatalf("expected no faults yet, got %d", len(history))
	}
}
