using Books.E2E.Tests.Actions;
using Books.E2E.Tests.Infrastructure;

namespace Books.E2E.Tests.Tests.CompanySetup;

/// <summary>
/// E2E tests for the Company Setup Wizard flow.
/// Tests the complete user journey of creating a new company.
///
/// Configure target environment via environment variables:
/// - E2E_BACKEND_URL (default: https://localhost:5001)
/// - E2E_FRONTEND_URL (default: http://localhost:5173)
/// </summary>
[Trait("Category", "E2E")]
public class CompanySetupWizardTests : E2ETestBase
{
    public CompanySetupWizardTests(
        E2ETestFixture fixture,
        PlaywrightFixture playwright)
        : base(fixture, playwright)
    {
    }

    [Fact]
    public async Task CompanySetup_WithValidData_CreatesCompanySuccessfully()
    {
        // Complete happy path through the wizard
        await Actions
            .Given.UserIsAuthenticated()
            .UserNavigatesToCompanySetup()
            // Step 1: Company info
            .UserEntersCompanyName("Test Virksomhed ApS")
            .UserEntersValidCvr()
            .UserClicksNext()
            // Step 2: Accounting settings
            .UserSelectsCountry("DK")
            .UserSelectsCurrency("DKK")
            .UserSelectsFiscalYearStartMonth(1)
            .UserClicksNext()
            // Step 3: VAT settings
            .UserSelectsVatRegistered(false)
            .UserClicksNext()
            // Step 4: Confirmation - Submit
            .UserSubmitsCompanyForm()
            .UserSeesSuccessMessage()
            .CompanyIsCreated();
    }

    [Fact]
    public async Task CompanySetup_WithVatRegistration_CreatesCompanySuccessfully()
    {
        // Test with VAT registration enabled
        await Actions
            .Given.UserIsAuthenticated()
            .UserNavigatesToCompanySetup()
            // Step 1: Company info (CVR required for VAT)
            .UserEntersCompanyName("Moms Virksomhed A/S")
            .UserEntersValidCvr()
            .UserClicksNext()
            // Step 2: Accounting settings
            .UserSelectsCountry("DK")
            .UserSelectsCurrency("DKK")
            .UserSelectsFiscalYearStartMonth(1)
            .UserClicksNext()
            // Step 3: VAT settings
            .UserSelectsVatRegistered(true)
            .UserSelectsVatPeriod("QUARTERLY")
            .UserClicksNext()
            // Step 4: Confirmation - Submit
            .UserSubmitsCompanyForm()
            .UserSeesSuccessMessage()
            .CompanyIsCreated();
    }

    [Fact]
    public async Task CompanySetup_InvalidCvr_ShowsValidationError()
    {
        // Test CVR validation (modulus 11 check)
        await Actions
            .Given.UserIsAuthenticated()
            .UserNavigatesToCompanySetup()
            .UserEntersCompanyName("Test Virksomhed ApS")
            .UserEntersCvr("12345678") // Invalid checksum
            .UserClicksNext()
            .Then()
            .ValidationErrorIsShown("modulus 11");
    }

    [Fact]
    public async Task CompanySetup_CvrTooShort_ShowsValidationError()
    {
        // Test CVR length validation
        await Actions
            .Given.UserIsAuthenticated()
            .UserNavigatesToCompanySetup()
            .UserEntersCompanyName("Test Virksomhed ApS")
            .UserEntersCvr("1234") // Too short
            .UserClicksNext()
            .Then()
            .ValidationErrorIsShown("8 cifre");
    }

    [Fact]
    public async Task CompanySetup_EmptyCompanyName_ShowsValidationError()
    {
        // Test required company name validation
        await Actions
            .Given.UserIsAuthenticated()
            .UserNavigatesToCompanySetup()
            // Don't enter company name
            .UserEntersValidCvr()
            .UserClicksNext()
            .Then()
            .ValidationErrorIsShown("virksomhedsnavn");
    }

    [Fact]
    public async Task CompanySetup_VatRegisteredWithoutCvr_ShowsError()
    {
        // VAT registration requires CVR
        await Actions
            .Given.UserIsAuthenticated()
            .UserNavigatesToCompanySetup()
            .UserEntersCompanyName("Test Virksomhed ApS")
            // No CVR entered
            .UserClicksNext()
            // Step 2: Accounting
            .UserSelectsCountry("DK")
            .UserSelectsCurrency("DKK")
            .UserSelectsFiscalYearStartMonth(1)
            .UserClicksNext()
            // Step 3: VAT - select registered
            .UserSelectsVatRegistered(true)
            .UserSelectsVatPeriod("MONTHLY")
            .UserClicksNext()
            .Then()
            .ErrorMessageIsShown("CVR er påkrævet");
    }

    [Fact]
    public async Task CompanySetup_NavigateBackAndForth_PreservesData()
    {
        // Test that form data is preserved when navigating back
        var companyName = "Navigation Test ApS";

        await Actions
            .Given.UserIsAuthenticated()
            .UserNavigatesToCompanySetup()
            // Step 1: Enter data
            .UserEntersCompanyName(companyName)
            .UserEntersValidCvr()
            .UserClicksNext()
            // Step 2: Enter more data
            .UserSelectsCountry("DK")
            .UserSelectsCurrency("DKK")
            .UserSelectsFiscalYearStartMonth(6) // June
            .UserClicksNext()
            // Step 3: Enter VAT settings
            .UserSelectsVatRegistered(false)
            .UserClicksNext()
            // Now go back and verify data
            .UserClicksBack()
            .UserClicksBack()
            .UserClicksBack()
            // We're back at step 1 - the name should still be there
            .Then()
            .TextIsVisible(companyName);
    }

    [Fact]
    public async Task CompanySetup_AfterSuccess_CanNavigateToDashboard()
    {
        // Test the full flow including navigation to dashboard
        await Actions
            .Given.UserIsAuthenticated()
            .UserNavigatesToCompanySetup()
            .UserEntersCompanyName("Dashboard Test ApS")
            .UserEntersValidCvr()
            .UserClicksNext()
            .UserSelectsCountry("DK")
            .UserSelectsCurrency("DKK")
            .UserSelectsFiscalYearStartMonth(1)
            .UserClicksNext()
            .UserSelectsVatRegistered(false)
            .UserClicksNext()
            .UserSubmitsCompanyForm()
            .UserSeesSuccessMessage();

        // Click go to dashboard
        await Actions
            .Given.UserIsAuthenticated()
            .UserClicksGoToDashboard()
            .UserIsRedirectedToDashboard();
    }
}
