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";
import { getWidgetDefinition } from "./registry";

interface Props {
	widget: WidgetInstance;
}

type SshTaskResult = {
	exit_status: number;
	stdout: string;
	stderr: string;
};

export function SshTaskWidget({ widget }: Props) {
	const def = getWidgetDefinition(widget.widget_type);
	const { data, isLoading } = useWidgetData(
		widget.id,
		def?.refreshInterval ?? 0,
	);
	const result = data?.data as SshTaskResult | undefined;

	return (
		<SectionCard title={widget.title} description={def?.description}>
			{isLoading && !data ? (
				<div className="flex flex-col gap-2">
					<Skeleton className="h-4 w-full" />
					<Skeleton className="h-4 w-3/4" />
				</div>
			) : data?.error ? (
				<Alert variant="destructive">
					<AlertDescription>{data.error}</AlertDescription>
				</Alert>
			) : result ? (
				<div className="flex flex-col gap-3">
					<div className="text-xs text-muted-foreground">
						Exit status:{" "}
						<span
							className={
								result.exit_status === 0 ? "text-green-600" : "text-destructive"
							}
						>
							{result.exit_status}
						</span>
					</div>
					{result.stdout ? (
						<pre className="max-h-64 overflow-auto rounded bg-muted p-2 text-xs">
							{result.stdout}
						</pre>
					) : null}
					{result.stderr ? (
						<pre className="max-h-64 overflow-auto rounded bg-destructive/10 p-2 text-xs text-destructive">
							{result.stderr}
						</pre>
					) : null}
				</div>
			) : null}
		</SectionCard>
	);
}
