// -----------------------------------------------------------------------------
// Author: Simon-Pierre Boucher
// Contact: contact@spboucher.ai
// Project: Toit-Ka
// components/ListingCard.tsx : carte d'annonce (grille) — bi-univers.
//   Le badge d'univers (Louer lime / Acheter rouge) garde la grille lisible
//   quand les deux univers se croisent (recherche unifiée, favoris).
// -----------------------------------------------------------------------------
import { Link } from "react-router-dom";
import { Listing, fichePath, fmtArea, fmtPrice, sourceName } from "../api";
import { useAccount } from "../account";
import { Ico, IcoHeart } from "./Icons";

export default function ListingCard({ l, showTx = false }:
  { l: Listing; showTx?: boolean }) {
  const img = l.images && l.images.length > 0 ? l.images[0] : null;
  const { favs, toggleFav } = useAccount();
  const fav = favs.has(l.uid);
  const louer = l.transaction_type === "louer";

  const meta: { ico: string; txt: string }[] = [];
  if (l.bedrooms != null) meta.push({ ico: "bed", txt: `${l.bedrooms} ch.` });
  if (l.bathrooms != null) meta.push({ ico: "bath", txt: `${l.bathrooms} sdb` });
  const area = fmtArea(l.area_sqft);
  if (area) meta.push({ ico: "area", txt: area });
  if (louer && l.pets === "oui") meta.push({ ico: "paw", txt: "animaux ok" });
  if (louer && l.furnished === 1) meta.push({ ico: "sofa", txt: "meublé" });

  const dispo = louer && l.availability_date
    ? (l.availability_date === "now" ? "Libre maintenant" : `Libre ${l.availability_date}`)
    : l.mls ? `MLS ${l.mls}` : "";

  const price = l.price != null
    ? <>{l.price.toLocaleString("fr-CA", { maximumFractionDigits: 0 })} $
        {louer && <em> /mois</em>}</>
    : <>{l.price_label || "Prix sur demande"}</>;

  return (
    <Link to={fichePath(l)} className="card">
      <div className="card-img">
        {img ? (
          <img src={img} alt={l.title || l.address} loading="lazy" />
        ) : (
          <div className="noimg"><Ico name="home" size={34} /></div>
        )}
        {showTx
          ? <span className={`badge tx-${l.transaction_type}`}>{louer ? "À louer" : "À vendre"}</span>
          : l.type && <span className="badge type">{l.type}</span>}
        {l.images.length > 1 && (
          <span className="badge right"><Ico name="camera" size={12} /> {l.images.length}</span>
        )}
        <button
          className={`fav-btn ${fav ? "on" : ""}`}
          aria-label={fav ? "Retirer des favoris" : "Ajouter aux favoris"}
          aria-pressed={fav}
          onClick={(e) => { e.preventDefault(); e.stopPropagation(); toggleFav(l); }}
        >
          <IcoHeart size={16} filled={fav} />
        </button>
      </div>
      <div className="card-body">
        <div className="card-price">{price}</div>
        <div className="card-title">{l.address || l.title}</div>
        <div className="card-meta">
          {showTx && l.type && <span>{l.type}</span>}
          {showTx && l.type && (l.sector || l.city) && <span className="sep" />}
          {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.ico}><Ico name={m.ico} size={13} /> {m.txt}</span>
            ))}
          </div>
        )}
        <div className="card-foot">
          <span className="source-tag">{sourceName(l.source)}</span>
          {dispo && <span className="avail">{dispo}</span>}
        </div>
      </div>
    </Link>
  );
}
