import { ArrowRight, Heart, MapPin, Moon, Star, Sun } from "lucide-react";
import { AiImage } from "@/components/AiImage";
import { cruiseImage, type Cruise } from "@/data/home";
import { getMediaUrl } from "@/lib/admin-api";
import type { CatalogTour } from "@/lib/tours-api";

type CruiseCardItem = Cruise & {
  slug?: string;
  cover_image?: string | null;
  duration?: string;
  rating?: number;
};

type Props = {
  cruise: CruiseCardItem | CatalogTour;
  featured?: boolean;
};

function coverSrc(cruise: CruiseCardItem, featured: boolean) {
  if (cruise.cover_image) return getMediaUrl(cruise.cover_image);
  return cruiseImage(cruise, featured ? "wide" : "card");
}

function cruiseHref(cruise: CruiseCardItem) {
  if (cruise.slug) return `/cruise-tours/${cruise.slug}`;
  return `/cruise-tours/${encodeURIComponent(String(cruise.id))}`;
}

export function CruiseCard({ cruise, featured = false }: Props) {
  const href = cruiseHref(cruise);
  const isDay = cruise.duration === "Day tour";
  const rating = Math.max(1, Math.min(5, Math.round(cruise.rating || 5)));

  return (
    <a
      href={href}
      className={`group flex h-full flex-col overflow-hidden rounded-xl border border-[var(--line)] bg-white text-brand transition duration-300 hover:-translate-y-0.5 hover:shadow-[0_14px_36px_rgba(13,58,77,0.12)] ${
        featured ? "md:flex-row" : ""
      }`}
    >
      <div
        className={`relative overflow-hidden ${
          featured ? "aspect-[16/11] md:aspect-auto md:h-full md:w-[55%]" : "aspect-[4/3]"
        }`}
      >
        <AiImage
          src={coverSrc(cruise, featured)}
          alt={cruise.name}
          className="h-full w-full transition duration-700 group-hover:scale-105"
        />
        <span className="absolute top-3 left-3 inline-flex items-center gap-1.5 rounded-full bg-black/55 px-3 py-1 text-[11px] font-semibold text-white backdrop-blur">
          {isDay ? (
            <Sun className="h-3.5 w-3.5" fill="currentColor" strokeWidth={0} />
          ) : (
            <Moon className="h-3.5 w-3.5" fill="currentColor" strokeWidth={0} />
          )}
          {isDay ? "Day tour" : "Overnight Tour"}
        </span>
        <span
          className="absolute top-3 right-3 grid h-8 w-8 place-items-center rounded-full bg-white/90 text-brand"
          aria-hidden
        >
          <Heart className="h-4 w-4" strokeWidth={2} />
        </span>
        <div className="absolute bottom-3 left-1/2 flex -translate-x-1/2 gap-1.5">
          <span className="h-1.5 w-1.5 rounded-full bg-white" />
          <span className="h-1.5 w-1.5 rounded-full bg-white/45" />
          <span className="h-1.5 w-1.5 rounded-full bg-white/45" />
        </div>
      </div>

      <div
        className={`flex flex-1 flex-col justify-between space-y-2.5 p-3.5 sm:p-4 ${
          featured ? "md:w-[45%]" : ""
        }`}
      >
        <div className="space-y-2">
          <div className="flex items-start justify-between gap-2">
            <h3 className="line-clamp-1 text-base leading-snug font-semibold capitalize">
              {cruise.name}
            </h3>
            <div
              className="flex shrink-0 text-amber-400"
              aria-label={`${rating} star rating`}
            >
              {Array.from({ length: 5 }).map((_, i) => (
                <Star
                  key={i}
                  className={`h-3.5 w-3.5 ${i < rating ? "" : "opacity-25"}`}
                  fill="currentColor"
                  strokeWidth={0}
                />
              ))}
            </div>
          </div>
          <p className="flex items-center gap-1.5 text-sm text-brand/65">
            <MapPin className="h-4 w-4" fill="currentColor" strokeWidth={0} /> {cruise.bay}
          </p>
          {cruise.pickup ? (
            <p className="inline-flex rounded-full bg-accent/10 px-2.5 py-1 text-[11px] font-semibold text-brand">
              Pick up: {cruise.pickup}
            </p>
          ) : null}
        </div>

        <div className="flex items-end justify-between gap-3 pt-1">
          <p className="text-sm">
            {cruise.price == null ? (
              <span className="font-bold text-accent">Price updating!</span>
            ) : (
              <>
                <span className="text-brand/70">From </span>
                <span className="font-bold text-accent">${cruise.price}</span>{" "}
                <span className="text-xs font-medium text-brand/50">USD per person</span>
              </>
            )}
          </p>
          <span
            className="grid h-10 w-10 place-items-center rounded-lg bg-accent text-white transition group-hover:bg-accent-hover"
            aria-label={`View ${cruise.name}`}
          >
            <ArrowRight className="h-4 w-4" strokeWidth={2.4} />
          </span>
        </div>
      </div>
    </a>
  );
}
