using Books.E2E.Tests.Infrastructure;
using Books.E2E.Tests.Pages;

namespace Books.E2E.Tests.Tests.Navigation;

/// <summary>
/// E2E tests for sidebar navigation.
/// Verifies that all main navigation links work correctly.
///
/// Prerequisites:
/// - Backend and frontend running
/// - User has access to at least one company
/// </summary>
[Trait("Category", "E2E")]
public class SidebarNavigationTests : E2ETestBase
{
    public SidebarNavigationTests(
        E2ETestFixture fixture,
        PlaywrightFixture playwright)
        : base(fixture, playwright)
    {
    }

    [Fact]
    public async Task NavigateToDashboard_LoadsSuccessfully()
    {
        // Arrange
        await Page.GotoAsync(Config.FrontendUrl);
        var sidebar = new SidebarPage(Page);

        // Act
        await sidebar.NavigateToDashboard();

        // Assert
        var dashboard = new DashboardPage(Page);
        await dashboard.WaitForDashboardToLoad();
    }

    [Fact]
    public async Task NavigateToOrders_LoadsSuccessfully()
    {
        // Arrange
        await Page.GotoAsync(Config.FrontendUrl);
        var sidebar = new SidebarPage(Page);

        // Act
        await sidebar.NavigateToOrders();

        // Assert
        var ordersPage = new OrdersPage(Page);
        await ordersPage.WaitForPageToLoad();
        (await ordersPage.IsTitleVisible()).Should().BeTrue();
    }

    [Fact]
    public async Task NavigateToInvoices_LoadsSuccessfully()
    {
        // Arrange
        await Page.GotoAsync(Config.FrontendUrl);
        var sidebar = new SidebarPage(Page);

        // Act
        await sidebar.NavigateToInvoices();

        // Assert
        var invoicesPage = new InvoicesPage(Page);
        await invoicesPage.WaitForPageToLoad();
        (await invoicesPage.IsTitleVisible()).Should().BeTrue();
    }

    [Fact]
    public async Task NavigateToCustomers_LoadsSuccessfully()
    {
        // Arrange
        await Page.GotoAsync(Config.FrontendUrl);
        var sidebar = new SidebarPage(Page);

        // Act
        await sidebar.NavigateToCustomers();

        // Assert
        var customersPage = new CustomersPage(Page);
        await customersPage.WaitForPageToLoad();
        (await customersPage.IsTitleVisible()).Should().BeTrue();
    }

    [Fact]
    public async Task NavigateToChartOfAccounts_LoadsSuccessfully()
    {
        // Arrange
        await Page.GotoAsync(Config.FrontendUrl);
        var sidebar = new SidebarPage(Page);

        // Act
        await sidebar.NavigateToChartOfAccounts();

        // Assert
        var pageTitle = Page.Locator("h4:has-text('Kontooversigt')");
        await pageTitle.WaitForAsync(new() { Timeout = 10000 });
        (await pageTitle.IsVisibleAsync()).Should().BeTrue();
    }

    [Fact]
    public async Task NavigateToJournal_LoadsSuccessfully()
    {
        // Arrange
        await Page.GotoAsync(Config.FrontendUrl);
        var sidebar = new SidebarPage(Page);

        // Act
        await sidebar.NavigateToJournal();

        // Assert
        var pageTitle = Page.Locator("h4:has-text('Kassekladde')");
        await pageTitle.WaitForAsync(new() { Timeout = 10000 });
        (await pageTitle.IsVisibleAsync()).Should().BeTrue();
    }

    [Fact]
    public async Task NavigateToVatReporting_LoadsSuccessfully()
    {
        // Arrange
        await Page.GotoAsync(Config.FrontendUrl);
        var sidebar = new SidebarPage(Page);

        // Act
        await sidebar.NavigateToVatReporting();

        // Assert
        var pageTitle = Page.Locator("h4:has-text('Momsindberetning')");
        await pageTitle.WaitForAsync(new() { Timeout = 10000 });
        (await pageTitle.IsVisibleAsync()).Should().BeTrue();
    }

    [Fact]
    public async Task NavigateToSettings_LoadsSuccessfully()
    {
        // Arrange
        await Page.GotoAsync(Config.FrontendUrl);
        var sidebar = new SidebarPage(Page);

        // Act
        await sidebar.NavigateToSettings();

        // Assert
        var pageTitle = Page.Locator("h4:has-text('Indstillinger')");
        await pageTitle.WaitForAsync(new() { Timeout = 10000 });
        (await pageTitle.IsVisibleAsync()).Should().BeTrue();
    }

    [Fact]
    public async Task NavigateBetweenPages_MaintainsState()
    {
        // Arrange
        await Page.GotoAsync(Config.FrontendUrl);
        var sidebar = new SidebarPage(Page);

        // Act - Navigate through multiple pages
        await sidebar.NavigateToOrders();
        await Page.WaitForLoadStateAsync(LoadState.NetworkIdle);

        await sidebar.NavigateToInvoices();
        await Page.WaitForLoadStateAsync(LoadState.NetworkIdle);

        await sidebar.NavigateToCustomers();
        await Page.WaitForLoadStateAsync(LoadState.NetworkIdle);

        await sidebar.NavigateToDashboard();
        await Page.WaitForLoadStateAsync(LoadState.NetworkIdle);

        // Assert - Should end up on dashboard
        var dashboard = new DashboardPage(Page);
        await dashboard.WaitForDashboardToLoad();
    }
}
