import { test, expect, type BrowserContext, type Page } from '@playwright/test';
import { RegistrationPage } from '../../pages/RegistrationPage';
import { waitForActivationCode } from '../../utils/ActivationCodeUtils';

let context: BrowserContext;
let page: Page;
let registrationPage: RegistrationPage;
let email: string;
let password: string;

test.describe.serial('Business Users / B2 User', () => {
    test.describe.configure({ timeout: 180000 });

    test.beforeAll(async ({ browser }) => {
        context = await browser.newContext();
        page = await context.newPage();
        registrationPage = new RegistrationPage(page);

        email = `automation.b2+${Date.now()}@datafab.ai`;
        password = process.env.USER_PASSWORD?.trim() || 'Password123!';

        await registrationPage.gotoSignIn();
        await registrationPage.openSignUpFromSignIn();
        await registrationPage.submitSignUpCredentials(email, password);
        await registrationPage.submitFullName('Automation B Two User');

        await registrationPage.waitForVerificationScreen();
        const code = await waitForActivationCode(email, { timeoutMs: 90000, intervalMs: 3000 });
        await registrationPage.enterVerificationCode(code);
        await registrationPage.clickVerify();
    });

    test.afterAll(async () => {
        await context?.close();
    });

    test('should register and activate successfully', async () => {
        await expect(page).toHaveURL(/\/sign-in\/account/i, { timeout: 30000 });
        await expect(page.locator('body')).not.toContainText(/please check your email/i, { timeout: 10000 });
    });

    test('should remain on the account selection milestone after activation', async () => {
        await expect(page).toHaveURL(/\/sign-in\/account/i, { timeout: 10000 });
        await expect(page.locator('body')).toContainText(/What best describes you\?/i, { timeout: 30000 });
    });

    test('should show both account type options', async () => {
        await expect(registrationPage.individualUserOption).toBeVisible({ timeout: 30000 });
        await expect(registrationPage.businessUserOption).toBeVisible({ timeout: 30000 });
    });

    test('should clear the session and log in again with the same user', async () => {
        const browser = context.browser();
        if (!browser) {
            throw new Error('Browser instance is not available for creating a new session.');
        }

        await context.close();
        context = await browser.newContext();
        page = await context.newPage();
        registrationPage = new RegistrationPage(page);

        await registrationPage.gotoSignIn();
        await registrationPage.loginFromSignIn(email, password);

        await expect(page).toHaveURL(/\/sign-in\/account/i, { timeout: 30000 });
        await expect(page.locator('body')).toContainText(/What best describes you\?/i, { timeout: 30000 });
    });

    test('should not make the chat view available after logging in again', async () => {
        await expect(page.getByPlaceholder('Ask anything...')).not.toBeVisible({ timeout: 5000 });
        await expect(page.locator('body')).toContainText(/What best describes you\?/i, { timeout: 30000 });
    });
});
