import { test, expect } from '../../fixtures';
import { LoginPage } from '../../pom/login.page';
import { ReqPage } from '../../pom/request.page';

test.describe('Verify feature Search', () => {
    let loginPage: LoginPage;
    let reqPage: ReqPage;

    test.beforeEach(async ({ page, conf }) => {
        loginPage = new LoginPage(page);
        reqPage = new ReqPage(page);

        await loginPage.open();
        await loginPage.login(conf.data.username, conf.data.password);
        await expect(loginPage.baseLoc.dashboardContainer).toBeVisible();

        await reqPage.open();
        await expect(reqPage.reqLoc.pageTitle).toBeVisible();
    });

    test('REQ_003 - Verify feature Search', {
        tag: ['@REQ_003', '@request', '@function']
    }, async ({ conf }) => {
        for (const [key, value] of Object.entries(conf.data.search_field as Record<string, any>)) {
            await test.step(`Search with invalid ${key}`, async () => {
                await reqPage.search(value.invalid_data);
                await expect(reqPage.dashboardLoc.table.noResult).toBeVisible();
            });
        }

        for (const [key, value] of Object.entries(conf.data.search_field as Record<string, any>)) {
            await test.step(`Search with valid ${key}`, async () => {
                await reqPage.search(value.valid_data);
                if (key === 'doi' || key === 'dob' || key === 'doi_month_day_numeric' || key === 'doi_full_date' ) {
                    expect(async () => {
                        const rowCount = await reqPage.getRowCount();
                        expect(rowCount).toBeGreaterThan(0);
                        await expect(reqPage.dashboardLoc.table.noResult).not.toBeVisible();
                    }).toPass();
                } else {
                    await expect(reqPage.dashboardLoc.table.itemInRow(value.valid_data).first()).toBeVisible();
                }
            });
        }
    });
});
