import Image from "next/image";
import { Check, Star } from "lucide-react";
import { cn } from "@/lib/cn";
import { StarRating } from "@/components/ui/StarRating";
import type { VpnProvider } from "@/lib/types";

interface VpnCardProps {
  provider: VpnProvider;
  /** Position in the ranked list. Omitted for the featured pick. */
  rank?: number;
}

/** Price plus two labelled terms, so the numbers read as data rather than stray text. */
function SpecPanel({
  priceFrom,
  refund,
  devices,
  featured,
}: {
  priceFrom: string;
  refund: string;
  devices: string;
  featured: boolean;
}) {
  const divider = featured ? "border-brand/15" : "border-border/70";
  const isRate = priceFrom.startsWith("$");

  return (
    <dl
      className={cn(
        "overflow-hidden rounded-[12px] border",
        featured ? "border-brand/25 bg-white/70" : "border-border/70 bg-bg-alt/50"
      )}
    >
      {/* Price leads the panel. A non-numeric value ("Free") takes no /mo suffix. */}
      <div className={cn("flex items-baseline justify-between gap-3 px-3.5 py-2.5", featured && "bg-brand-light/60")}>
        <dt className="text-[0.62rem] font-bold uppercase tracking-[0.09em] text-muted">
          From
        </dt>
        <dd className="flex items-baseline gap-0.5 leading-none text-brand">
          <span className="text-[1.15rem] font-extrabold">{priceFrom}</span>
          {isRate && <span className="text-[0.68rem] font-bold text-muted">/mo</span>}
        </dd>
      </div>

      {[
        { label: "Money-back", value: refund },
        { label: "Devices", value: devices },
      ].map((row) => (
        <div
          key={row.label}
          className={cn("flex items-baseline justify-between gap-3 border-t px-3.5 py-2.5", divider)}
        >
          <dt className="text-[0.62rem] font-bold uppercase tracking-[0.09em] text-muted">
            {row.label}
          </dt>
          <dd className="text-[0.88rem] font-extrabold leading-none text-ink">{row.value}</dd>
        </div>
      ))}
    </dl>
  );
}

export function VpnCard({ provider, rank }: VpnCardProps) {
  const featured = provider.isOurs === true;

  return (
    <article
      className={cn(
        "relative grid grid-cols-1 gap-5 rounded-[18px] p-5 transition-all duration-300",
        "md:grid-cols-[1fr_200px] md:items-center md:gap-7 md:p-6",
        featured
          ? "mt-3 border-2 border-brand bg-[linear-gradient(160deg,#EAF2FF_0%,#F7FAFF_38%,#FFFFFF_100%)] shadow-[0_24px_60px_-26px_rgba(10,132,255,0.55)]"
          : "border border-border/80 bg-white shadow-[0_2px_8px_-2px_rgba(0,0,0,0.04)] hover:-translate-y-1 hover:border-brand/30 hover:shadow-[0_18px_44px_-18px_rgba(10,132,255,0.18)]"
      )}
    >
      {featured && (
        <span className="absolute -top-[14px] left-1/2 inline-flex h-7 -translate-x-1/2 items-center gap-1.5 whitespace-nowrap rounded-full bg-gradient-to-r from-brand to-[#3b7bff] pl-2.5 pr-3.5 text-white shadow-[0_6px_18px_-6px_rgba(10,132,255,0.8)] ring-[3px] ring-background">
          <Star className="h-3.5 w-3.5 shrink-0" fill="currentColor" strokeWidth={0} />
          <span className="text-[0.68rem] font-extrabold uppercase leading-none tracking-[0.085em]">
            Editor&rsquo;s pick
          </span>
        </span>
      )}

      {/* Identity, rating, summary, specs */}
      <div className="min-w-0 text-center md:text-left">
        <div className="flex flex-col items-center gap-3 md:flex-row md:items-center">
          {/* Rank sits with the logo instead of floating in its own column. */}
          <span
            className={cn(
              "grid h-8 w-8 shrink-0 place-items-center rounded-[10px] text-[0.95rem] font-extrabold",
              featured
                ? "bg-gradient-to-br from-brand to-[#3b7bff] text-white shadow-[0_6px_16px_-6px_rgba(10,132,255,0.7)]"
                : "bg-brand-light text-brand ring-1 ring-brand/15"
            )}
          >
            {rank}
          </span>

          <Image
            src={provider.logo}
            alt={`${provider.name} app icon`}
            width={56}
            height={56}
            className={cn(
              "shrink-0 rounded-[14px]",
              featured
                ? "h-16 w-16 shadow-[0_10px_26px_-8px_rgba(10,132,255,0.75)] ring-2 ring-white"
                : "h-[52px] w-[52px] shadow-[0_4px_14px_-4px_rgba(15,23,42,0.35)]"
            )}
          />

          <div className="min-w-0">
            <h3
              className={cn(
                "font-extrabold leading-tight",
                featured ? "text-[1.32rem]" : "text-[1.18rem]"
              )}
            >
              {provider.name}
            </h3>
            <p className="mt-0.5 truncate text-[0.78rem] font-semibold text-muted">
              {provider.appStoreName || `by ${provider.developer}`}
            </p>
          </div>
        </div>

        <div className="mt-2.5 flex flex-wrap items-center justify-center gap-x-3 gap-y-1.5 md:justify-start">
          <StarRating rating={provider.rating} count={provider.ratingCount} />
          {featured && (
            <span className="inline-flex items-center gap-1.5 rounded-full bg-success-bg px-2.5 py-1 text-[0.72rem] font-bold text-success">
              <Check className="h-3 w-3" strokeWidth={3} />
              Our own app — scored on the same criteria
            </span>
          )}
        </div>

        <p className="mt-3 text-[0.92rem] leading-relaxed text-secondary">{provider.summary}</p>

        <ul className="mt-3.5 flex flex-wrap justify-center gap-x-5 gap-y-2 md:justify-start">
          {provider.highlights.map((point) => (
            <li
              key={point}
              className="flex items-center gap-1.5 text-[0.82rem] font-medium text-muted"
            >
              <span className="grid h-4 w-4 shrink-0 place-items-center rounded-full bg-success-bg">
                <Check className="h-2.5 w-2.5 text-success" strokeWidth={3.5} />
              </span>
              {point}
            </li>
          ))}
        </ul>
      </div>

      {/* Terms + CTA */}
      <div className="flex flex-col gap-3">
        <SpecPanel
          priceFrom={provider.priceFrom}
          refund={provider.refund}
          devices={provider.devices}
          featured={featured}
        />

        <a
          href={provider.url}
          target="_blank"
          rel="nofollow sponsored noopener"
          aria-label={`Visit ${provider.name}`}
          className={cn(
            "inline-flex w-full items-center justify-center gap-2 rounded-[12px] px-6 text-[0.95rem] font-extrabold",
            "outline-none transition-all duration-200 focus-visible:ring-2 focus-visible:ring-brand focus-visible:ring-offset-2",
            "hover:-translate-y-0.5",
            featured
              ? "bg-gradient-to-r from-brand to-[#3b7bff] py-3.5 text-white shadow-[0_12px_30px_-10px_rgba(10,132,255,0.85)] hover:shadow-[0_16px_38px_-10px_rgba(10,132,255,0.95)]"
              : "border-[1.5px] border-border bg-white py-3 text-brand hover:border-brand hover:bg-brand-light"
          )}
        >
          Visit
          <svg
            className="h-3.5 w-3.5"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth={2.5}
            aria-hidden="true"
          >
            <path strokeLinecap="round" strokeLinejoin="round" d="M9 5l7 7-7 7" />
          </svg>
        </a>
      </div>
    </article>
  );
}
