namespace Lumi.Companion.App;

public enum TrayHealth { Ready, Operating, Degraded, Failed }
public enum CompanionPage { Overview, Transcription, Test, Connection, Logs, Settings }
public enum TestStageState { Waiting, Running, Passed, Blocked, Failed }

public sealed record CompanionState(
    bool Paired = false,
    bool Connected = false,
    bool ObsBridgeInstalled = false,
    bool ObsConnected = false,
    bool ObsStreaming = false,
    bool ObsRecording = false,
    bool TestRunning = false,
    bool BenchmarkRunning = false,
    TrayHealth Health = TrayHealth.Degraded,
    string Detail = "Pair Lumi Companion to get started.",
    string? DeviceName = null,
    string? Host = null,
    DateTimeOffset? LastConnectedAt = null,
    bool UpdateAvailable = false,
    string? AvailableVersion = null,
    string UpdateDetail = "Checking for updates…",
    string BenchmarkDetail = "Start a dedicated test, speak naturally, then end it when you have enough material.",
    bool PathTestValid = false,
    string PathTestDetail = "Run once after setup or a relevant configuration change.",
    bool ObsBridgeRepairNeeded = false,
    bool ObsBridgePackageAvailable = false,
    string ObsBridgeDetail = "Checking the managed OBS integration…")
{
    public string Summary => Health switch
    {
        TrayHealth.Operating => "Transcription active",
        TrayHealth.Ready => "Ready",
        TrayHealth.Failed => "Needs attention",
        _ when !Paired => "Setup required",
        _ when !Connected => "Lumi offline",
        _ when !ObsBridgeInstalled => "OBS setup required",
        _ => "Partially ready"
    };

    public bool RequiresQuitConfirmation => ObsStreaming || ObsRecording || BenchmarkRunning;
    public string QuitWarning => BenchmarkRunning
        ? "A transcription accuracy and latency test is running. Quitting will safely end and mark the test as aborted."
        : ObsStreaming
        ? "OBS is streaming. Quitting Lumi Companion will stop transcription and closed captions, but it will not stop the OBS stream."
        : ObsRecording ? "OBS is recording. Quitting Lumi Companion will stop active companion features." : string.Empty;
}

public sealed record TestStage(string Name, string Detail, TestStageState State);
public sealed record BenchmarkWord(string Text, double LatencyMs, double? Confidence, bool Final);
public sealed record MetricStatistics(int Count, double? Minimum, double? LowOnePercentAverage, double? Median, double? Average, double? P99, double? HighOnePercentAverage, double? Maximum);
public sealed record BenchmarkSnapshot(string? Id, string Transcript, IReadOnlyList<BenchmarkWord> Words, MetricStatistics Latency, MetricStatistics Confidence, DateTimeOffset? StartedAt, string Status);
public sealed record ObsSource(string Uuid, string Name, bool Active, bool Missing)
{
    public override string ToString() => Missing ? $"{Name} (missing)" : Active ? $"{Name} (active)" : Name;
}
