// 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("Edge Cases", () => {
  test.describe("Empty states", () => {
    test("files page handles empty file list", async ({ page, context }) => {
      await loginWithMSW(context);
      await page.goto("/files");
      // Verify page loaded with data first
      await expect(page.getByText("sample-process.xes").first()).toBeVisible();
      // Now override via MSW and use client-side navigation
      await page.evaluate(() => {
        const { http, HttpResponse } = (window as any).__msw__;
        (window as any).__mswWorker__.use(
          http.get("/api/files", () => HttpResponse.json([])),
        );
      });
      // Trigger re-fetch by navigating via the app's router
      await page.evaluate(() => {
        (window as any).__vue_app__?.config?.globalProperties?.$router?.push("/files");
      });
      // Wait for re-render
      await page.waitForTimeout(1000);
      // Use a more resilient check: the table exists but no file names
      await expect(page.locator("table")).toBeVisible();
    });

    test("account admin handles empty user list", async ({
      page,
      context,
    }) => {
      await loginWithMSW(context);
      await page.goto("/account-admin");
      await expect(page.getByText("Test Admin").first()).toBeVisible();
      // Override users endpoint
      await page.evaluate(() => {
        const { http, HttpResponse } = (window as any).__msw__;
        (window as any).__mswWorker__.use(
          http.get("/api/users", () => HttpResponse.json([])),
        );
      });
      // Navigate away and back via app router to trigger re-fetch
      await page.evaluate(() => {
        (window as any).__vue_app__?.config?.globalProperties?.$router?.push("/files");
      });
      await page.waitForTimeout(500);
      await page.evaluate(() => {
        (window as any).__vue_app__?.config?.globalProperties?.$router?.push("/account-admin");
      });
      await page.waitForTimeout(1000);
      await expect(page.locator("#create_new_acct_btn")).toBeVisible();
    });

    test("unauthenticated user is redirected to login", async ({
      page,
      context,
    }) => {
      await loginWithMSW(context);
      await page.goto("/files");
      await expect(page.getByText("sample-process.xes").first()).toBeVisible();
      // Override my-account to return 401
      await page.evaluate(() => {
        const { http, HttpResponse } = window.__msw__;
        window.__mswWorker__.use(
          http.get(
            "/api/my-account",
            () => new HttpResponse(null, { status: 401 }),
          ),
        );
      });
      // Clear cookies to simulate logged out
      await context.clearCookies();
      await page.goto("/files");
      await expect(page).toHaveURL(/\/login/);
    });

    test("unauthenticated user cannot access account-admin", async ({
      page,
    }) => {
      await page.goto("/account-admin");
      await expect(page).toHaveURL(/\/login/);
    });

    test("unauthenticated user cannot access my-account", async ({
      page,
    }) => {
      await page.goto("/my-account");
      await expect(page).toHaveURL(/\/login/);
    });
  });

  test.describe("Login validation", () => {
    test("shows error on failed login", async ({ page, context }) => {
      // Visit login page first to load the app
      await page.goto("/login");
      await expect(page.locator("#login_btn_main_btn")).toBeVisible();
      // Override token endpoint to return 401
      await page.evaluate(() => {
        const { http, HttpResponse } = window.__msw__;
        window.__mswWorker__.use(
          http.post("/api/oauth/token", () =>
            HttpResponse.json(
              { detail: "Invalid credentials" },
              { status: 401 },
            ),
          ),
        );
      });
      await page.locator("#account").fill("wronguser");
      await page.locator("#password").fill("wrongpass");
      await page.locator("#login_btn_main_btn").click();
      await expect(page).toHaveURL(/\/login/);
    });

    test("confirm stays disabled with only account field filled", async ({
      page,
      context,
    }) => {
      await loginWithMSW(context);
      await page.goto("/account-admin");
      await expect(page.getByText("Test Admin").first()).toBeVisible();
      await page.getByRole("button", { name: "Create New" }).click();
      await page.locator("#input_account_field").fill("onlyaccount");
      await expect(
        page.getByRole("button", { name: "Confirm" }),
      ).toBeDisabled();
    });

    test("confirm stays disabled with only name field filled", async ({
      page,
      context,
    }) => {
      await loginWithMSW(context);
      await page.goto("/account-admin");
      await expect(page.getByText("Test Admin").first()).toBeVisible();
      await page.getByRole("button", { name: "Create New" }).click();
      await page.locator("#input_name_field").fill("onlyname");
      await expect(
        page.getByRole("button", { name: "Confirm" }),
      ).toBeDisabled();
    });

    test("confirm stays disabled with only password field filled", async ({
      page,
      context,
    }) => {
      await loginWithMSW(context);
      await page.goto("/account-admin");
      await expect(page.getByText("Test Admin").first()).toBeVisible();
      await page.getByRole("button", { name: "Create New" }).click();
      await page.locator("#input_first_pwd").fill("onlypassword");
      await expect(
        page.getByRole("button", { name: "Confirm" }),
      ).toBeDisabled();
    });
  });
});
