'use client'

import * as React from 'react'
import { Loader2 } from 'lucide-react'
import { cn } from '../../lib/utils'

export interface ConnectionStatusProps {
  providerName: string
  providerIcon: React.ElementType
  connected: boolean
  accountLabel?: string
  onConnect?: () => void
  onDisconnect?: () => void
  isLoading?: boolean
  className?: string
}

export function ConnectionStatus({
  providerName,
  providerIcon: Icon,
  connected,
  accountLabel,
  onConnect,
  onDisconnect,
  isLoading = false,
  className,
}: ConnectionStatusProps) {
  return (
    <div
      className={cn(
        'flex items-center justify-between rounded-lg border p-4',
        className
      )}
    >
      <div className="flex items-center gap-3">
        <div className="flex-shrink-0 w-9 h-9 rounded-lg bg-gray-100 flex items-center justify-center">
          <Icon className="w-5 h-5 text-gray-600" />
        </div>
        <div>
          <p className="text-sm font-medium">{providerName}</p>
          {connected && accountLabel && (
            <p className="text-xs text-muted-foreground">{accountLabel}</p>
          )}
          {!connected && (
            <p className="text-xs text-muted-foreground">Not connected</p>
          )}
        </div>
      </div>

      <div>
        {isLoading ? (
          <button
            disabled
            className="inline-flex items-center gap-1.5 rounded-md px-3 py-1.5 text-xs font-medium bg-gray-100 text-gray-400 cursor-not-allowed"
          >
            <Loader2 className="w-3 h-3 animate-spin" />
            Loading...
          </button>
        ) : connected ? (
          <button
            onClick={onDisconnect}
            className="inline-flex items-center rounded-md px-3 py-1.5 text-xs font-medium border border-red-200 text-red-600 hover:bg-red-50 transition-colors"
          >
            Disconnect
          </button>
        ) : (
          <button
            onClick={onConnect}
            className="inline-flex items-center rounded-md px-3 py-1.5 text-xs font-medium bg-primary text-primary-foreground hover:bg-primary/90 transition-colors"
          >
            Connect
          </button>
        )}
      </div>
    </div>
  )
}
