'use client'
import { MessageCircle } from 'lucide-react'
import { cn } from '@/lib/utils'

interface WhatsAppLinkProps {
  phone?: string | null
  name?: string
  className?: string
  showIcon?: boolean
}

function cleanPhone(phone: string) {
  // Eliminar espacios, guiones, paréntesis y el 0 inicial de área argentina
  return phone.replace(/[\s\-\(\)]/g, '').replace(/^0/, '54').replace(/^(?!54)/, '54')
}

export function WhatsAppLink({ phone, name, className, showIcon = true }: WhatsAppLinkProps) {
  if (!phone) return <span className="text-muted-foreground">—</span>

  const cleaned = cleanPhone(phone)
  const message = name ? `Hola ${name}, te contacto desde Horacio E. Acosta Negocios Inmobiliarios.` : ''
  const url = `https://wa.me/${cleaned}${message ? `?text=${encodeURIComponent(message)}` : ''}`

  return (
    <a
      href={url}
      target="_blank"
      rel="noreferrer"
      className={cn(
        'inline-flex items-center gap-1.5 text-green-600 hover:text-green-700 dark:text-green-500 dark:hover:text-green-400 transition-colors font-medium',
        className
      )}
    >
      {showIcon && <MessageCircle size={13} className="flex-shrink-0" />}
      {phone}
    </a>
  )
}
