'use client';

import Image from 'next/image';
import { GeoMarkerHotel, GeoHotelPricing } from '@/services/search/search.client';


function getRatingLabel(starRating: number): string {
  if (starRating >= 4.5) return 'Excellent';
  if (starRating >= 4) return 'Very Good';
  return 'Good';
}


function StarRating({ rating }: { rating: number }) {
  const filled = Math.round(rating);
  return (
    <div style={{ display: 'flex', gap: 2 }}>
      {Array.from({ length: 5 }).map((_, i) => (
        <svg
          key={i}
          width="12"
          height="12"
          viewBox="0 0 24 24"
          fill={i < filled ? '#f5a623' : '#ddd'}
          xmlns="http://www.w3.org/2000/svg"
        >
          <path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z" />
        </svg>
      ))}
    </div>
  );
}



function HotelHoverCard({ hotel, pricing, onClick }: { hotel: GeoMarkerHotel; pricing?: GeoHotelPricing; onClick: () => void }) {
  return (
    <div
      onClick={onClick}
      style={{
        background: '#fff',
        borderRadius: 10,
        boxShadow: '0 4px 24px rgba(0,0,0,0.18)',
        width: 240,
        overflow: 'hidden',
        border: '1px solid #e9ecef',
        cursor: 'pointer',
      }}
    >
      {hotel.primary_image_url ? (
        <div style={{ height: 130, position: 'relative', overflow: 'hidden' }}>
          <Image
            src={hotel.primary_image_url}
            alt={hotel.hotel_name}
            fill
            style={{ objectFit: 'cover' }}
            unoptimized
          />
        </div>
      ) : (
        <div
          style={{
            height: 130,
            background: 'linear-gradient(135deg, #003580 0%, #3b82f6 100%)',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
          }}
        >
          <svg width="32" height="32" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M3 9l9-7 9 7v11a2 2 0 01-2 2H5a2 2 0 01-2-2z" stroke="rgba(255,255,255,0.5)" strokeWidth="1.5" fill="none" />
            <path d="M9 22V12h6v10" stroke="rgba(255,255,255,0.5)" strokeWidth="1.5" />
          </svg>
        </div>
      )}
      <div style={{ padding: '10px 12px 12px' }}>
        <p
          style={{
            margin: 0,
            fontWeight: 700,
            fontSize: 13,
            color: '#1a1a2e',
            lineHeight: 1.4,
            marginBottom: 5,
            overflow: 'hidden',
            display: '-webkit-box',
            WebkitLineClamp: 2,
            WebkitBoxOrient: 'vertical' as const,
          }}
        >
          {hotel.hotel_name}
        </p>
        {hotel.avg_overall_rating > 0 && <div style={{ display: 'flex', alignItems: 'center', gap: 5 }}>
          <StarRating rating={hotel.avg_overall_rating} />
          <span
            style={{
              fontSize: 11,
              fontWeight: 600,
              color: '#003580',
              background: '#e8f0fe',
              borderRadius: 4,
              padding: '1px 5px',
            }}
          >
            {getRatingLabel(hotel.avg_overall_rating)}
          </span>
        </div>}
        {hotel.address.lines[0] && (
          <p style={{ margin: '6px 0 0', fontSize: 11, color: '#888', lineHeight: 1.4 }}>
            {hotel.address.lines[0]}
            {hotel.address.city_name ? `, ${hotel.address.city_name}` : ''}
          </p>
        )}

        {/* ── Pricing section ── */}
        {pricing?.prices_available && pricing?.offers?.[0] && (
          <div
            style={{
              marginTop: 8,
              paddingTop: 8,
              borderTop: '1px solid #f0f0f0',
              textAlign: 'right',
            }}
          >
            <div style={{ fontWeight: 700, fontSize: 22, color: '#1a1a2e', lineHeight: 1.1 }}>
              ${Math.round(pricing.offers[0].pricePerNight.basePricePerNight).toLocaleString()}
            </div>
            <div style={{ fontSize: 11, color: '#888', marginTop: 2 }}>
              +${Math.round(pricing.offers[0].pricePerNight.taxesAndFeesPerNight).toLocaleString()} taxes &amp; fees
            </div>
            <div style={{ fontSize: 11, color: '#888' }}>Per Night</div>
          </div>
        )}
      </div>
    </div>
  );
}

export default HotelHoverCard;