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

// Applications still embeds the MUI Media child (migrated in slice 7). Mock it
// so this slice-4 test stays focused on the migrated Applications shell and
// does not pull the still-MUI DataGrid into the jsdom render.
vi.mock("../Media", () => ({
	Media: () => <div data-testid="media-child">Media</div>,
}));

vi.mock("react-router-dom", () => ({
	useSearchParams: () => [new URLSearchParams(), vi.fn()],
}));

vi.mock("../../hooks/useSettings", () => ({
	useMonitoringSettings: () => ({
		data: [
			{
				id: "m1",
				name: "Main",
				enabled: true,
				services: ["jellyfin"],
			},
		],
	}),
}));

vi.mock("../../hooks/useServices", () => ({
	useServiceInstances: () => ({
		data: [{ id: "jfs1", service_type: "jellyfin", name: "Main", enabled: true }],
	}),
}));

vi.mock("../../hooks/useDashboard", () => ({
	useCounts: () => ({
		data: { movies: 10, series: 5, episodes: 100 },
	}),
	useLibraries: () => ({
		data: [
			{ library: "Movies", total: 10, movies: 10, series: 0 },
			{ library: "Shows", total: 5, movies: 0, series: 5 },
		],
	}),
}));

describe("Applications", () => {
	it("renders the Jellyfin library counts grid and tabs, and keeps the Media child", () => {
		render(<Applications />);

		// Library stats header.
		expect(screen.getByText("Library stats")).toBeInTheDocument();

		// Counts: Total = 10 + 5 + 100 = 115, plus the per-type counts.
		expect(screen.getByText("115")).toBeInTheDocument();
		expect(screen.getByText("Episodes")).toBeInTheDocument();

		// Library rows render their per-library totals (unique strings).
		expect(
			screen.getByText(/Total 10 · Movies 10 · Series 0/),
		).toBeInTheDocument();
		expect(
			screen.getByText(/Total 5 · Movies 0 · Series 5/),
		).toBeInTheDocument();

		// Tabs present.
		expect(screen.getByRole("tab", { name: "Jellyfin" })).toBeInTheDocument();
		expect(screen.getByRole("tab", { name: "Nextcloud" })).toBeInTheDocument();

		// The still-MUI Media child is rendered unchanged inside the Jellyfin tab.
		expect(screen.getByTestId("media-child")).toBeInTheDocument();
	});
});
