import { test, expect } from "@playwright/test";
import accounts from "../../../account.json";
import { PW } from "../../../src/utils/PW";
import { createAuthContext } from "../../../src/utils/auth-helper";
import { uc19RegisterURLPath } from "../uc19.default.data";
const BASE_URL = process.env.BASE_URL!;
const USE_ALL_ACCOUNTS = false;

const permissionCases = [
  { role: "librarian", expectCreate: true, label: "Thủ thư" },
  { role: "admin", expectCreate: true, label: "Admin" },
];

for (const { role } of permissionCases) {
  const filtered = accounts.filter((acc) => acc.role === role);
  const accountsToTest = USE_ALL_ACCOUNTS ? filtered : filtered.slice(0, 1);

  test.describe(`UC19 1.1.2 Tìm kiếm & Danh sách options`, () => {
    for (const account of accountsToTest) {
      test(`${account.des} ${account.email}: Tổng hợp tìm kiếm option và danh sách options`, async ({
        browser,
      }) => {
        const context = await createAuthContext(browser, account);
        const page = await context.newPage();
        const pw = new PW(page);
        const errors: string[] = [];

        try {
          await page.goto(`${BASE_URL}${uc19RegisterURLPath}`);
          await pw.isVisible("btn-create-hstl", 120000);
          await pw.wait(10000);
          await pw.clickButton("btn-create-hstl");

          // ── Tìm kiếm option ──

          // TC 1.1.2.11: [Thư mục lưu trữ] Tính năng tìm kiếm option
          await test.step("1.1.2.14 - [Thư mục lưu trữ] Tìm kiếm option", async () => {
            try {
              await pw.checkSearchTreeDropdownHasOption(
                "tree-sel-mucLuuTru",
                "Hồ sơ Phòng ban",
              );
            } catch (e) {
              errors.push(`[1.1.2.14] ${(e as Error).message}`);
            }
          });

          // TC 1.1.2.16: [Công ty. Tổ chức] Tính năng tìm kiếm option
          await test.step("1.1.2.19 - [Công ty. Tổ chức] Tìm kiếm option", async () => {
            try {
              await pw.checkSearchDropdownHasOption(
                "sel-companyInvestor",
                "Công ty TNHH Mặt Trời Phú Quốc",
              );
            } catch (e) {
              errors.push(`[1.1.2.19] ${(e as Error).message}`);
            }
          });

          // TC 1.1.2.19: [Đơn vị sở hữu] Tính năng tìm kiếm option
          await test.step("1.1.2.22 - [Đơn vị sở hữu] Tìm kiếm option", async () => {
            try {
              await pw.checkSearchTreeDropdownHasOption(
                "tree-sel-owner-department",
                "Sáng lập tập đoàn",
              );
            } catch (e) {
              errors.push(`[1.1.2.22] ${(e as Error).message}`);
            }
          });

          // TC 1.1.2.24: [Loại Hồ sơ] Tính năng tìm kiếm option
          await test.step("1.1.2.27 - [Loại Hồ sơ] Tìm kiếm option", async () => {
            try {
              await pw.checkSearchDropdownHasOption(
                "sel-loaiBoHoSo",
                "Hồ sơ phòng ban",
              );
            } catch (e) {
              errors.push(`[1.1.2.27] ${(e as Error).message}`);
            }
          });

          // TC 1.1.2.33: [Độ mật] Tính năng tìm kiếm option
          await test.step("1.1.2.36 - [Độ mật] Tìm kiếm option", async () => {
            try {
              await pw.checkSearchDropdownHasOption(
                "sel-securityLevel",
                "Tuyệt mật",
              );
            } catch (e) {
              errors.push(`[1.1.2.36] ${(e as Error).message}`);
            }
          });

          // TC 1.1.2.41: [Cấu trúc HS] Tính năng tìm kiếm option
          await test.step("1.1.2.44 - [Cấu trúc HS] Tìm kiếm option", async () => {
            try {
              await pw.checkSearchDropdownHasOption(
                "sel-cauTrucHoSo",
                "Cấu trúc hồ sơ bất động sản vFinal",
              );
            } catch (e) {
              errors.push(`[1.1.2.44] ${(e as Error).message}`);
            }
          });

          // TC 1.1.2.47: [Trạng thái hiển thị] Tính năng tìm kiếm option
          await test.step("1.1.2.50 - [Trạng thái hiển thị] Tìm kiếm option", async () => {
            try {
              await pw.checkSearchDropdownHasOption("sel-trangThai", "Private");
            } catch (e) {
              errors.push(`[1.1.2.50] ${(e as Error).message}`);
            }
          });

          // TC 1.1.2.50: [Thời hạn lưu trữ] Tính năng tìm kiếm option
          await test.step("1.1.2.53 - [Thời hạn lưu trữ] Tìm kiếm option", async () => {
            try {
              await pw.checkSearchDropdownHasOption(
                "sel-storageTerm",
                "20 năm",
              );
            } catch (e) {
              errors.push(`[1.1.2.53] ${(e as Error).message}`);
            }
          });

          // ── Danh sách options ──

          // TC 1.1.2.34: [Độ mật] Danh sách options theo điều kiện
          await test.step("1.1.2.37 - [Độ mật] Danh sách options", async () => {
            try {
              await pw.checkListValueDropdownList("sel-securityLevel", [
                "Thường",
                "Mật",
                "Tuyệt mật",
              ]);
            } catch (e) {
              errors.push(`[1.1.2.37] ${(e as Error).message}`);
            }
          });

          // TC 1.1.2.48: [Trạng thái hiển thị] Danh sách options theo điều kiện
          await test.step("1.1.2.51 - [Trạng thái hiển thị] Danh sách options", async () => {
            try {
              await pw.checkListValueDropdownList("sel-trangThai", [
                "Public",
                "Private",
              ]);
            } catch (e) {
              errors.push(`[1.1.2.51] ${(e as Error).message}`);
            }
          });

          // TC 1.1.2.54: [Thời hạn lưu trữ] Danh sách options theo điều kiện
          await test.step("1.1.2.54 - [Thời hạn lưu trữ] Danh sách options", async () => {
            try {
              await pw.checkListValueDropdownList("sel-storageTerm", [
                "Vĩnh viễn",
                "5 năm",
                "10 năm",
                "20 năm",
              ]);
            } catch (e) {
              errors.push(`[1.1.2.54] ${(e as Error).message}`);
            }
          });
        } finally {
          await context.close();
        }

        if (errors.length > 0) {
          throw new Error(`Các test case thất bại:\n${errors.join("\n")}`);
        }
      });
    }
  });
}
