// 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("Files to Discover Entry Flow", () => {
  test.beforeEach(async ({ page, context }) => {
    await loginWithMSW(context);
    await page.goto("/files");
    await expect(page.getByText("sample-process.xes").first()).toBeVisible();
  });

  test.describe("double-click table row to enter Discover", () => {
    test("double-click log file navigates to Map page", async ({ page }) => {
      // Target the Name column (has class .fileName) to avoid matching Dependency column
      await page
        .locator("td.fileName", { hasText: "sample-process.xes" })
        .locator("..")
        .dblclick();
      await expect(page).toHaveURL(/\/discover\/log\/1\/map/);
      await expect(page.locator("#cy")).toBeVisible();
    });

    test("double-click filter file navigates to Map page", async ({
      page,
    }) => {
      await page
        .locator("td.fileName", { hasText: "filtered-sample" })
        .locator("..")
        .dblclick();
      await expect(page).toHaveURL(/\/discover\/filter\/10\/map/);
      await expect(page.locator("#cy")).toBeVisible();
    });
  });

  test.describe("double-click to enter Discover from file list", () => {
    test("double-click log file navigates to Map page", async ({
      page,
    }) => {
      // Find the row with "production-log.csv" (a pure log, no parent ambiguity)
      await page
        .locator("table tbody tr", { hasText: "production-log.csv" })
        .first()
        .dblclick();
      await expect(page).toHaveURL(/\/discover\/log\/.*\/map/);
      await expect(page.locator("#cy")).toBeVisible();
    });
  });

  test.describe("DISCOVER tab filters files", () => {
    test("clicking DISCOVER tab shows only Log, Filter, and Rule files", async ({
      page,
    }) => {
      await page.locator(".nav-item", { hasText: "DISCOVER" }).click();
      await expect(
        page.locator("td.fileName", { hasText: "sample-process.xes" }),
      ).toBeVisible();
      await expect(
        page.locator("td.fileName", { hasText: "filtered-sample" }),
      ).toBeVisible();
      await expect(
        page.locator("td.fileName", { hasText: "conformance-check-1" }),
      ).toBeVisible();
    });
  });

  test.describe("Navbar state after entering Discover", () => {
    test("shows DISCOVER heading and tabs after entering from Files", async ({
      page,
    }) => {
      await page
        .locator("td.fileName", { hasText: "sample-process.xes" })
        .locator("..")
        .dblclick();
      await expect(page).toHaveURL(/\/discover\//);
      await expect(
        page.locator("#nav_bar", { hasText: "DISCOVER" }),
      ).toBeVisible();
      await expect(
        page.locator(".nav-item", { hasText: "MAP" }),
      ).toBeVisible();
      await expect(
        page.locator(".nav-item", { hasText: "CONFORMANCE" }),
      ).toBeVisible();
      await expect(
        page.locator(".nav-item", { hasText: "PERFORMANCE" }),
      ).toBeVisible();
    });

    test("shows back arrow pointing to /files", async ({ page }) => {
      await page
        .locator("td.fileName", { hasText: "sample-process.xes" })
        .locator("..")
        .dblclick();
      await expect(page).toHaveURL(/\/discover\//);
      await expect(
        page.locator("#backPage"),
      ).toHaveAttribute("href", "/files");
    });
  });
});
