import Link from "next/link";
import { formatCount } from "@/lib/formatCount";
import VideoThumbnail from "@/components/VideoThumbnail";

const TIER_LABEL: Record<string, string> = {
  free: "Free",
  starter: "🎖️ Starter",
  plus: "🎖️ Plus",
  premium: "🎖️ Premium",
  ppv: "💰",
};

const TIER_CLASS: Record<string, string> = {
  free: "badge-free",
  starter: "badge-starter",
  plus: "badge-plus",
  premium: "badge-premium",
  ppv: "badge-ppv",
};

export function TierBadge({
  tier,
  price,
  inline = false,
}: {
  tier: string;
  price?: number | null;
  inline?: boolean; // true = render in normal document flow (not absolutely positioned over a thumbnail)
}) {
  const label = tier === "ppv" ? `💰 $${(price || 0).toFixed(2)}` : TIER_LABEL[tier] || tier;
  const position = inline ? "" : "absolute top-1.5 left-1.5";
  return (
    <span className={`${TIER_CLASS[tier] || "badge-free"} ${position} text-[9px] font-bold px-1.5 py-0.5 rounded-full whitespace-nowrap leading-tight`}>
      {label}
    </span>
  );
}

export default function VideoCard({ video, large = false }: { video: any; large?: boolean }) {
  return (
    <Link href={`/contents/${video.slug}`} className="group block card overflow-hidden hover:border-neutral-600 transition-colors h-full">
      <div className="relative aspect-video bg-neutral-900 overflow-hidden">
        <VideoThumbnail
          thumbnailUrl={video.thumbnailUrl}
          youtubeId={video.youtubeId}
          title={video.title}
        />
        <TierBadge tier={video.accessTier} price={video.price} />
      </div>
      <div className="p-3">
        <h3 className={`font-semibold line-clamp-2 group-hover:text-red-500 ${large ? "text-base" : "text-sm"}`}>
          {video.title}
        </h3>
        <p className="text-xs text-neutral-500 mt-1">{formatCount(video.views)} views</p>
      </div>
    </Link>
  );
}
