"use client";

import { cn } from "@/shared/lib/utils";
import { useT } from "@/shared/i18n/client";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/presentation/components/ui/tooltip";
import {
  COMPLIANCE_RULE_BY_KEY,
  COMPLIANCE_SEVERITY_CLASSES,
  COMPLIANCE_STATUS_CLASSES,
  complianceScoreColor,
} from "@/shared/constants/compliance";
import type { ComplianceSeverity, ComplianceStatus } from "@/domain/entities/compliance";

/** Label of a rule key, falling back to the key itself for rules the UI doesn't know yet. */
export function useRuleLabel() {
  const t = useT();
  const labels = t.compliance.rules as Record<string, string>;
  return (key: string) => labels[key] ?? key;
}

export function ComplianceStatusBadge({ status, className }: { status: ComplianceStatus; className?: string }) {
  const t = useT();
  return (
    <span
      className={cn(
        "inline-flex items-center rounded-md border px-2 py-0.5 text-[11px] font-semibold uppercase tracking-wide whitespace-nowrap",
        COMPLIANCE_STATUS_CLASSES[status],
        className,
      )}
    >
      {t.compliance.status[status]}
    </span>
  );
}

export function ComplianceScoreBar({ score, className, wide }: { score: number; className?: string; wide?: boolean }) {
  return (
    <div className={cn("flex items-center gap-2", className)}>
      <div className={cn("h-1.5 rounded-full bg-muted overflow-hidden", wide ? "flex-1" : "w-20")}>
        <div className={cn("h-full rounded-full", complianceScoreColor(score))} style={{ width: `${score}%` }} />
      </div>
      <span className="font-mono text-xs font-semibold tabular-nums w-9 text-right">{score}%</span>
    </div>
  );
}

export function SeverityTag({ severity, className }: { severity: ComplianceSeverity; className?: string }) {
  const t = useT();
  return (
    <span
      className={cn(
        "inline-flex items-center rounded border px-1.5 py-0 text-[10px] font-semibold uppercase tracking-wide",
        COMPLIANCE_SEVERITY_CLASSES[severity],
        className,
      )}
    >
      {t.compliance.severity[severity]}
    </span>
  );
}

/** Missing-rule tags coloured by severity; overflow is summarised as "+N" with a tooltip. */
export function ComplianceMissingTags({ missing, max = 3 }: { missing: string[]; max?: number }) {
  const t = useT();
  const label = useRuleLabel();
  const visible = missing.filter((k) => COMPLIANCE_RULE_BY_KEY[k]?.severity !== "low");
  if (visible.length === 0) {
    return <span className="text-xs text-emerald-600 font-medium">{t.compliance.noMissing}</span>;
  }
  const shown = visible.slice(0, max);
  const rest = visible.slice(max);
  return (
    <div className="flex flex-wrap items-center gap-1 min-w-[260px] max-w-[360px]">
      {shown.map((key) => (
        <span
          key={key}
          className={cn(
            "inline-flex items-center rounded-md border px-1.5 py-0.5 text-[11px] font-medium whitespace-nowrap",
            COMPLIANCE_SEVERITY_CLASSES[COMPLIANCE_RULE_BY_KEY[key]?.severity ?? "medium"],
          )}
        >
          {label(key)}
        </span>
      ))}
      {rest.length > 0 && (
        <Tooltip>
          <TooltipTrigger asChild>
            <span className="inline-flex items-center rounded-md border border-border bg-muted px-1.5 py-0.5 text-[11px] font-semibold text-muted-foreground cursor-default">
              +{rest.length}
            </span>
          </TooltipTrigger>
          <TooltipContent className="max-w-xs">
            <ul className="space-y-0.5">
              {rest.map((key) => (
                <li key={key}>• {label(key)}</li>
              ))}
            </ul>
          </TooltipContent>
        </Tooltip>
      )}
    </div>
  );
}
