import React from "react";
import { Check } from "lucide-react";
import { Badge } from "./badge";
import { Button } from "./button";
import { cn } from "../../lib/utils";

export interface IntegrationCardProps {
  name: string;
  description: string;
  /** Image src for the integration logo (e.g. aggregator logo) */
  logoSrc?: string;
  /** Fallback icon node shown when no logoSrc is provided but a logo area is desired */
  logoFallback?: React.ReactNode;
  connected?: boolean;
  onConnect?: () => void;
  onDisconnect?: () => void;
  /** Extra content rendered between description and action button (e.g. inbox assignment Select) */
  children?: React.ReactNode;
  className?: string;
}

export function IntegrationCard({
  name,
  description,
  logoSrc,
  logoFallback,
  connected = false,
  onConnect,
  onDisconnect,
  children,
  className,
}: IntegrationCardProps) {
  const hasLogo = Boolean(logoSrc || logoFallback);

  return (
    <div
      className={cn(
        "flex flex-col gap-3 border border-border p-4",
        connected && "bg-muted/20",
        className,
      )}
    >
      <div className="flex items-center gap-2">
        {hasLogo && (
          <div className="flex shrink-0 items-center">
            {logoSrc ? (
              <img
                src={logoSrc}
                alt={`${name} logo`}
                className="h-8 w-16 object-contain object-left"
              />
            ) : (
              <div className="flex h-9 w-9 items-center justify-center">
                {logoFallback}
              </div>
            )}
          </div>
        )}
        <span className="text-label-medium">{name}</span>
        {connected && (
          <Badge variant="success" className="gap-1">
            <Check className="h-3 w-3" />
            Connected
          </Badge>
        )}
      </div>

      <p className="text-body-small text-muted-foreground">{description}</p>

      {children}

      {!connected && onConnect && (
        <div className="flex justify-end">
          <Button variant="outline" size="sm" onClick={onConnect}>
            Connect
          </Button>
        </div>
      )}
      {connected && onDisconnect && (
        <div className="flex justify-end">
          <Button variant="outline" size="sm" onClick={onDisconnect}>
            Disconnect
          </Button>
        </div>
      )}
    </div>
  );
}
