import Link from "next/link";
import { Heart } from "lucide-react";
import type { Artist } from "@/types/artist";
import { cn } from "@/lib/cn";
import ArtistPortrait from "./ArtistPortrait";

interface ArtistCardProps {
  artist: Artist;
  /** 触发投票（不传则跳详情页） */
  onVote?: (artist: Artist) => void;
  className?: string;
}

function formatVotes(v: number): string {
  if (v >= 10_000) return `${(v / 10_000).toFixed(1)}w`;
  return v.toLocaleString();
}

export default function ArtistCard({
  artist,
  onVote,
  className,
}: ArtistCardProps) {
  // 「真正进 Top12」必须有票 —— 0 票时编号兜底排出来的前 12 不算
  const inTop12 = artist.rank <= 12 && artist.votes > 0;

  return (
    <div
      className={cn(
        "group relative rounded-xl overflow-hidden bg-grad-card border transition-all",
        inTop12
          ? "border-purple-500/55 shadow-[0_8px_32px_rgba(0,0,0,0.65),0_0_24px_rgba(139,92,246,0.25)]"
          : "border-white/[0.10] shadow-[0_8px_32px_rgba(0,0,0,0.65)]",
        "hover:-translate-y-1 hover:shadow-[0_12px_36px_rgba(0,0,0,0.7),0_0_24px_rgba(139,92,246,0.25)]",
        className,
      )}
    >
      <Link
        href={`/artist/${artist.id}`}
        className="block"
        aria-label={`查看 ${artist.name} 详情`}
      >
        {/* 立绘区（13+ 卡片轻度暗化） */}
        <div className={cn("relative aspect-[4/5]", !inTop12 && "opacity-[0.78]")}>
          <ArtistPortrait
            artist={artist}
            rounded="rounded-none"
            className="absolute inset-0"
          />

          {/* 排名徽章（左上独立紫色圆） */}
          <div
            className={cn(
              "absolute top-2 left-2 w-7 h-7 rounded-full font-display text-xs flex items-center justify-center text-white tabular-nums",
              inTop12
                ? "bg-purple-500 shadow-[0_0_12px_rgba(139,92,246,0.7)]"
                : "bg-elevated border border-white/15 text-white/55",
            )}
          >
            {artist.rank}
          </div>

          {/* 顶部轻微渐变蒙层 */}
          <div className="absolute inset-x-0 top-0 h-12 bg-gradient-to-b from-black/40 to-transparent pointer-events-none" />
        </div>

        {/* 信息区（黑色背景明显分隔） */}
        <div className="p-3 bg-black/40 border-t border-white/[0.06]">
          <div className="font-display text-[11px] text-white/55 tracking-wider">
            No.{artist.no}
          </div>
          <div className="mt-0.5 text-sm font-semibold text-white truncate">
            {artist.name}
          </div>
          <div className="text-[11px] text-white/55 truncate mt-0.5">
            {artist.enName}
          </div>
          <div
            className={cn(
              "mt-1.5 inline-flex items-center gap-1 font-display text-xs tabular-nums",
              inTop12 ? "text-purple-300" : "text-white/55",
            )}
          >
            <Heart size={12} fill="currentColor" />
            {formatVotes(artist.votes)} 票
          </div>
        </div>
      </Link>

      {/* 投票按钮（所有排名统一样式 · 紫色实心） */}
      <div className="px-3 pb-3 bg-black/40">
        <button
          type="button"
          onClick={(e) => {
            e.preventDefault();
            onVote?.(artist);
          }}
          className={cn(
            "w-full h-9 rounded-lg font-body font-semibold text-sm transition-all",
            "bg-grad-purple text-white shadow-[0_0_12px_rgba(139,92,246,0.35)]",
            "hover:brightness-110 active:brightness-95",
          )}
        >
          投票
        </button>
      </div>
    </div>
  );
}
