/* ============================================================
 * Hostelova - shared listing status and verified badges
 * Loaded after icons.jsx and reused by landing + booking pages.
 * ========================================================== */
window.HostelovaBadges = (function () {
  const CLAIM_STATUS_CONFIG = {
    unclaimed: {
      label: "Unclaimed",
      tone: "unclaimed",
      title: "Unclaimed listing",
      text: "This hostel has not yet been claimed by its owner. It is a neutral status. Are you the owner? Claim this listing.",
    },
    claimed: {
      label: "Claimed",
      tone: "claimed",
      title: "Claimed listing",
      text: "The owner account is linked and documents are under review by Hostelova.",
    },
    verified: {
      label: "Verified",
      tone: "verified",
      title: "Verified listing",
      text: "Owner documents and hostel details have been checked and approved by Hostelova.",
    },
    featured: {
      label: "Featured",
      tone: "featured",
      title: "Featured listing",
      text: "This listing is highlighted on Hostelova. Featured listings can also show verified owner and listing details after review.",
    },
  };

  const CLAIM_STATUS_ALIASES = {
    verification_pending: "claimed",
    pending_verification: "claimed",
    pending: "claimed",
    claimed: "claimed",
    owner_claimed: "claimed",
    owner_verified: "verified",
    verified_listing: "verified",
  };

  const truthy = (value) =>
    value === true ||
    value === 1 ||
    String(value).toLowerCase() === "true" ||
    String(value).toLowerCase() === "yes";

  const claimStatusConfig = (status) =>
    CLAIM_STATUS_CONFIG[status] || CLAIM_STATUS_CONFIG.unclaimed;

  const normalizeClaimStatus = (row = {}) => {
    if (truthy(row.featured)) return "featured";
    const raw = String(row.claim_status || row.claimStatus || "")
      .trim()
      .toLowerCase()
      .replace(/[\s-]+/g, "_");
    if (CLAIM_STATUS_CONFIG[raw]) return raw;
    if (CLAIM_STATUS_ALIASES[raw]) return CLAIM_STATUS_ALIASES[raw];
    if (truthy(row.is_verified) || truthy(row.verified)) return "verified";
    return "unclaimed";
  };

  const isTrustedClaimStatus = (status) =>
    status === "verified" || status === "featured";

  const isVerifiedListing = (hostel = {}) =>
    isTrustedClaimStatus(normalizeClaimStatus(hostel)) ||
    truthy(hostel.is_verified) ||
    truthy(hostel.verified);

  const VerifiedListingBadge = ({ hostel, className = "", label = "Verified" }) => {
    if (!isVerifiedListing(hostel)) return null;
    const cn = ["listing-trust-badge", "is-verified", className].filter(Boolean).join(" ");
    return (
      <span className={cn}>
        <Icon.Verified className="listing-trust-badge-icon" width="13" height="13" aria-hidden="true" />
        {label}
      </span>
    );
  };

  const ClaimStatusBadge = ({ hostel, onInfo, className = "" }) => {
    const status = normalizeClaimStatus(hostel);
    const config = claimStatusConfig(status);
    const trusted = isTrustedClaimStatus(status);
    const toneClass = trusted ? `status-${config.tone}` : config.tone;
    const cn = [
      "badge",
      "claim-badge",
      "listing-status-badge",
      toneClass,
      trusted ? "is-verified" : "",
      className,
    ].filter(Boolean).join(" ");

    return (
      <button
        type="button"
        className={cn}
        onClick={(e) => {
          e.stopPropagation();
          if (onInfo) onInfo(hostel);
        }}
        title={`${config.label} status`}
        aria-label={`${config.label} listing status`}
      >
        {trusted && (
          <Icon.Verified className="listing-trust-badge-icon" width="13" height="13" aria-hidden="true" />
        )}
        {config.label}
      </button>
    );
  };

  return {
    claimStatusConfig,
    normalizeClaimStatus,
    isTrustedClaimStatus,
    isVerifiedListing,
    truthy,
    VerifiedListingBadge,
    ClaimStatusBadge,
  };
})();
