// The Lucia project.
// Copyright 2026-2026 DSP, inc.  All rights reserved.
// Authors:
//   imacat.yang@dsp.im (imacat), 2026/03/22

import { test, expect } from "@playwright/test";
import { loginWithMSW } from "../helpers";

test.describe("SweetAlert2 Modals", () => {
  test.describe("File Context Menu - Rename", () => {
    test.beforeEach(async ({ page, context }) => {
      await loginWithMSW(context);
      await page.goto("/files");
      await expect(page.getByText("sample-process.xes").first()).toBeVisible();
    });

    test("right-click on table row shows context menu with Rename", async ({
      page,
    }) => {
      await page
        .locator("table tbody tr")
        .first()
        .click({ button: "right" });
      await expect(page.locator('.p-contextmenu-item-content', { hasText: 'Rename' }).first()).toBeVisible();
    });

    test("right-click context menu shows Download option", async ({
      page,
    }) => {
      await page
        .locator("table tbody tr")
        .first()
        .click({ button: "right" });
      await expect(page.locator('.p-contextmenu-item-content', { hasText: 'Download' }).first()).toBeVisible();
    });

    test("right-click context menu shows Delete option", async ({
      page,
    }) => {
      await page
        .locator("table tbody tr")
        .first()
        .click({ button: "right" });
      await expect(page.locator('.p-contextmenu-item-content', { hasText: 'Delete' }).first()).toBeVisible();
    });

    test("clicking Rename opens SweetAlert rename dialog", async ({
      page,
    }) => {
      await page
        .locator("table tbody tr")
        .first()
        .click({ button: "right" });
      await page.locator('.p-contextmenu-item-content', { hasText: 'Rename' }).first().click();
      // SweetAlert popup should appear with RENAME title
      await expect(page.locator(".swal2-popup")).toBeVisible();
      await expect(
        page.locator(".swal2-title"),
      ).toContainText("RENAME");
      await expect(page.locator(".swal2-input")).toBeVisible();
    });

    test("rename dialog has pre-filled file name", async ({ page }) => {
      await page
        .locator("table tbody tr")
        .first()
        .click({ button: "right" });
      await page.locator('.p-contextmenu-item-content', { hasText: 'Rename' }).first().click();
      const value = await page.locator(".swal2-input").inputValue();
      expect(value).not.toBe("");
    });

    test("rename dialog can be cancelled", async ({ page }) => {
      await page
        .locator("table tbody tr")
        .first()
        .click({ button: "right" });
      await page.locator('.p-contextmenu-item-content', { hasText: 'Rename' }).first().click();
      await expect(page.locator(".swal2-popup")).toBeVisible();
      await page.locator(".swal2-cancel").click();
      await expect(page.locator(".swal2-popup")).not.toBeVisible();
    });

    test("clicking Delete opens SweetAlert delete confirmation", async ({
      page,
    }) => {
      await page
        .locator("table tbody tr")
        .first()
        .click({ button: "right" });
      await page.locator('.p-contextmenu-item-content', { hasText: 'Delete' }).first().click();
      // SweetAlert popup should appear with CONFIRM DELETION
      await expect(page.locator(".swal2-popup")).toBeVisible();
      await expect(
        page.locator(".swal2-title"),
      ).toContainText("CONFIRM DELETION");
    });

    test("delete confirmation shows file name", async ({ page }) => {
      await page
        .locator("table tbody tr")
        .first()
        .click({ button: "right" });
      await page.locator('.p-contextmenu-item-content', { hasText: 'Delete' }).first().click();
      await expect(page.locator(".swal2-popup")).toBeVisible();
      await expect(
        page.locator(".swal2-html-container"),
      ).toContainText("delete");
    });

    test("delete confirmation can be cancelled", async ({ page }) => {
      await page
        .locator("table tbody tr")
        .first()
        .click({ button: "right" });
      await page.locator('.p-contextmenu-item-content', { hasText: 'Delete' }).first().click();
      await expect(page.locator(".swal2-popup")).toBeVisible();
      await page.locator(".swal2-cancel").click();
      await expect(page.locator(".swal2-popup")).not.toBeVisible();
    });
  });

  test.describe("File Context Menu on Grid View", () => {
    test.beforeEach(async ({ page, context }) => {
      await loginWithMSW(context);
      await page.goto("/files");
      await expect(page.getByText("sample-process.xes").first()).toBeVisible();
      // Switch to grid view
      await page.locator("li.cursor-pointer").last().click();
    });

    test("right-click on grid card shows context menu", async ({ page }) => {
      await page.locator("li[title]").first().click({ button: "right" });
      await expect(page.locator('.p-contextmenu-item-content', { hasText: 'Rename' }).first()).toBeVisible();
      await expect(page.locator('.p-contextmenu-item-content', { hasText: 'Delete' }).first()).toBeVisible();
    });

    test("grid card rename opens SweetAlert dialog", async ({ page }) => {
      await page.locator("li[title]").first().click({ button: "right" });
      await page.locator('.p-contextmenu-item-content', { hasText: 'Rename' }).first().click();
      await expect(page.locator(".swal2-popup")).toBeVisible();
      await expect(
        page.locator(".swal2-title"),
      ).toContainText("RENAME");
    });
  });

  test.describe("Account Delete Confirmation", () => {
    test.beforeEach(async ({ page, context }) => {
      await loginWithMSW(context);
      await page.goto("/account-admin");
      await expect(page.getByText("Test Admin").first()).toBeVisible();
    });

    test("delete confirmation Yes button triggers delete API", async ({
      page,
    }) => {
      await page.locator(".delete-account").first().click();
      await expect(page.locator("#modal_container")).toBeVisible();
      await page.locator("#sure_to_delete_acct_btn").click();
      // Modal should close after deletion
      await expect(
        page.locator("#modal_container"),
      ).not.toBeVisible();
    });
  });
});
