import Link from "next/link";

import type { QualityBreakdown } from "@/lib/quality/score";

type Props = {
  quality: QualityBreakdown | null;
  scoredAt: Date | null;
};

const FACTOR_LINKS: Record<string, string | null> = {
  logo: "/portal/profil",
  description: "/portal/profil",
  photos: "/portal/profil",
  phone: "/portal/profil",
  hours: "/portal/profil",
  reviews5: null,
  rating4: null,
  verified: "/portal/verifikacija",
  optional90: "/portal/profil",
};

function scoreTone(score: number) {
  if (score <= 39) return { stroke: "#f43f5e", text: "text-rose-600" };
  if (score <= 69) return { stroke: "#f59e0b", text: "text-amber-600" };
  return { stroke: "#10b981", text: "text-emerald-600" };
}

/**
 * Server-side rendered profile-quality card for the owner dashboard.
 * Falls back to a friendly empty state when the score has not been computed yet.
 */
export function QualityScoreCard({ quality, scoredAt }: Props) {
  if (!quality || !scoredAt) {
    return (
      <section className="partner-app-panel">
        <p className="eyebrow">Kvalitet profila</p>
        <h3 className="sidebar-panel-title mt-3">Skor se još računa</h3>
        <p className="sidebar-panel-copy mt-3">
          Kvalitet profila se računa svakih nekoliko sati. Vrati se uskoro.
        </p>
      </section>
    );
  }

  const { score, factors } = quality;
  const tone = scoreTone(score);
  const radius = 52;
  const circumference = 2 * Math.PI * radius;
  const dashOffset = circumference * (1 - Math.max(0, Math.min(100, score)) / 100);

  const achieved = factors.filter((f) => f.ok);
  const missing = factors.filter((f) => !f.ok);

  return (
    <section className="partner-app-panel">
      <p className="eyebrow">Kvalitet profila</p>
      <div className="mt-4 flex flex-wrap items-center gap-6">
        <div className="relative h-32 w-32 shrink-0">
          <svg viewBox="0 0 120 120" className="h-full w-full -rotate-90">
            <circle
              cx="60"
              cy="60"
              r={radius}
              fill="none"
              stroke="#e5e7eb"
              strokeWidth="10"
            />
            <circle
              cx="60"
              cy="60"
              r={radius}
              fill="none"
              stroke={tone.stroke}
              strokeWidth="10"
              strokeLinecap="round"
              strokeDasharray={circumference}
              strokeDashoffset={dashOffset}
            />
          </svg>
          <div className="absolute inset-0 flex flex-col items-center justify-center">
            <span className={`text-3xl font-semibold ${tone.text}`}>{score}</span>
            <span className="text-[10px] uppercase tracking-wider text-muted-foreground">
              / 100
            </span>
          </div>
        </div>
        <div className="min-w-0 flex-1">
          <h3 className="sidebar-panel-title">Kvalitet profila: {score}/100</h3>
          <p className="sidebar-panel-copy mt-2">
            Skor se zasniva na kompletnosti profila, recenzijama i verifikaciji.
          </p>
        </div>
      </div>

      <ul className="mt-5 space-y-2 text-sm">
        {achieved.map((factor) => (
          <li key={factor.key} className="flex items-center gap-2">
            <span aria-hidden className="text-emerald-500">✓</span>
            <span className="text-muted-foreground line-through">{factor.label}</span>
          </li>
        ))}
        {missing.map((factor) => {
          const href = FACTOR_LINKS[factor.key];
          return (
            <li key={factor.key} className="flex flex-wrap items-center gap-2">
              <span aria-hidden className="text-amber-500">→</span>
              <span className="font-medium text-foreground">{factor.label}</span>
              {href ? (
                <Link
                  href={href}
                  className="ml-auto inline-flex items-center rounded-full border border-primary/40 px-3 py-0.5 text-xs font-semibold text-primary hover:bg-primary hover:text-white"
                >
                  Popravi
                </Link>
              ) : null}
            </li>
          );
        })}
      </ul>
    </section>
  );
}
