import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { render, screen } from "@testing-library/react";
import { MemoryRouter, Route, Routes } from "react-router-dom";
import { beforeEach, describe, expect, it, vi } from "vitest";
import TenantDetailPage from "./TenantDetailPage";

vi.mock("../../../lib/adminApi", () => ({
  fetchMe: vi.fn(async () => ({ role: "super_admin" })),
  fetchTenant: vi.fn(async () => ({
    id: "hanmac-family-id",
    name: "한맥 가족",
    slug: "hanmac-family",
    parentId: null,
  })),
}));

function renderTenantDetailPage() {
  const queryClient = new QueryClient({
    defaultOptions: {
      queries: {
        retry: false,
      },
    },
  });

  return render(
    <QueryClientProvider client={queryClient}>
      <MemoryRouter initialEntries={["/tenants/hanmac-family-id"]}>
        <Routes>
          <Route path="/tenants/:tenantId/*" element={<TenantDetailPage />}>
            <Route index element={<div>profile</div>} />
            <Route path="worksmobile" element={<div>worksmobile</div>} />
          </Route>
        </Routes>
      </MemoryRouter>
    </QueryClientProvider>,
  );
}

describe("TenantDetailPage Worksmobile navigation", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("opens Worksmobile management in the current admin route", async () => {
    renderTenantDetailPage();

    const link = await screen.findByRole("link", { name: /Worksmobile/i });

    expect(link).toHaveAttribute(
      "href",
      "/tenants/hanmac-family-id/worksmobile",
    );
    expect(link).not.toHaveAttribute("target");
    expect(link).not.toHaveAttribute("rel");
  });
});
