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

function makeInstance(config: Record<string, unknown>): ServiceInstance {
	return {
		id: "jellyfin-1",
		service_type: "jellyfin",
		name: "Main Jellyfin",
		config,
		secrets_set: {},
		enabled: true,
		created_at: 1_700_000_000,
		updated_at: 1_700_000_000,
	};
}

describe("RequestsTab", () => {
	it("shows empty-state CTA when Jellyseerr is not configured", () => {
		render(
			<RequestsTab
				instance={makeInstance({
					base_url: "https://jf.example.com",
					user_id: "u1",
				})}
			/>,
		);
		expect(screen.getByText(/not configured/i)).toBeInTheDocument();
		expect(screen.getByText(/jellyseerr_url/i)).toBeInTheDocument();
	});

	it("shows the configured Jellyseerr URL when both fields are set", () => {
		render(
			<RequestsTab
				instance={makeInstance({
					base_url: "https://jf.example.com",
					jellyseerr_url: "https://requests.example.com",
					jellyseerr_api_key: "secret-key",
				})}
			/>,
		);
		expect(
			screen.getByText("https://requests.example.com"),
		).toBeInTheDocument();
		expect(screen.queryByText(/not configured/i)).not.toBeInTheDocument();
	});

	it("shows empty-state when only URL is set (missing api_key)", () => {
		render(
			<RequestsTab
				instance={makeInstance({
					jellyseerr_url: "https://requests.example.com",
				})}
			/>,
		);
		expect(screen.getByText(/not configured/i)).toBeInTheDocument();
	});
});
