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

namespace Books.E2E.Tests.Tests.Orders;

/// <summary>
/// E2E tests for creating orders.
/// Tests the complete order creation workflow.
///
/// Prerequisites:
/// - Backend and frontend running
/// - At least one company exists
/// - At least one customer exists in the company
/// </summary>
[Trait("Category", "E2E")]
public class CreateOrderTests : E2ETestBase
{
    public CreateOrderTests(
        E2ETestFixture fixture,
        PlaywrightFixture playwright)
        : base(fixture, playwright)
    {
    }

    [Fact]
    public async Task CreateOrder_WithCustomer_CreatesOrderSuccessfully()
    {
        // Arrange
        await Page.GotoAsync($"{Config.FrontendUrl}/ordrer");
        var ordersPage = new OrdersPage(Page);
        await ordersPage.WaitForPageToLoad();

        var initialOrderCount = await ordersPage.GetOrderCount();

        // Act - Open create modal
        await ordersPage.ClickNewOrder();

        var createModal = new CreateOrderModalPage(Page);
        await createModal.WaitForModalToOpen();

        // Select first available customer (type to search)
        await createModal.SelectCustomer("");

        // Set dates
        await createModal.SetOrderDate(DateTime.Today);
        await createModal.SetExpectedDeliveryDate(TestDataGenerator.GenerateFutureDeliveryDate());

        // Add reference and notes
        await createModal.EnterReference(TestDataGenerator.GenerateReference());
        await createModal.EnterNotes(TestDataGenerator.GenerateNotes());

        // Submit
        await createModal.Submit();

        // Assert - Order drawer should open
        var drawer = new OrderDrawerPage(Page);
        await drawer.WaitForDrawerToOpen();

        var orderNumber = await drawer.GetOrderNumber();
        orderNumber.Should().NotBeNullOrEmpty("Order number should be assigned");

        var status = await drawer.GetOrderStatus();
        status.Should().Contain("Kladde", "New order should be in draft status");
    }

    [Fact]
    public async Task CreateOrder_WithoutCustomer_ShowsValidationError()
    {
        // Arrange
        await Page.GotoAsync($"{Config.FrontendUrl}/ordrer");
        var ordersPage = new OrdersPage(Page);
        await ordersPage.WaitForPageToLoad();

        // Act
        await ordersPage.ClickNewOrder();

        var createModal = new CreateOrderModalPage(Page);
        await createModal.WaitForModalToOpen();

        // Don't select a customer, just try to submit
        await createModal.Submit();

        // Assert
        (await createModal.HasValidationErrors()).Should().BeTrue("Should show validation error for missing customer");
    }

    [Fact]
    public async Task CreateOrder_CanCancelModal()
    {
        // Arrange
        await Page.GotoAsync($"{Config.FrontendUrl}/ordrer");
        var ordersPage = new OrdersPage(Page);
        await ordersPage.WaitForPageToLoad();

        // Act
        await ordersPage.ClickNewOrder();

        var createModal = new CreateOrderModalPage(Page);
        await createModal.WaitForModalToOpen();

        await createModal.Cancel();

        // Assert
        (await createModal.IsVisible()).Should().BeFalse("Modal should be closed after cancel");
    }
}
