import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { MessagingTab } from "../MessagingTab";
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,
				},
			],
			total: 1,
			page: 1,
			page_size: 100,
		},
	})),
	useSendAuthentikMessage: vi.fn(() => ({
		mutate: vi.fn(),
		isPending: false,
		data: undefined,
	})),
}));

describe("MessagingTab", () => {
	it("renders the compose form (subject, body, send)", () => {
		render(<MessagingTab instance={instance} />);
		expect(screen.getByLabelText("Subject")).toBeInTheDocument();
		expect(screen.getByLabelText("Message (HTML)")).toBeInTheDocument();
		expect(
			screen.getByRole("button", { name: "Send message" }),
		).toBeInTheDocument();
	});

	it("renders recipient toggle buttons from the directory", () => {
		render(<MessagingTab instance={instance} />);
		expect(screen.getByText("Alice")).toBeInTheDocument();
	});
});
