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

test.describe("Data integrity management", () => {
  test.beforeEach(async ({ page }) => {
    let orphanLoginIDDeleted = false;
    let integrityReportRequests = 0;

    await page.addInitScript(() => {
      window.localStorage.setItem("locale", "ko");
      window.localStorage.setItem("admin_session", "fake-token");
      window.localStorage.setItem("RoleSwitcher-Collapsed", "true");
      (
        window as Window & typeof globalThis & { _IS_TEST_MODE?: boolean }
      )._IS_TEST_MODE = true;

      const authority = "http://localhost:5000/oidc";
      const clientId = "adminfront";
      const key = `oidc.user:${authority}:${clientId}`;
      window.localStorage.setItem(
        key,
        JSON.stringify({
          access_token: "fake-token",
          token_type: "Bearer",
          profile: {
            sub: "admin-user",
            name: "Admin",
            email: "admin@test.com",
            role: "super_admin",
          },
          expires_at: Math.floor(Date.now() / 1000) + 36000,
        }),
      );
    });

    await page.route("**/api/v1/user/me", async (route) => {
      await route.fulfill({
        json: {
          id: "admin-user",
          name: "Admin",
          email: "admin@test.com",
          role: "super_admin",
          manageableTenants: [],
        },
      });
    });

    await page.route("**/api/v1/admin/integrity", async (route) => {
      await route.fulfill({
        json: {
          status: "fail",
          checkedAt: "2026-05-14T00:00:00Z",
          summary: {
            totalChecks: 2,
            passed: 1,
            warnings: 0,
            failures: 1,
          },
          sections: [
            {
              key: "tenant_integrity",
              label: "테넌트 정합성",
              status: "fail",
              checks: [
                {
                  key: "duplicate_tenant_slugs",
                  label: "중복 테넌트 slug",
                  description:
                    "삭제되지 않은 tenant의 slug를 대소문자 무시 기준으로 검사합니다.",
                  status: "fail",
                  severity: "error",
                  count: 1,
                },
              ],
            },
          ],
        },
      });
    });

    await page.route(/.*\/api\/v1\/.*/, async (route) => {
      const url = route.request().url();
      if (url.includes("/api/v1/user/me")) {
        await route.fulfill({
          json: {
            id: "admin-user",
            name: "Admin",
            email: "admin@test.com",
            role: "super_admin",
            manageableTenants: [],
          },
        });
        return;
      }
      if (url.includes("/api/v1/admin/integrity/orphan-user-login-ids")) {
        if (route.request().method() === "DELETE") {
          orphanLoginIDDeleted = true;
          await route.fulfill({
            json: {
              deletedCount: 1,
              deleted: [
                {
                  id: "login-id-1",
                  userId: "user-1",
                  tenantId: "tenant-1",
                  fieldKey: "emp_id",
                  loginId: "EMP001",
                  reasons: ["deleted_tenant"],
                },
              ],
              skippedIds: [],
            },
          });
          return;
        }
        await route.fulfill({
          json: orphanLoginIDDeleted
            ? { items: [], total: 0 }
            : {
                items: [
                  {
                    id: "login-id-1",
                    userId: "user-1",
                    userEmail: "missing@example.com",
                    tenantId: "tenant-1",
                    tenantSlug: "deleted-tenant",
                    fieldKey: "emp_id",
                    loginId: "EMP001",
                    reasons: ["deleted_tenant"],
                  },
                ],
                total: 1,
              },
        });
        return;
      }
      if (url.includes("/api/v1/admin/integrity")) {
        integrityReportRequests += 1;
        if (integrityReportRequests > 1) {
          await new Promise((resolve) => setTimeout(resolve, 150));
        }
        await route.fulfill({
          json: {
            status: "fail",
            checkedAt: "2026-05-14T00:00:00Z",
            summary: {
              totalChecks: 2,
              passed: 1,
              warnings: 0,
              failures: 1,
            },
            sections: [
              {
                key: "tenant_integrity",
                label: "테넌트 정합성",
                status: "fail",
                checks: [
                  {
                    key: "duplicate_tenant_slugs",
                    label: "중복 테넌트 slug",
                    description:
                      "삭제되지 않은 tenant의 slug를 대소문자 무시 기준으로 검사합니다.",
                    status: "fail",
                    severity: "error",
                    count: 1,
                  },
                ],
              },
            ],
          },
        });
        return;
      }
      if (route.request().method() === "GET") {
        await route.fulfill({ json: { items: [], total: 0 } });
      } else {
        await route.fulfill({ status: 200, json: {} });
      }
    });
  });

  test("shows the super-admin integrity report", async ({ page }) => {
    await page.goto("/system/data-integrity");

    await expect(
      page.getByRole("heading", { name: "데이터 정합성 검증" }),
    ).toBeVisible();
    await expect(page.getByText("테넌트 정합성")).toBeVisible();
    await expect(page.getByText("중복 테넌트 slug")).toBeVisible();
    await expect(page.getByRole("button", { name: "다시 검사" })).toBeVisible();
  });

  test("shows manual recheck progress and completion", async ({ page }) => {
    await page.goto("/system/data-integrity");

    await expect(page.getByText("중복 테넌트 slug")).toBeVisible();
    await page.getByRole("button", { name: "다시 검사" }).click();

    await expect(page.getByRole("button", { name: "검사 중" })).toBeDisabled();
    await expect(page.getByText("정합성 검사를 실행 중입니다.")).toBeVisible();
    await expect(page.getByText("검사가 완료되었습니다.")).toBeVisible();
    await expect(page.getByRole("button", { name: "다시 검사" })).toBeEnabled();
  });

  test("deletes selected orphan login ID targets after confirmation", async ({
    page,
  }) => {
    page.on("dialog", async (dialog) => {
      await dialog.accept();
    });

    await page.goto("/system/data-integrity");

    await expect(page.getByText("EMP001")).toBeVisible();
    await expect(page.getByText("삭제된 테넌트")).toBeVisible();
    await page.getByRole("checkbox", { name: "EMP001 선택" }).check();
    await page.getByRole("button", { name: "선택 삭제" }).click();

    await expect(
      page.getByText("1개의 유령 로그인 ID를 삭제했습니다."),
    ).toBeVisible();
    await expect(
      page.getByText("삭제할 유령 로그인 ID가 없습니다."),
    ).toBeVisible();
  });

  test("shows the latest integrity summary on the overview page", async ({
    page,
  }) => {
    await page.goto("/");

    await expect(page.getByText("정합성 최종 검증")).toBeVisible();
    await expect(page.getByText("실패 1건")).toBeVisible();
    await expect(page.getByText("테넌트 정합성")).toBeVisible();
  });
});
