import { kbVar } from '@/config/kb/var';
import { test, expect } 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;

test.beforeAll(async ({ api: { login } }) => {
  await login();
  // await roleManager.makeUserSuperuser(users.authorized.username);
  company = await kbEntityManager.create('Company');
});

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

test('Create Bia', async ({
  page,
  widgetHelper: w,
  actions: { loginAsUser },
}) => {
  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({ label: 'Přidat' }).click();
  await w.selectInput({ label: 'Společnost:' }).select(company.kod);
  await w.textInput({ label: 'Název' }).change(kbVar.val.BIA);
  await w.button({ label: 'Vytvořit' }).click();
  await expect(page.locator('body')).toContainText(kbVar.val.BIA);
});

test('Create Bia (Unauthorized attempt)', async ({
  page,
  widgetHelper: w,
  layoutHeper: l,
  actions: { loginAsUser },
}) => {
  const { unauthorized: 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({ label: 'Přidat' }).isNotVisible();
  await page.goto(kbVar.nav.procesniBia.create);

  await expect(page.getByTestId('Společnost:')).toBeVisible();
  await w.selectInput({ label: 'Společnost:' }).select(company.kod);
  await w.textInput({ label: 'Název' }).change(kbVar.val.BIA);
  await w.button({ label: 'Vytvořit' }).click();

  await l.globalAlert.hasAlert({ variant: 'danger' });
});
