import { Alert, AlertDescription } from "@/components/ui/alert";
import { Skeleton } from "@/components/ui/skeleton";
import { SectionCard } from "../components/SectionCard";
import { useWidgetData } from "../hooks/useWidgets";
import type { WidgetInstance } from "../types";

interface Props {
	widget: WidgetInstance;
	refreshIntervalMs: number;
	description?: string;
}

interface StatData {
	key?: string;
	label?: string;
	value?: number;
}

/** Single Jellyseerr request statistic (e.g. pending / total). */
export function RequestStatWidget({
	widget,
	refreshIntervalMs,
	description,
}: Props) {
	const { data, isLoading } = useWidgetData(widget.id, refreshIntervalMs);
	const stat = data?.data as StatData | undefined;

	return (
		<SectionCard title={widget.title} description={description}>
			{isLoading && !data ? (
				<Skeleton className="h-16 w-24" />
			) : data?.error ? (
				<Alert variant="destructive">
					<AlertDescription>{data.error}</AlertDescription>
				</Alert>
			) : stat ? (
				<div className="flex flex-col items-center justify-center py-4">
					<span className="text-3xl font-bold">{stat.value ?? "—"}</span>
					<span className="text-xs uppercase tracking-wide text-muted-foreground">
						{stat.label ?? "Requests"}
					</span>
				</div>
			) : (
				<Alert>
					<AlertDescription>No request data.</AlertDescription>
				</Alert>
			)}
		</SectionCard>
	);
}
