import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { SessionActivityPanel } from "../SessionActivityPanel";
import type { NowPlayingSession } from "../../types";

function session(
	overrides: Partial<NowPlayingSession> = {},
): NowPlayingSession {
	return {
		user: "alice",
		title: "Movie",
		type: "Movie",
		state: "playing",
		transcoding: "no",
		transcoding_type: "",
		device: "Web",
		session_id: "s1",
		...overrides,
	};
}

describe("SessionActivityPanel", () => {
	it("maps a playing (healthy) session to the success Badge variant", () => {
		render(<SessionActivityPanel sessions={[session({ state: "playing" })]} />);
		const badge = screen.getByText("Playing");
		expect(badge.getAttribute("data-variant")).toBe("success");
	});

	it("maps paused → warning and idle → secondary", () => {
		const { rerender } = render(
			<SessionActivityPanel sessions={[session({ state: "paused" })]} />,
		);
		expect(screen.getByText("Paused").getAttribute("data-variant")).toBe(
			"warning",
		);
		rerender(<SessionActivityPanel sessions={[session({ state: "idle" })]} />);
		expect(screen.getByText("Idle").getAttribute("data-variant")).toBe(
			"secondary",
		);
	});

	it("renders the empty-state message when there are no sessions", () => {
		render(
			<SessionActivityPanel sessions={[]} emptyMessage="Nothing playing." />,
		);
		expect(screen.getByText("Nothing playing.")).toBeInTheDocument();
	});

	it("calls onSelectSession on row click and on the action button", async () => {
		const onSelectSession = vi.fn();
		render(
			<SessionActivityPanel
				sessions={[session({ state: "playing" })]}
				onSelectSession={onSelectSession}
			/>,
		);
		await userEvent.click(screen.getByText("alice"));
		expect(onSelectSession).toHaveBeenCalledTimes(1);
		await userEvent.click(
			screen.getByRole("button", { name: "Open in Users" }),
		);
		expect(onSelectSession).toHaveBeenCalledTimes(2);
	});
});
