using ZB.MOM.WW.ScadaBridge.CLI;

namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;

[Collection("Console")]
public class OutputFormatterTests
{
    [Fact]
    public void WriteJson_WritesIndentedJson()
    {
        var writer = new StringWriter();
        Console.SetOut(writer);

        OutputFormatter.WriteJson(new { Name = "test", Value = 42 });

        var output = writer.ToString().Trim();
        Assert.Contains("\"name\"", output);
        Assert.Contains("\"value\": 42", output);

        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
    }

    [Fact]
    public void WriteError_WritesToStdErr()
    {
        var writer = new StringWriter();
        Console.SetError(writer);

        OutputFormatter.WriteError("something went wrong", "ERR_CODE");

        var output = writer.ToString().Trim();
        Assert.Contains("something went wrong", output);
        Assert.Contains("ERR_CODE", output);

        Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
    }

    [Fact]
    public void WriteTable_RendersHeadersAndRows()
    {
        var writer = new StringWriter();
        Console.SetOut(writer);

        var headers = new[] { "Id", "Name", "Status" };
        var rows = new List<string[]>
        {
            new[] { "1", "Alpha", "Active" },
            new[] { "2", "Beta", "Inactive" }
        };

        OutputFormatter.WriteTable(rows, headers);

        var output = writer.ToString();
        Assert.Contains("Id", output);
        Assert.Contains("Name", output);
        Assert.Contains("Status", output);
        Assert.Contains("Alpha", output);
        Assert.Contains("Beta", output);
        Assert.Contains("Inactive", output);

        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
    }

    [Fact]
    public void WriteTable_EmptyRows_ShowsHeadersOnly()
    {
        var writer = new StringWriter();
        Console.SetOut(writer);

        var headers = new[] { "Id", "Name" };
        OutputFormatter.WriteTable(Array.Empty<string[]>(), headers);

        var output = writer.ToString();
        Assert.Contains("Id", output);
        Assert.Contains("Name", output);

        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
    }

    [Fact]
    public void WriteTable_ColumnWidthsAdjustToContent()
    {
        var writer = new StringWriter();
        Console.SetOut(writer);

        var headers = new[] { "X", "LongColumnName" };
        var rows = new List<string[]>
        {
            new[] { "ShortValue", "Y" }
        };

        OutputFormatter.WriteTable(rows, headers);

        var lines = writer.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
        // Header line: "X" should be padded to at least "ShortValue" width
        Assert.True(lines.Length >= 2);
        // The "X" column header should be padded wider than 1 character
        var headerLine = lines[0];
        Assert.True(headerLine.IndexOf("LongColumnName") > 1);

        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
    }
}
