import { test } from '@core/fixtures';
import { users } from '@/tests/KB/const';
import { kbVar } from '@/config/kb';
import { kbEntityManager } from '@/test-data/KB';
import { roleManager } from '@/core/api/classes';

const { authorized: user } = users;
const { new: RPO, updated: RPO_updated } = kbVar.val.bia.rpo.flow;

test.beforeAll(async ({ api: { login } }) => {
  await login();
  await roleManager.makeUserSuperuser(user.username);
});

test.afterAll(async ({ api: { login } }) => {
  await login();
  await kbEntityManager.flush();
});

test('RPO flow', async ({
  page,
  widgetHelper: w,
  actions: { loginAsUser },
}) => {
  await page.goto('/');
  await loginAsUser(user.username, user.password);

  await test.step('Create', async () => {
    await w.tile({ label: 'Regulace' }).click();
    await w.tile({ label: 'BIA Tool' }).click();
    await w.tile({ label: 'RPO' }).click();
    await w.table().singleAction({ label: 'Přidat' }).click();
    await w.textInput({ label: 'Kód' }).change(RPO.code);
    await w.textInput({ label: 'Label' }).change(RPO.label);
    await w.numberInput({ label: 'Ranking' }).change(RPO.ranking);
    await w.textInput({ label: 'BIA AGG' }).change(RPO.label);
    await w.button({ variant: 'primary' }).click();
    await w.header({ text: 'RPO' }).isVisible();
  });

  await test.step('Update', async () => {
    await w.textInput({ label: 'Label' }).change(RPO_updated.label);
    await w.numberInput({ label: 'Ranking' }).change(RPO_updated.ranking);
    await w.textInput({ label: 'BIA AGG' }).change(RPO_updated.bia_agg);
    await w.textInput({ label: 'BIA AGG' }).saved();
  });

  await page.goto('/gui/Rpo');

  await test.step('Delete', async () => {
    await w.table().child.text({ text: RPO.code }).l.first().click();
    await w.button({ variant: 'danger' }).click();
    await w.button({ variant: 'danger' }).toBeDisabled();
    //delayed button becomes outline after debounce
    await w.button({ variant: 'outline-danger' }).notToBeDisabled();
    await w.button({ variant: 'outline-danger' }).click();

    await w.table().isVisible();
    await w.table().child.text({ text: RPO.code }).isNotVisible();
  });
});
