'use client';

import { Star } from 'lucide-react';
import type { LayoverAirportSummary } from '@/services/flights/layoverAirport.client';
import { isStarFilled } from '@/services/flights/layoverDisplay';

/**
 * Rating summary.
 *
 * Deliberately not the MakeMyTrip layout the ticket illustrates. That card's
 * centrepiece is a five-row Excellent/Good/Average/Poor/Terrible breakdown, and
 * Google Places exposes no rating histogram at all — only the average and the
 * number of ratings. Inventing the distribution from an average would be
 * fabricating data about a real place, so the card is built around the two
 * figures we actually have and made to carry its weight with size instead.
 *
 * Category sub-scores (Transfer ease, Cleanliness, Lounges…) are absent for the
 * same reason.
 */

function StarRow({ value }: { value: number }) {
  return (
    <span className="layover-stars">
      {[1, 2, 3, 4, 5].map((star) => (
        <Star
          key={star}
          size={16}
          className={isStarFilled(value, star) ? 'is-on' : ''}
          fill={isStarFilled(value, star) ? 'currentColor' : 'none'}
          strokeWidth={1.5}
          aria-hidden="true"
        />
      ))}
    </span>
  );
}

export default function LayoverRatingCard({ summary }: { summary: LayoverAirportSummary }) {
  const { rating, attribution, airport } = summary;
  if (!rating) return null;

  return (
    <section className="layover-card" aria-labelledby="layover-rating-heading">
      <h4 className="layover-card__title" id="layover-rating-heading">
        Traveller rating
      </h4>

      <div className="layover-rating">
        <div className="layover-rating__score">{rating.average.toFixed(1)}</div>
        <div className="min-w-0">
          <div className="d-flex align-items-center flex-wrap gap-2">
            <StarRow value={rating.average} />
            <span className="fw-600">{rating.label}</span>
          </div>
          {/* The count is ratings, not readable reviews — Google returns tens of
              thousands of the former and at most five of the latter, and saying
              "411 reviews" above five of them invites the obvious question. */}
          <div className="text-sm color-text-muted">{rating.count.toLocaleString()} ratings</div>
        </div>
      </div>

      <p className="layover-attribution mb-0">
        {attribution.label}
        {airport.googleMapsUri && (
          <>
            {' · '}
            <a href={airport.googleMapsUri} target="_blank" rel="noreferrer noopener">
              View on Google Maps
            </a>
          </>
        )}
      </p>
    </section>
  );
}
