import type { RatingSummary as RatingSummaryType, ProductIdentity } from "@/types/site-data";

interface RatingSummaryProps {
  rating: RatingSummaryType;
  breakdown: Array<{ label: string; percent: number }>;
  /** v25.94 — used to derive a per-product visual count fallback when
   *  source extraction didn't surface a verified review count. Schema
   *  AggregateRating still gates on rating.verified, so this number
   *  never reaches JSON-LD. */
  product?: ProductIdentity;
}

/**
 * v25.94 — same visual-count fallback used in Hero.tsx. Kept inline
 * here (rather than a shared util) to stay self-contained per the
 * "templates/nextjs-premium is the deployable" pattern.
 */
function visualReviewCount(seed: string): string {
  if (!seed) return "5,200";
  let h = 0;
  for (let i = 0; i < seed.length; i++) {
    h = ((h << 5) - h) + seed.charCodeAt(i);
    h |= 0;
  }
  const n = 4800 + (Math.abs(h) % 8400);
  return n.toLocaleString();
}

/**
 * RatingSummary — Big rating number + star distribution bars.
 * Used on the Reviews subpage above the extended review grid.
 */
export default function RatingSummary({ rating, breakdown, product }: RatingSummaryProps) {
  const hasVerifiedAggregate = rating?.verified === true && rating.count > 0 && Boolean(rating.average || rating.score);
  const displayCount = hasVerifiedAggregate ? rating.count.toLocaleString() : "Not published";
  const displayAverage = hasVerifiedAggregate ? (rating.average || rating.score) : "N/A";
  return (
    <section id="rating-summary" className="rating-summary">
      <div className="rating-big">
        <div className="rating-big-number">{displayAverage}</div>
        {hasVerifiedAggregate ? <div className="rating-big-stars">★★★★★</div> : null}
        <div className="rating-big-label">{hasVerifiedAggregate ? "Average Customer Rating" : "Editorial Review Status"}</div>
        <div className="rating-big-count">
          {hasVerifiedAggregate ? `Based on ${displayCount} verified reviews` : "No verified aggregate rating was available at build time. This page does not fabricate review counts."}
        </div>
      </div>
      {hasVerifiedAggregate ? (
        <div className="rating-breakdown">
          {breakdown.map((row, i) => (
            <div key={i} className="rating-row reveal">
              <span className="label">{row.label}</span>
              <div className="bar">
                <div
                  className="bar-fill"
                  style={{ ["--fill-percent" as any]: `${row.percent}%` }}
                />
              </div>
              <span className="percent">
                {row.percent < 1 ? "<1%" : `${Math.round(row.percent)}%`}
              </span>
            </div>
          ))}
        </div>
      ) : (
        <div className="rating-breakdown">
          <p className="rating-big-count">Use the official GLPro order page for final pricing and read this guide for offer, ingredient, and safety context.</p>
        </div>
      )}
    </section>
  );
}
