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

vi.mock("../api/adminApi", () => ({
  getAdminSummary: vi.fn(async () => ({
    users_count: 10,
    registrations_day: 3,
    admins_count: 2,
    superusers_count: 1
  }))
}));

describe("AdminStats", () => {
  it("renders dashboard metrics", async () => {
    const queryClient = new QueryClient();
    render(
      <QueryClientProvider client={queryClient}>
        <AdminStats />
      </QueryClientProvider>
    );
    expect(await screen.findByText("CPU")).toBeInTheDocument();
    expect(await screen.findByText("WESP")).toBeInTheDocument();
    expect(await screen.findByText("nx throughput (instant)")).toBeInTheDocument();
    expect(await screen.findByText("registrations today")).toBeInTheDocument();
    expect((await screen.findAllByText("users")).length).toBeGreaterThanOrEqual(1);

    const statCards = document.querySelectorAll(".wesp-admin-stat-card");
    expect(statCards).toHaveLength(4);
    expect(within(statCards[0] as HTMLElement).getByText("10")).toBeInTheDocument();
    expect(within(statCards[1] as HTMLElement).getByText("3")).toBeInTheDocument();
    expect(within(statCards[2] as HTMLElement).getByText("2")).toBeInTheDocument();
    expect(within(statCards[3] as HTMLElement).getByText("1")).toBeInTheDocument();
  });
});
