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

namespace Books.E2E.Tests.Actions;

/// <summary>
/// BDD "When" actions - user interactions and actions.
/// </summary>
public class WhenActions
{
    private readonly ActionContext _context;
    private readonly CompanySetupWizardPage _wizardPage;

    internal WhenActions(ActionContext context)
    {
        _context = context;
        _wizardPage = new CompanySetupWizardPage(context.Page);
    }

    /// <summary>
    /// Navigates to the company setup wizard.
    /// </summary>
    public async Task<WhenActions> UserNavigatesToCompanySetup()
    {
        var url = $"{_context.FrontendBaseUrl}/opret-virksomhed";
        Console.WriteLine($"[When] Navigating to {url}");

        await _context.Page.GotoAsync(url);
        await _wizardPage.WaitForWizardToLoad();

        return this;
    }

    /// <summary>
    /// Enters a company name in the wizard.
    /// </summary>
    public async Task<WhenActions> UserEntersCompanyName(string name)
    {
        Console.WriteLine($"[When] Entering company name: {name}");
        _context.CompanyName = name;
        await _wizardPage.EnterCompanyName(name);
        return this;
    }

    /// <summary>
    /// Generates and enters a valid CVR number.
    /// </summary>
    public async Task<WhenActions> UserEntersValidCvr()
    {
        var cvr = CvrGenerator.Generate();
        Console.WriteLine($"[When] Entering valid CVR: {cvr}");
        _context.Cvr = cvr;
        await _wizardPage.EnterCvr(cvr);
        return this;
    }

    /// <summary>
    /// Enters a specific CVR number (may be invalid).
    /// </summary>
    public async Task<WhenActions> UserEntersCvr(string cvr)
    {
        Console.WriteLine($"[When] Entering CVR: {cvr}");
        _context.Cvr = cvr;
        await _wizardPage.EnterCvr(cvr);
        return this;
    }

    /// <summary>
    /// Selects the country in the wizard.
    /// </summary>
    public async Task<WhenActions> UserSelectsCountry(string country)
    {
        Console.WriteLine($"[When] Selecting country: {country}");
        await _wizardPage.SelectCountry(country);
        return this;
    }

    /// <summary>
    /// Selects the currency in the wizard.
    /// </summary>
    public async Task<WhenActions> UserSelectsCurrency(string currency)
    {
        Console.WriteLine($"[When] Selecting currency: {currency}");
        await _wizardPage.SelectCurrency(currency);
        return this;
    }

    /// <summary>
    /// Selects the fiscal year start month (1-12).
    /// </summary>
    public async Task<WhenActions> UserSelectsFiscalYearStartMonth(int month)
    {
        Console.WriteLine($"[When] Selecting fiscal year start month: {month}");
        await _wizardPage.SelectFiscalYearStartMonth(month);
        return this;
    }

    /// <summary>
    /// Sets whether the company is VAT registered.
    /// </summary>
    public async Task<WhenActions> UserSelectsVatRegistered(bool isRegistered)
    {
        Console.WriteLine($"[When] Setting VAT registered: {isRegistered}");
        await _wizardPage.SelectVatRegistered(isRegistered);
        return this;
    }

    /// <summary>
    /// Selects the VAT period frequency.
    /// </summary>
    public async Task<WhenActions> UserSelectsVatPeriod(string period)
    {
        Console.WriteLine($"[When] Selecting VAT period: {period}");
        await _wizardPage.SelectVatPeriod(period);
        return this;
    }

    /// <summary>
    /// Clicks the Next button to advance the wizard.
    /// </summary>
    public async Task<WhenActions> UserClicksNext()
    {
        Console.WriteLine("[When] Clicking Next");
        await _wizardPage.ClickNext();
        return this;
    }

    /// <summary>
    /// Clicks the Back button to go back in the wizard.
    /// </summary>
    public async Task<WhenActions> UserClicksBack()
    {
        Console.WriteLine("[When] Clicking Back");
        await _wizardPage.ClickBack();
        return this;
    }

    /// <summary>
    /// Submits the company creation form.
    /// </summary>
    public async Task<ThenAssertions> UserSubmitsCompanyForm()
    {
        Console.WriteLine("[When] Submitting company form");
        await _wizardPage.SubmitForm();
        return new ThenAssertions(_context);
    }

    /// <summary>
    /// Clicks the "Go to Dashboard" button after successful creation.
    /// </summary>
    public async Task<ThenAssertions> UserClicksGoToDashboard()
    {
        Console.WriteLine("[When] Clicking 'Go to Dashboard'");
        await _wizardPage.ClickGoToDashboard();
        return new ThenAssertions(_context);
    }

    /// <summary>
    /// Chain multiple When actions with "And".
    /// </summary>
    public WhenActions And => this;

    /// <summary>
    /// Transition to Then assertions.
    /// </summary>
    public ThenAssertions Then => new(_context);
}
