import { test, expect } from "../fixtures/audit-log";
import { AuditEntryDetailPage } from "../pages/audit/audit-entry-detail";
import { AuditEntryListPage } from "../pages/audit/audit-entry-list";
import { LoginPage } from "../pages/auth/login";

test.describe("user--view-audit-log", () => {
  test.beforeEach(async ({ page, User }) => {
    const loginPage = new LoginPage(page);
    await loginPage.login(User.email, User.password);
  });

  test("View all audit log entries without any filter applied", async ({ page, AuditRole }) => {
    const listPage = new AuditEntryListPage(page);
    await listPage.goto();

    await expect(listPage.heading).toBeVisible();
    await expect(listPage.table).toBeVisible();
    await expect(listPage.getRow(AuditRole.roleId)).toBeVisible();
  });

  test("Role creation is listed in the audit log", async ({ page, AuditRole }) => {
    const listPage = new AuditEntryListPage(page);
    await listPage.goto();

    await expect(listPage.getRow(AuditRole.roleId)).toContainText(/role/i);
    await expect(listPage.getRow(AuditRole.roleId)).toContainText(/create/i);
  });

  test("Role update is listed in the audit log", async ({ page, UpdatedAuditRole }) => {
    const listPage = new AuditEntryListPage(page);
    await listPage.goto();

    await expect(
      listPage.getRow(UpdatedAuditRole.roleId).filter({ hasText: /update/i }),
    ).toBeVisible();
  });

  test("Audit log rows link to the detail page", async ({ page, AuditRole }) => {
    const listPage = new AuditEntryListPage(page);
    await listPage.goto();

    await listPage.getRow(AuditRole.roleId).getByRole("link", { name: /view/i }).click();

    const detailPage = new AuditEntryDetailPage(page);
    await expect(detailPage.heading).toBeVisible();
    await expect(detailPage.entityIdField).toContainText(AuditRole.roleId);
  });
});

test.describe("user--view-audit-log-detail", () => {
  test.beforeEach(async ({ page, User }) => {
    const loginPage = new LoginPage(page);
    await loginPage.login(User.email, User.password);
  });

  test("View detail of an update entry that includes both old and new values", async ({
    page,
    UpdatedAuditRole,
  }) => {
    const listPage = new AuditEntryListPage(page);
    await listPage.goto();

    await listPage
      .getRow(UpdatedAuditRole.roleId)
      .filter({ hasText: /update/i })
      .getByRole("link", { name: /view/i })
      .click();

    const detailPage = new AuditEntryDetailPage(page);
    await expect(detailPage.heading).toBeVisible();
    await expect(detailPage.operationTypeField).toBeVisible();
    await expect(detailPage.changeDetails).toBeVisible();
  });

  test("View detail of a creation entry where no old values exist", async ({ page, AuditRole }) => {
    const listPage = new AuditEntryListPage(page);
    await listPage.goto();

    await listPage
      .getRow(AuditRole.roleId)
      .filter({ hasText: /create/i })
      .getByRole("link", { name: /view/i })
      .click();

    const detailPage = new AuditEntryDetailPage(page);
    await expect(detailPage.heading).toBeVisible();
    await expect(detailPage.operationTypeField).toBeVisible();
    await expect(detailPage.entityTypeField).toBeVisible();
  });
});
