using Microsoft.Playwright;

namespace ScadaLink.CentralUI.PlaywrightTests;

[Collection("Playwright")]
public class NavigationTests
{
    private readonly PlaywrightFixture _fixture;

    public NavigationTests(PlaywrightFixture fixture)
    {
        _fixture = fixture;
    }

    [Fact]
    public async Task Dashboard_IsVisibleAfterLogin()
    {
        var page = await _fixture.NewAuthenticatedPageAsync();

        // The nav sidebar should be visible with the brand
        await Expect(page.Locator(".brand")).ToHaveTextAsync("ScadaLink");
        // The nav should contain "Dashboard" link (exact match to avoid "Health Dashboard")
        await Expect(page.GetByRole(AriaRole.Link, new() { Name = "Dashboard", Exact = true })).ToBeVisibleAsync();
    }

    [Theory]
    [InlineData("Sites", "/admin/sites")]
    [InlineData("Connections", "/admin/connections")]
    [InlineData("API Keys", "/admin/api-keys")]
    [InlineData("LDAP Mappings", "/admin/ldap-mappings")]
    public async Task AdminNavLinks_NavigateCorrectly(string linkText, string expectedPath)
    {
        var page = await _fixture.NewAuthenticatedPageAsync();
        await ClickNavAndWait(page, linkText, expectedPath);
    }

    [Theory]
    [InlineData("Templates", "/design/templates")]
    [InlineData("Shared Scripts", "/design/shared-scripts")]
    [InlineData("External Systems", "/design/external-systems")]
    public async Task DesignNavLinks_NavigateCorrectly(string linkText, string expectedPath)
    {
        var page = await _fixture.NewAuthenticatedPageAsync();
        await ClickNavAndWait(page, linkText, expectedPath);
    }

    [Theory]
    [InlineData("Topology", "/deployment/topology")]
    [InlineData("Deployments", "/deployment/deployments")]
    public async Task DeploymentNavLinks_NavigateCorrectly(string linkText, string expectedPath)
    {
        var page = await _fixture.NewAuthenticatedPageAsync();
        await ClickNavAndWait(page, linkText, expectedPath);
    }

    [Theory]
    [InlineData("Health Dashboard", "/monitoring/health")]
    [InlineData("Event Logs", "/monitoring/event-logs")]
    [InlineData("Parked Messages", "/monitoring/parked-messages")]
    [InlineData("Audit Log", "/monitoring/audit-log")]
    public async Task MonitoringNavLinks_NavigateCorrectly(string linkText, string expectedPath)
    {
        var page = await _fixture.NewAuthenticatedPageAsync();
        await ClickNavAndWait(page, linkText, expectedPath);
    }

    private static async Task ClickNavAndWait(IPage page, string linkText, string expectedPath)
    {
        await page.Locator($"nav a:has-text('{linkText}')").ClickAsync();
        await PlaywrightFixture.WaitForPathAsync(page, expectedPath);
        Assert.Contains(expectedPath, page.Url);
    }

    private static ILocatorAssertions Expect(ILocator locator) =>
        Assertions.Expect(locator);
}
