using ZB.MOM.WW.ScadaBridge.CLI;
using ZB.MOM.WW.ScadaBridge.CLI.Commands;

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

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

        var response = new ManagementResponse(200, "{\"id\":1,\"name\":\"test\"}", null, null);
        var exitCode = CommandHelpers.HandleResponse(response, "json");

        Assert.Equal(0, exitCode);
        Assert.Contains("{\"id\":1,\"name\":\"test\"}", writer.ToString());

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

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

        var json = "[{\"Id\":1,\"Name\":\"Alpha\"},{\"Id\":2,\"Name\":\"Beta\"}]";
        var response = new ManagementResponse(200, json, null, null);
        var exitCode = CommandHelpers.HandleResponse(response, "table");

        Assert.Equal(0, exitCode);
        var output = writer.ToString();
        Assert.Contains("Id", output);
        Assert.Contains("Name", output);
        Assert.Contains("Alpha", output);
        Assert.Contains("Beta", output);

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

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

        var json = "{\"Id\":1,\"Name\":\"Alpha\",\"Status\":\"Active\"}";
        var response = new ManagementResponse(200, json, null, null);
        var exitCode = CommandHelpers.HandleResponse(response, "table");

        Assert.Equal(0, exitCode);
        var output = writer.ToString();
        Assert.Contains("Property", output);
        Assert.Contains("Value", output);
        Assert.Contains("Id", output);
        Assert.Contains("Alpha", output);

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

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

        var response = new ManagementResponse(200, "[]", null, null);
        var exitCode = CommandHelpers.HandleResponse(response, "table");

        Assert.Equal(0, exitCode);
        Assert.Contains("(no results)", writer.ToString());

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

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

        var response = new ManagementResponse(400, null, "Something failed", "FAIL_CODE");
        var exitCode = CommandHelpers.HandleResponse(response, "json");

        Assert.Equal(1, exitCode);
        Assert.Contains("Something failed", errWriter.ToString());

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

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

        var response = new ManagementResponse(403, null, "Access denied", "UNAUTHORIZED");
        var exitCode = CommandHelpers.HandleResponse(response, "json");

        Assert.Equal(2, exitCode);
        Assert.Contains("Access denied", errWriter.ToString());

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

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

        var response = new ManagementResponse(401, null, "Invalid credentials", "AUTH_FAILED");
        var exitCode = CommandHelpers.HandleResponse(response, "json");

        Assert.Equal(1, exitCode);
        Assert.Contains("Invalid credentials", errWriter.ToString());

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

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

        var response = new ManagementResponse(0, null, "Connection failed: No such host", "CONNECTION_FAILED");
        var exitCode = CommandHelpers.HandleResponse(response, "json");

        Assert.Equal(1, exitCode);
        Assert.Contains("Connection failed", errWriter.ToString());

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

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

        var response = new ManagementResponse(504, null, "Request timed out.", "TIMEOUT");
        var exitCode = CommandHelpers.HandleResponse(response, "json");

        Assert.Equal(1, exitCode);
        Assert.Contains("timed out", errWriter.ToString());

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