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

const user = users.authorized;

let person = {} as Entity;
let company = {} as Entity;
let orgUnit = {} as Entity;
let logicalDomainRoot = {} as Entity;
let logicalProcessDomain = {} as Entity;
let logicalBusinessObject = {} as Entity;
let capabilityDomain = {} as Entity;
let capability = {} as Entity;

test.beforeAll(async ({ api: { login } }) => {
  await login();
  await roleManager.makeUserSuperuser(user.username);
  const { entity, entities } = await kbEntityManager.createCapability();
  person = entities.person;
  company = entities.company;
  orgUnit = entities.orgUnit;
  logicalDomainRoot = entities.logicalDomainRoot;
  logicalProcessDomain = entities.logicalProcessDomain;
  capabilityDomain = entities.capabilityDomain;
  capabilityDomain = entities.capabilityDomain;
  capability = entity;

  logicalBusinessObject = await kbEntityManager.create(
    'LogicalBusinessObject',
    {
      domainRoot: logicalDomainRoot.gid,
    }
  );
});

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

test('Create Agenda', async ({
  page,
  widgetHelper: w,
  actions: { loginAsUser },
}) => {
  await page.goto('/');
  await loginAsUser(user.username, user.password);
  await w.tile({ label: 'Capability' }).click();
  await w.tile({ label: 'Agendy' }).click();
  await w.dataTable({}).singleAction({ label: 'Přidat' }).click();
  await w
    .searchInput({ label: 'Capabilita' })
    .selectFirstBySearch(capability.nazevCz);
  await w
    .searchInput({ label: 'Business object' })
    .selectFirstBySearch(logicalBusinessObject.nameCz);
  await w.textInput({ label: 'Název Agendy' }).change(kbVar.val.agenda);
  await w.button({ label: 'Vytvořit agendu' }).click();
  await w.header({ text: kbVar.val.agenda }).isVisible();
  await expect(page).toHaveURL(/detail/);
});
