/**
 * `PluginLayoutCellView` — renders a `customPluginLayout` top-level
 * {@link LayoutNode} (ticket #43, F4, carnet v2 §3): the plugin's registered
 * React component when its provider is loaded and declares the type,
 * otherwise {@link PluginLayoutFallback}.
 *
 * State is opaque to the domain (`unknown`) and round-trips through
 * `LayoutGateway` exactly like any other node field — `setState` here is the
 * only way a plugin component is meant to persist it (never direct gateway
 * calls from inside a plugin component).
 */

import type { CustomPluginLayoutCell, PluginAdmin } from "@/domain";
import { useGateways } from "@/app/di";
import { usePluginRuntime } from "./PluginRuntimeProvider";
import { PluginLayoutFallback } from "./PluginLayoutFallback";
import { resolvePluginLayoutAvailability } from "./layoutAvailability";

interface PluginLayoutCellViewProps {
  projectId: string;
  cell: CustomPluginLayoutCell;
  installedPlugins?: PluginAdmin[];
  onStateChange: (nextState: unknown) => void;
  onOpenPlugins: () => void;
  onChooseAnotherLayout: () => void;
}

export function PluginLayoutCellView({
  projectId,
  cell,
  installedPlugins,
  onStateChange,
  onOpenPlugins,
  onChooseAnotherLayout,
}: PluginLayoutCellViewProps) {
  const { registry } = usePluginRuntime();
  const gateways = useGateways();
  const availability = resolvePluginLayoutAvailability(registry, cell, installedPlugins);

  if (availability !== "available") {
    const providerDisplayName =
      registry.get(cell.pluginId)?.displayName ??
      installedPlugins?.find((p) => p.id === cell.pluginId)?.displayName;
    return (
      <PluginLayoutFallback
        cell={cell}
        availability={availability}
        providerDisplayName={providerDisplayName}
        onOpenPlugins={onOpenPlugins}
        onChooseAnotherLayout={onChooseAnotherLayout}
      />
    );
  }

  const Component = registry.layoutComponent(cell.pluginId, cell.layoutType)!;
  return (
    <Component
      projectId={projectId}
      nodeId={cell.id}
      layoutType={cell.layoutType}
      state={cell.state}
      setState={onStateChange}
      availability="available"
      gateways={{
        project: gateways.project,
        git: gateways.git,
        terminal: gateways.terminal,
        agents: gateways.agent,
        system: gateways.system,
      }}
    />
  );
}
