// 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("Discover Tab Navigation", () => {
  test.beforeEach(async ({ context }) => {
    await loginWithMSW(context);
  });

  test.describe("navigating from Map page", () => {
    test.beforeEach(async ({ page }) => {
      await page.goto("/discover/log/297310264/map");
      await expect(page.locator("#cy")).toBeVisible();
    });

    test("shows DISCOVER heading and MAP/CONFORMANCE/PERFORMANCE tabs", async ({
      page,
    }) => {
      await expect(
        page.locator("#nav_bar", { hasText: "DISCOVER" }),
      ).toBeVisible();
      const navItems = page.locator(".nav-item");
      await expect(navItems).toHaveCount(3);
      await expect(navItems.nth(0)).toContainText("MAP");
      await expect(navItems.nth(1)).toContainText("CONFORMANCE");
      await expect(navItems.nth(2)).toContainText("PERFORMANCE");
    });

    test("clicking PERFORMANCE tab navigates to performance page", async ({
      page,
    }) => {
      await page.locator(".nav-item", { hasText: "PERFORMANCE" }).click();
      await expect(page).toHaveURL(/\/performance/);
      await expect(
        page.locator(".z-\\[9999\\]"),
      ).not.toBeVisible({ timeout: 10000 });
      await expect(page.getByText("Time Usage").first()).toBeVisible();
    });

    test("clicking CONFORMANCE tab navigates to conformance page", async ({
      page,
    }) => {
      await page.locator(".nav-item", { hasText: "CONFORMANCE" }).click();
      await expect(page).toHaveURL(/\/conformance/);
      await expect(
        page.locator(".z-\\[9999\\]"),
      ).not.toBeVisible({ timeout: 10000 });
      await expect(page.getByText("Rule Settings")).toBeVisible();
    });

    test("shows back arrow to return to Files", async ({ page }) => {
      await expect(page.locator("#backPage")).toBeVisible();
      await expect(
        page.locator("#backPage"),
      ).toHaveAttribute("href", "/files");
    });
  });

  test.describe("navigating from Performance page", () => {
    test.beforeEach(async ({ page }) => {
      await page.goto("/discover/log/297310264/performance");
      await expect(
        page.locator(".chart-container, canvas").first(),
      ).toBeVisible();
      await expect(
        page.locator(".z-\\[9999\\]"),
      ).not.toBeVisible({ timeout: 10000 });
    });

    test("clicking MAP tab navigates to map page", async ({ page }) => {
      await page.locator(".nav-item", { hasText: "MAP" }).click();
      await expect(page).toHaveURL(/\/map/);
      await expect(page.locator("#cy")).toBeVisible();
    });

    test("clicking CONFORMANCE tab navigates to conformance page", async ({
      page,
    }) => {
      await page.locator(".nav-item", { hasText: "CONFORMANCE" }).click();
      await expect(page).toHaveURL(/\/conformance/);
      await expect(
        page.locator(".z-\\[9999\\]"),
      ).not.toBeVisible({ timeout: 10000 });
      await expect(page.getByText("Rule Settings")).toBeVisible();
    });
  });

  test.describe("navigating from Conformance page", () => {
    test.beforeEach(async ({ page }) => {
      await page.goto("/discover/log/297310264/conformance");
      await expect(
        page.locator(".p-radiobutton, [class*=conformance]").first(),
      ).toBeVisible();
      await expect(
        page.locator(".z-\\[9999\\]"),
      ).not.toBeVisible({ timeout: 10000 });
    });

    test("clicking MAP tab navigates to map page", async ({ page }) => {
      await page.locator(".nav-item", { hasText: "MAP" }).click();
      await expect(page).toHaveURL(/\/map/);
      await expect(page.locator("#cy")).toBeVisible();
    });

    test("clicking PERFORMANCE tab navigates to performance page", async ({
      page,
    }) => {
      await page.locator(".nav-item", { hasText: "PERFORMANCE" }).click();
      await expect(page).toHaveURL(/\/performance/);
      await expect(
        page.locator(".z-\\[9999\\]"),
      ).not.toBeVisible({ timeout: 10000 });
      await expect(page.getByText("Time Usage").first()).toBeVisible();
    });
  });
});
