import type { ReactNode } from "react";
import type { Business } from "@/mocks/dshn";
import { cn } from "./ui";

function fmt(n: number) {
  return n.toLocaleString("en-US");
}

export function SeekingBadge() {
  return (
    <span className="inline-flex items-center gap-1 rounded-full bg-secondary-100 text-secondary-900 px-2 py-0.5 text-[10px] font-bold whitespace-nowrap">
      <i className="ri-seedling-line text-[11px] leading-none" />
      تبحث عن استثمار
    </span>
  );
}

export function VerifiedBadge() {
  return (
    <span className="inline-flex items-center gap-0.5 text-accent-700 text-[10.5px] font-semibold whitespace-nowrap">
      <i className="ri-verified-badge-fill text-[12px] leading-none" />
      موثّق
    </span>
  );
}

export function BusinessRow({
  business,
  onClick,
  action,
}: {
  business: Business;
  onClick?: () => void;
  action?: ReactNode;
}) {
  return (
    <div
      onClick={onClick}
      className="bg-background-100 border border-background-200 rounded-[20px] p-4 flex gap-3.5 cursor-pointer hover:border-background-300 transition-colors"
    >
      <div className="w-14 h-14 rounded-full overflow-hidden bg-background-200 shrink-0">
        <img src={business.logo} alt={business.name} className="w-full h-full object-cover object-top" />
      </div>
      <div className="flex-1 min-w-0">
        <div className="flex items-center gap-2 flex-wrap">
          <h3 className="text-[15px] font-semibold text-foreground-950 truncate">{business.name}</h3>
          {business.verified && <VerifiedBadge />}
        </div>
        <p className="text-[12px] text-foreground-600 mt-0.5">
          {business.activity} • {business.city}
        </p>
        <div className="flex items-center gap-3 mt-2 flex-wrap">
          <span className="text-[11.5px] text-foreground-600">
            التقييم السوقي <span className="text-foreground-500" title="تقييم تقديري لقيمة المنشأة يُستخدم من المستثمرين لتقييم الفرصة">(لأغراض الاستثمار)</span>: <strong className="text-foreground-900 font-semibold">{fmt(business.estimatedValue)} ر.س</strong>
          </span>
        </div>
        <div className="flex items-center justify-between gap-2 mt-2">
          <span className="text-[11.5px] text-foreground-600">
            حجم المبيعات: <strong className="text-foreground-900 font-semibold">{fmt(business.annualSales)} ر.س</strong>
          </span>
          {business.seekingInvest && <SeekingBadge />}
        </div>
      </div>
      {action}
    </div>
  );
}

export function BusinessMini({
  business,
  onClick,
  width = "w-[210px]",
}: {
  business: Business;
  onClick?: () => void;
  width?: string;
}) {
  return (
    <div
      onClick={onClick}
      className={cn(
        "shrink-0 bg-background-100 border border-background-200 rounded-[20px] p-3.5 cursor-pointer hover:border-background-300 transition-colors",
        width,
      )}
    >
      <div className="flex items-center gap-3">
        <div className="w-12 h-12 rounded-full overflow-hidden bg-background-200 shrink-0">
          <img src={business.logo} alt={business.name} className="w-full h-full object-cover object-top" />
        </div>
        <div className="min-w-0">
          <h3 className="text-[14px] font-semibold text-foreground-950 truncate">{business.name}</h3>
          <p className="text-[11px] text-foreground-600 truncate">
            {business.activity} • {business.city}
          </p>
        </div>
      </div>
      <div className="mt-3 space-y-1.5">
        <div className="flex items-center justify-between text-[11px]">
          <span
            className="text-foreground-600 flex items-center gap-1"
            title="تقييم تقديري لقيمة المنشأة يُستخدم من المستثمرين لتقييم الفرصة"
          >
            التقييم السوقي
            <i className="ri-information-line text-[11px] leading-none text-foreground-400" />
          </span>
          <span className="font-semibold text-foreground-900">{fmt(business.estimatedValue)} ر.س</span>
        </div>
        <div className="flex items-center justify-between text-[11px]">
          <span className="text-foreground-600">حجم المبيعات</span>
          <span className="font-semibold text-foreground-900">{fmt(business.annualSales)} ر.س</span>
        </div>
      </div>
      <div className="mt-3 flex items-center justify-between gap-1">
        <span className="flex items-center gap-1 text-[11px] font-semibold text-foreground-900">
          <i className="ri-star-fill text-secondary-500 text-[12px] leading-none" />
          {business.rating}
        </span>
        {business.seekingInvest && <SeekingBadge />}
      </div>
    </div>
  );
}