import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { render, screen } from "@testing-library/react";
import type React from "react";
import { MemoryRouter, Route, Routes } from "react-router-dom";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createI18nMock } from "../../test/i18nMock";
import TenantGroupsPage from "../tenants/routes/TenantGroupsPage";

const tenant = {
  id: "tenant-company",
  type: "COMPANY",
  name: "GPDTDC",
  slug: "gpdtdc",
  description: "",
  status: "active",
  memberCount: 2,
  createdAt: "2026-05-01T00:00:00Z",
  updatedAt: "2026-05-01T00:00:00Z",
};

const members = [
  {
    id: "user-1",
    name: "Member User",
    email: "member@example.com",
  },
];

vi.mock("../../lib/i18n", () => createI18nMock());

vi.mock("../../lib/adminApi", () => ({
  fetchTenant: vi.fn(async () => tenant),
  fetchUsers: vi.fn(async () => ({
    items: [
      {
        id: "user-1",
        name: "Member User",
        email: "member@example.com",
        role: "user",
        status: "active",
      },
      {
        id: "user-2",
        name: "Candidate User",
        email: "candidate@example.com",
        role: "user",
        status: "active",
      },
    ],
    total: 2,
  })),
  fetchGroups: vi.fn(async () => [
    {
      id: "group-root",
      tenantId: "tenant-company",
      name: "연구소",
      description: "root group",
      members,
      createdAt: "2026-05-01T00:00:00Z",
      updatedAt: "2026-05-01T00:00:00Z",
    },
    {
      id: "group-child",
      tenantId: "tenant-company",
      parentId: "group-root",
      name: "플랫폼팀",
      description: "child group",
      members: [],
      createdAt: "2026-05-01T00:00:00Z",
      updatedAt: "2026-05-01T00:00:00Z",
    },
  ]),
  createGroup: vi.fn(async () => undefined),
  deleteGroup: vi.fn(async () => undefined),
  addGroupMember: vi.fn(async () => undefined),
  removeGroupMember: vi.fn(async () => undefined),
}));

function renderWithProviders(ui: React.ReactElement, entry: string) {
  const queryClient = new QueryClient({
    defaultOptions: {
      queries: { retry: false },
      mutations: { retry: false },
    },
  });

  return render(
    <QueryClientProvider client={queryClient}>
      <MemoryRouter initialEntries={[entry]}>{ui}</MemoryRouter>
    </QueryClientProvider>,
  );
}

describe("TenantGroupsPage coverage smoke", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.spyOn(window, "confirm").mockReturnValue(true);
  });

  it("renders group hierarchy and selected group members", async () => {
    renderWithProviders(
      <Routes>
        <Route
          path="/tenants/:tenantId/groups"
          element={<TenantGroupsPage />}
        />
      </Routes>,
      "/tenants/tenant-company/groups",
    );

    expect((await screen.findAllByText("연구소")).length).toBeGreaterThan(0);
    expect(screen.getAllByText("플랫폼팀").length).toBeGreaterThan(0);
    expect(screen.getByText("새 그룹 생성")).toBeInTheDocument();
    expect(screen.getByText("조직 단위 레벨")).toBeInTheDocument();
  });
});
