using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace TestersAI.NUnitSupport;

public static class TestersAIAssert
{
    public static void Clean(AnalysisResult r)
    {
        if (!r.Failed) return;
        var lines = new List<string>();
        foreach (var i in r.Issues) lines.Add($"[{i.Severity}] {i.Category}: {i.Message}");
        Assert.Fail("testersai found issues: " + string.Join("; ", lines));
    }
}

public sealed class TestersAIFixture
{
    public TestersAIClient Client { get; }
    public List<Action<AnalysisResult>> Sinks { get; }

    public TestersAIFixture()
    {
        var cfg = Config.FromEnv();
        Client = new TestersAIClient(cfg);
        Sinks = TestersAI.Sinks.Build(cfg, r =>
            TestContext.WriteLine($"[testersai:{r.Kind}] ok={r.Ok} skipped={r.Skipped} issues={r.Issues.Count}"));
    }

    public AnalysisResult Wrap(AnalysisResult r) => TestersAI.Sinks.Dispatch(r, Sinks);
}
