import { Page } from "@playwright/test";
import { test, expect, ACCOUNT } from "../../../src/fixture/base-test";
import { TIMEOUT } from "../../../src/constant/timeout";
import { PW } from "../../../src/utils/PW";
import { uc29DefaultData, uc29DefaultUrl } from "../uc29.default.data";

const BASE_URL = process.env.BASE_URL!;

const runTest = async (page: Page) => {
  const pw = new PW(page);

  await test.step("1. Truy cập màn hình Kho tài liệu", async () => {
    await page.goto(`${BASE_URL}${uc29DefaultUrl}`);
  });
  await test.step("2. Chọn nút Tạo mới", async () => {
    await pw.isVisible("btn-create-hstl", TIMEOUT.PAGE_LOADING);
    await pw.wait(TIMEOUT.HARD_WAITING);
    await pw.clickButton("btn-create-hstl");
  });
  const parent = page
    .locator('[data-testid="btn-add-related-ecm"]')
    .locator("..");
  await test.step("3. Mở pop-up tìm tài liệu liên quan", async () => {
    await parent.locator(".ant-dropdown-trigger").hover();
    await page.getByText("Tài liệu liên quan").click();
    await expect(page.getByText("Tìm tài liệu liên quan"), {
      message:
        "Lỗi: Pop-up tìm tài liệu liên quan không hiển thị sau khi bấm chọn",
    }).toBeVisible({
      timeout: TIMEOUT.DATA_LOADING,
    });
    await pw.wait(TIMEOUT.DATA_LOADING);
  });

  await test.step("4. Chọn tài liệu", async () => {
    const rows = page
      .getByTestId("related-item-table")
      .locator(".cssMaHSTLChild");

    const modalContent = page.locator(".ant-modal-content:visible").last();
    await modalContent
      .locator(".ant-table-row.ant-table-row-level-0")
      .first()
      .hover();
    await modalContent
      .locator(".ant-table-row.ant-table-row-level-0 .ant-checkbox-wrapper")
      .first()
      .click();
    const searchInput = modalContent.locator(
      'input[placeholder="Nhập tên tài liệu, mã tài liệu"]',
    );

    await expect(page.getByTestId("btn-add-related"), {
      message: "Lỗi: Nút 'Thêm' không hiển thị khi đã chọn tài liệu",
    }).toBeVisible({
      timeout: TIMEOUT.CONTROL_LOADING,
    });
    await pw.clickButton("btn-add-related");
    await pw.isVisible("related-item-table");
    const selectedRelatedCode = (
      (await rows.first().textContent()) || ""
    ).trim();
    await parent.locator(".ant-dropdown-trigger").hover();
    await page.getByText("Tài liệu liên quan").click();
    await searchInput.fill(selectedRelatedCode);
    await searchInput.press("Enter");

    const rowsResult = modalContent.locator(
      ".ant-table-row.ant-table-row-level-0",
    );
    await expect(rowsResult, {
      message:
        "Lỗi: vẫn còn hiển thị tài liệu đã được chọn trong danh sách tìm tài liệu liên quan",
    }).toHaveCount(0);
  });
};

test(
  "UC29 1.1.2.7.78 - Ngăn chặn thêm trùng 1 tài liệu đã được liên kết - Thủ thư" +
    ACCOUNT.LIBRARIAN,
  async ({ librarian }) => {
    await runTest(librarian);
  },
);
test(
  "UC29 1.1.2.7.78 - Ngăn chặn thêm trùng 1 tài liệu đã được liên kết - Admin" +
    ACCOUNT.ADMIN,
  async ({ admin }) => {
    await runTest(admin);
  },
);
