"use client";

import { Loader2, TriangleAlert, Users } from "lucide-react";
import { cn } from "@/shared/lib/utils";
import { useT } from "@/shared/i18n/client";
import type { AudienceCountState } from "@/presentation/components/announcements/use-audience-count";

/** "N users match", with the 0 / everybody warnings (spec §7.2). */
export function AudienceCount({ state, compact }: { state: AudienceCountState; compact?: boolean }) {
  const t = useT();
  const a = t.announcements.audience;
  const { count, counting } = state;
  const zero = count !== null && count.count === 0;
  const everyone = count !== null && count.total > 0 && count.count === count.total;

  return (
    <div className="space-y-2" aria-live="polite">
      <div
        className={cn(
          "flex items-center gap-2 rounded-lg border px-3 py-2",
          zero ? "border-red-200 bg-red-50 text-red-800" : everyone ? "border-amber-200 bg-amber-50 text-amber-900" : "border-blue-200 bg-blue-50 text-blue-900",
        )}
      >
        <Users className="h-4 w-4 shrink-0" />
        {count === null ? (
          <span className="text-sm">{a.counting}</span>
        ) : (
          <span className={cn("font-semibold tabular-nums", compact ? "text-sm" : "text-base")}>
            {a.match(count.count)} <span className="font-normal opacity-70">{a.ofTotal(count.total)}</span>
          </span>
        )}
        {counting && <Loader2 className="ml-auto h-3.5 w-3.5 animate-spin opacity-60" aria-label={a.counting} />}
      </div>
      {(zero || everyone) && (
        <p className={cn("flex items-start gap-1.5 text-xs", zero ? "text-red-700" : "text-amber-800")}>
          <TriangleAlert className="mt-0.5 h-3.5 w-3.5 shrink-0" />
          {zero ? a.warnZero : a.warnEveryone}
        </p>
      )}
    </div>
  );
}
