import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { AdminUsersTable } from "./AdminUsersTable";

vi.mock("@modules/auth/store/authStore", () => ({
  useAuthStore: (selector: (state: { user: { id: string } }) => unknown) =>
    selector({ user: { id: "admin-id" } })
}));

vi.mock("@modules/auth", () => ({
  useIsSuperuser: () => false
}));

vi.mock("../api/adminApi", () => ({
  getAdminUsers: vi.fn(async () => ({
    data: [{ id: "1", email: "user@example.com", role: "user", is_superuser: false, status: "active" }],
    meta: { total: 1, page: 1, limit: 20 }
  })),
  patchAdminUser: vi.fn()
}));

describe("AdminUsersTable", () => {
  it("renders users table", async () => {
    const queryClient = new QueryClient();
    render(
      <QueryClientProvider client={queryClient}>
        <AdminUsersTable />
      </QueryClientProvider>
    );
    expect(await screen.findByText("user@example.com")).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Edit" })).toBeInTheDocument();
  });
});
