import { test } from '@core/fixtures';
import { users } from '../../const';
import { Entity } from '@/core/api/types';
import { kbEntityManager } from '@/test-data/KB';
import { roleManager } from '@/core/api/classes';

let company = {} as Entity;
let bia = {} as Entity;
let person = {} as Entity;

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

  company = await kbEntityManager.create('Company');
  bia = await kbEntityManager.create('ProcesniBia', { firma: company.gid });
  person = await kbEntityManager.create('EaPerson');
});

test.afterAll(async () => await kbEntityManager.flush());

test('Bia Admin moves through all workflows', async ({
  page,
  widgetHelper: w,

  actions: { loginAsUser, logOut },
}) => {
  await test.step('All stages', async () => {
    const { authorized: user } = users;

    await page.goto('/');
    await loginAsUser(user.username, user.password);
    await w.tile({ label: 'Regulace' }).click();
    await w.tile({ label: 'BIA Tool' }).click();
    await w.tile({ label: 'BIA Procesní' }).click();
    await w.dataTable().singleAction({ icon: 'arrows-rotate' }).click(); // update cache just in case

    await w
      .dataTable()
      .cell({ text: '100', exact: true, columnId: 'stav' })
      .click();

    const workflow = w.workflow();
    await workflow.toggle();
    await workflow.iterateStates([
      100, 200, 300, 400, 500, 900, 100, 200, 100, 800, 900, 800, 100, 200,
    ]);
  });
  await test.step('Authorized user 200->100', async () => {
    const { architect: user } = users;
    await logOut();
    //Login as admin and select new BCM manager
    await page.goto('/');
    await loginAsUser(user.username, user.password);

    await w.tile({ label: 'Regulace' }).click();
    await w.tile({ label: 'BIA Tool' }).click();
    await w.tile({ label: 'BIA Procesní' }).click();
    await w.dataTable().singleAction({ icon: 'arrows-rotate' }).click(); // update cache just in case
    await w
      .dataTable()
      .cell({ text: '200', exact: true, columnId: 'stav' })
      .click();

    const bcmManagers = w.multiSearchInput({ fieldName: 'bcmManagers' });
    await bcmManagers.clear(true);
    await bcmManagers.selectFirstBySearch(person.lastName);

    await page.waitForTimeout(2000); // can't logout before input change. probably FE issue
    await logOut();
    //Login as new BCM manager and move through workflows
    await loginAsUser(user.username, user.password);

    const workflow = w.workflow();
    await workflow.toggle();
    await workflow.iterateStates([200, 100]);
  });
});

// Can't create person for this test since it's readonly

test('Unauthorized user tries to move through workflow', async ({
  page,
  widgetHelper: w,
  actions: { loginAsUser },
}) => {
  const { unauthorized } = users;

  await page.goto('/');
  await loginAsUser(unauthorized.username, unauthorized.password);
  await w.tile({ label: 'Regulace' }).click();
  await w.tile({ label: 'BIA Tool' }).click();
  await w.tile({ label: 'BIA Procesní' }).click();
  await w.dataTable().singleAction({ icon: 'arrows-rotate' }).click(); // update cache just in case
  await w.dataTable().cell({ text: bia.nameCz, columnId: 'nazev' }).click();

  const workflow = w.workflow();
  await workflow.isState(100);
  await workflow.toggle();
  await workflow.hasNoTransitions();
});
