// -----------------------------------------------------------------------------
// Immo-Ka — Agrégateur de propriétés à vendre (province de Québec)
// Auteur : Simon-Pierre Boucher — contact@spboucher.ai
// components/ListingCard.tsx : carte de propriété (grille de résultats)
// -----------------------------------------------------------------------------
import { Link } from "react-router-dom";
import { Listing, fmtArea, fmtPrice, sourceName } from "../api";

export default function ListingCard({ l }: { l: Listing }) {
  const img = l.images && l.images.length > 0 ? l.images[0] : null;
  const meta: string[] = [];
  if (l.bedrooms != null) meta.push(`${l.bedrooms} ch.`);
  if (l.bathrooms != null) meta.push(`${l.bathrooms} sdb`);
  const area = fmtArea(l.area_sqft);
  if (area) meta.push(area);

  return (
    <Link to={`/propriete/${encodeURIComponent(l.uid)}`} className="card">
      <div className="card-img">
        {img ? (
          <img src={img} alt={l.title || l.address} loading="lazy" />
        ) : (
          <div className="noimg">🏠</div>
        )}
        {l.property_type && <span className="badge type">{l.property_type}</span>}
        {l.images.length > 1 && (
          <span className="badge right">📷 {l.images.length}</span>
        )}
      </div>
      <div className="card-body">
        <div className="card-price">{fmtPrice(l.price, l.price_label)}</div>
        <div className="card-title">{l.address || l.title}</div>
        <div className="card-meta">
          {l.sector && <span>{l.sector}</span>}
          {l.sector && l.city && <span className="sep" />}
          {l.city && <span>{l.city}</span>}
        </div>
        {meta.length > 0 && (
          <div className="card-specs">
            {meta.map((m) => (
              <span key={m}>{m}</span>
            ))}
          </div>
        )}
        <div className="card-foot">
          <span className="source-tag">{sourceName(l.source)}</span>
          {l.mls && <span className="avail">MLS {l.mls}</span>}
        </div>
      </div>
    </Link>
  );
}
