using Tests.EndToEnd.Infra;
using Xunit.Abstractions;

namespace Tests.EndToEnd.SwitchTests;

[Collection("Yaml CLI tests")]
public class SwitchWorkflowTests(TempYamlCliFixture fs, ITestOutputHelper outputHelper)
    : IClassFixture<TempYamlCliFixture> {
    private async Task<(string, string)> ExecuteAsync(params string[] args) {
        outputHelper.WriteLine($"rpk {string.Join(" ", args)}");

        var output = await YamlCliTestHost.RunAsync(
            args,
            fs.Root,
            outputHelper,
            "config.yaml"
        );

        outputHelper.WriteLine(output);

        var yaml = await File.ReadAllTextAsync(Path.Combine(fs.Root, "config.yaml"));
        return (output, yaml);
    }

    [Fact]
    public async Task switches_cli_workflow_test() {
        await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), "");

        // Add switch
        (var output, var yaml) = await ExecuteAsync("switches", "add", "sw01");
        Assert.Equal("Switch 'sw01' added.\n", output);
        Assert.Contains("name: sw01", yaml);

        // Update switch
        (output, yaml) = await ExecuteAsync(
            "switches", "set", "sw01",
            "--Model", "Netgear GS108",
            "--managed", "true",
            "--poe", "true"
        );
        // ToDo fails due command incorrect 
        Assert.Equal("Switch 'sw01' updated.\n", output);

        Assert.Equal("""
                     version: 3
                     resources:
                     - kind: Switch
                       model: Netgear GS108
                       managed: true
                       poe: true
                       name: sw01
                     connections: []

                     """, yaml);

        // Add second switch
        (output, yaml) = await ExecuteAsync("switches", "add", "sw02");
        Assert.Equal("Switch 'sw02' added.\n", output);

        (output, yaml) = await ExecuteAsync(
            "switches", "set", "sw02",
            "--Model", "TP-Link TL-SG108E",
            "--managed", "false",
            "--poe", "false"
        );
        // ToDo fails due command incorrect 
        Assert.Equal("Switch 'sw02' updated.\n", output);

        Assert.Equal("""
                     version: 3
                     resources:
                     - kind: Switch
                       model: Netgear GS108
                       managed: true
                       poe: true
                       name: sw01
                     - kind: Switch
                       model: TP-Link TL-SG108E
                       managed: false
                       poe: false
                       name: sw02
                     connections: []

                     """, yaml);

        // Get switch
        (output, yaml) = await ExecuteAsync("switches", "get", "sw01");
        Assert.Equal("sw01  Model: Netgear GS108, Managed: Yes, PoE: Yes\n", output);

        // List switches
        (output, yaml) = await ExecuteAsync("switches", "list");
        Assert.Contains("sw01", output);
        Assert.Contains("sw02", output);
        Assert.Contains("Netgear GS108", output);
        Assert.Contains("TP-Link TL-SG108E", output);
        Assert.Contains("Managed", output);
        Assert.Contains("PoE", output);
        Assert.Contains("Ports", output);

        // Summary
        (output, yaml) = await ExecuteAsync("switches", "summary");
        Assert.Contains("sw01", output);
        Assert.Contains("sw02", output);
        Assert.Contains("Netgear GS108", output);
        Assert.Contains("TP-Link TL-SG108E", output);
        Assert.Contains("Managed", output);
        Assert.Contains("PoE", output);
        Assert.Contains("Max Speed", output);

        // Delete switch
        (output, yaml) = await ExecuteAsync("switches", "del", "sw02");
        Assert.Equal("""
                     Switch 'sw02' deleted.

                     """, output);

        // List again
        (output, yaml) = await ExecuteAsync("switches", "list");
        Assert.Contains("sw01", output);
        Assert.Contains("Netgear GS108", output);
        Assert.Contains("Model", output);
        Assert.Contains("Managed", output);
        Assert.Contains("PoE", output);
    }
}
