using Microsoft.Playwright;

namespace Books.E2E.Tests.Pages;

/// <summary>
/// Page object for the sidebar navigation.
/// Handles menu navigation across all pages.
/// </summary>
public class SidebarPage : BasePage
{
    public SidebarPage(IPage page) : base(page)
    {
    }

    /// <summary>
    /// Navigates to Dashboard.
    /// </summary>
    public async Task NavigateToDashboard()
    {
        await ClickMenuItem("Dashboard");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Navigates to Orders page (Ordrer).
    /// </summary>
    public async Task NavigateToOrders()
    {
        await ClickMenuItem("Ordrer");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Navigates to Invoices page (Fakturaer).
    /// </summary>
    public async Task NavigateToInvoices()
    {
        await ClickMenuItem("Fakturaer");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Navigates to Customers page (Kunder).
    /// </summary>
    public async Task NavigateToCustomers()
    {
        await ClickMenuItem("Kunder");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Navigates to Products page (Produkter).
    /// </summary>
    public async Task NavigateToProducts()
    {
        await ClickMenuItem("Produkter");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Navigates to Chart of Accounts page (Kontooversigt).
    /// </summary>
    public async Task NavigateToChartOfAccounts()
    {
        await ClickMenuItem("Kontooversigt");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Navigates to Journal page (Kassekladde).
    /// </summary>
    public async Task NavigateToJournal()
    {
        await ClickMenuItem("Kassekladde");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Navigates to Quick Booking page (Hurtig bogfoering).
    /// </summary>
    public async Task NavigateToQuickBooking()
    {
        await ClickMenuItem("Hurtig bogfoering");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Navigates to Bank Reconciliation page (Bankafstemning).
    /// </summary>
    public async Task NavigateToBankReconciliation()
    {
        await ClickMenuItem("Bankafstemning");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Navigates to VAT Reporting page (Momsindberetning).
    /// </summary>
    public async Task NavigateToVatReporting()
    {
        await ClickMenuItem("Momsindberetning");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Navigates to Export page (Eksport).
    /// </summary>
    public async Task NavigateToExport()
    {
        await ClickMenuItem("Eksport");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Navigates to Settings page (Indstillinger).
    /// </summary>
    public async Task NavigateToSettings()
    {
        await ClickMenuItem("Indstillinger");
        await WaitForNetworkIdleAsync();
    }

    /// <summary>
    /// Clicks a menu item in the sidebar by its label text.
    /// </summary>
    private async Task ClickMenuItem(string label)
    {
        // Ant Design menu item selector
        var menuItem = Page.Locator($".ant-menu-item:has-text('{label}'), .ant-menu-submenu-title:has-text('{label}')");
        await menuItem.First.ClickAsync();
    }

    /// <summary>
    /// Checks if a menu item is currently selected/active.
    /// </summary>
    public async Task<bool> IsMenuItemActive(string label)
    {
        var menuItem = Page.Locator($".ant-menu-item-selected:has-text('{label}')");
        return await menuItem.IsVisibleAsync();
    }

    /// <summary>
    /// Gets the currently active menu item label.
    /// </summary>
    public async Task<string?> GetActiveMenuItemLabel()
    {
        var activeItem = Page.Locator(".ant-menu-item-selected");
        if (await activeItem.IsVisibleAsync())
        {
            return await activeItem.TextContentAsync();
        }
        return null;
    }
}
