using Microsoft.Playwright;

namespace Books.E2E.Tests.Pages;

/// <summary>
/// Page object for the Customers page (Kunder).
/// </summary>
public class CustomersPage : BasePage
{
    private readonly ILocator _pageTitle;
    private readonly ILocator _newCustomerButton;
    private readonly ILocator _searchInput;
    private readonly ILocator _customersTable;
    private readonly ILocator _loadingSpinner;
    private readonly ILocator _emptyState;

    public CustomersPage(IPage page) : base(page)
    {
        _pageTitle = page.Locator("h4:has-text('Kunder')");
        _newCustomerButton = page.GetByRole(AriaRole.Button, new() { Name = "Ny kunde" });
        _searchInput = page.GetByPlaceholder("Soeg kunde...");
        _customersTable = page.Locator(".ant-table");
        _loadingSpinner = page.Locator(".ant-spin");
        _emptyState = page.Locator("[class*='empty-state'], .ant-empty");
    }

    /// <summary>
    /// Waits for the Customers page to load completely.
    /// </summary>
    public async Task WaitForPageToLoad()
    {
        await _pageTitle.WaitForAsync(new() { Timeout = 10000 });
        await WaitForNetworkIdleAsync();
        await _loadingSpinner.WaitForAsync(new() { State = WaitForSelectorState.Hidden, Timeout = 10000 });
    }

    /// <summary>
    /// Checks if the page title is visible.
    /// </summary>
    public async Task<bool> IsTitleVisible()
    {
        return await _pageTitle.IsVisibleAsync();
    }

    /// <summary>
    /// Clicks the "Ny kunde" button to create a new customer.
    /// </summary>
    public async Task ClickNewCustomer()
    {
        await _newCustomerButton.ClickAsync();
        await Page.Locator(".ant-modal, .ant-drawer").WaitForAsync(new() { Timeout = 5000 });
    }

    /// <summary>
    /// Searches for customers by text.
    /// </summary>
    public async Task SearchCustomers(string searchText)
    {
        await _searchInput.ClearAsync();
        await _searchInput.FillAsync(searchText);
        await Task.Delay(300);
    }

    /// <summary>
    /// Gets the number of customers displayed in the table.
    /// </summary>
    public async Task<int> GetCustomerCount()
    {
        var rows = _customersTable.Locator("tbody tr.ant-table-row");
        return await rows.CountAsync();
    }

    /// <summary>
    /// Checks if the empty state is visible.
    /// </summary>
    public async Task<bool> IsEmptyStateVisible()
    {
        return await _emptyState.IsVisibleAsync();
    }

    /// <summary>
    /// Checks if the customers table is visible.
    /// </summary>
    public async Task<bool> IsTableVisible()
    {
        return await _customersTable.IsVisibleAsync();
    }

    /// <summary>
    /// Gets all customer names from the table.
    /// </summary>
    public async Task<IReadOnlyList<string>> GetCustomerNames()
    {
        // Customer name is typically in the second column
        var cells = _customersTable.Locator("tbody tr.ant-table-row td:nth-child(2)");
        return await cells.AllTextContentsAsync();
    }

    /// <summary>
    /// Clicks the view/edit button for a customer by name.
    /// </summary>
    public async Task ViewCustomer(string customerName)
    {
        var row = _customersTable.Locator($"tr:has-text('{customerName}')");
        var viewButton = row.Locator("button").First;
        await viewButton.ClickAsync();
        await Page.Locator(".ant-drawer, .ant-modal").WaitForAsync(new() { Timeout = 5000 });
    }
}
