import { test, expect } from '../../../fixtures/index';
import { LoginPage } from '../../../pom/login.page';
import { GeneralPage } from '../../../pom/setting/general.page';

test.describe('UI case for general tests', async () => {
  let loginPage: LoginPage;
  let generalPage: GeneralPage;

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

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

    await generalPage.open();
  });

  test('SETTING_011 - Verify UI Hashtags when empty state', {
    tag: ['@SETTING_011', '@general', '@ui']
  }, async ({ page }) => {
    await expect(page.locator('#root')).toMatchAriaSnapshot(`- text: No matching results found. Try again.`);
  });

  test('SETTING_012 - Verify UI Hashtags when have case', {
    tag: ['@SETTING_012', '@general', '@ui']
  }, async ({ page }) => {
    await expect(page.locator('#root')).toMatchAriaSnapshot(`
    - heading "Hashtags" [level=2]
    - button "Add New"
    `);

    await expect(page.locator('#root')).toMatchAriaSnapshot(`
    - text: Show
    - textbox: /\\d+/
    - text: entries
    - img
    - textbox "Search..."
    `);

    await expect(async () => {
      const rowCount = await generalPage.getTableRowCount();
      expect(rowCount).toBeGreaterThan(1);
    }).toPass();

    // Verify default columns visible
    const defaultColumns = ['Hashtag', 'Category', 'Description', 'Total', 'Actions'];
    for (let i = 0; i < defaultColumns.length; i++) {
      await expect(generalPage.dashboardLoc.table.headingColumn(defaultColumns[i])).toBeVisible();
    }
  });

  test('SETTING_013 - Verify feature Search Hashtags', {
    tag: ['@SETTING_013', '@general', '@ui']
  }, async ({ page, conf }) => {
    await test.step('Verify default have more than 1 row', async () => {
      await expect(async () => {
        const rowCount = await generalPage.getTableRowCount();
        expect(rowCount).toBeGreaterThan(1);
      }).toPass();
    });

    await test.step('Search by exact hashtag', async () => {
      await generalPage.search(conf.data.hashtag);
      await expect(page.locator(`text=${conf.data.hashtag}`)).toBeVisible();
      await expect(page.locator(`//a[text()='${conf.data.category}']`).first()).toBeVisible();
      await expect(page.locator(`text=${conf.data.description}`).first()).toBeVisible();

      await expect(async () => {
        const rowCount = await generalPage.getTableRowCount();
        expect(rowCount).toBe(1);
      }).toPass();
    });

    await test.step('Search by hashtag keyword and no result', async () => {
      await generalPage.search("invalid_key_word");

      await expect(page.locator('#root')).toMatchAriaSnapshot(`- text: No matching results found. Try again.`);

      await expect(async () => {
        const rowCount = await generalPage.getTableRowCount();
        expect(rowCount).toBe(0);
      }).toPass();
    });
  });
});