import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { UsersTab } from "../UsersTab";
import type { ServiceInstance } from "../../../types";

const instance: ServiceInstance = {
	id: "auth-1",
	service_type: "authentik",
	name: "Main Authentik",
	config: { base_url: "https://auth.example.com", timeout_seconds: 10 },
	secrets_set: { api_token: true },
	enabled: true,
	created_at: 1_700_000_000,
	updated_at: 1_700_000_000,
};

vi.mock("../../../hooks/useAuthentik", () => ({
	useAuthentikUsers: vi.fn(() => ({
		data: {
			items: [
				{
					pk: 1,
					username: "alice",
					name: "Alice",
					email: "alice@example.com",
					is_active: true,
				},
				{
					pk: 2,
					username: "bob",
					name: "Bob",
					email: "bob@example.com",
					is_active: false,
				},
			],
			total: 2,
			page: 1,
			page_size: 25,
		},
		isLoading: false,
	})),
}));

describe("UsersTab", () => {
	it("renders the directory table with users", () => {
		render(<UsersTab instance={instance} />);
		expect(screen.getByText("Alice")).toBeInTheDocument();
		expect(screen.getByText("bob")).toBeInTheDocument();
		expect(screen.getByText("alice@example.com")).toBeInTheDocument();
		expect(screen.getByText("Active")).toBeInTheDocument();
		expect(screen.getByText("Inactive")).toBeInTheDocument();
	});

	it("renders search input and pagination", () => {
		render(<UsersTab instance={instance} />);
		expect(screen.getByPlaceholderText("Search users…")).toBeInTheDocument();
		expect(screen.getByText(/2 users/)).toBeInTheDocument();
		expect(screen.getByText("Previous")).toBeInTheDocument();
		expect(screen.getByText("Next")).toBeInTheDocument();
	});
});
