import type { ComponentType, ReactNode, SVGProps } from "react";
import { cn } from "@/lib/utils";

type IconType = ComponentType<SVGProps<SVGSVGElement>>;

export type Tone = "brand" | "success" | "warning" | "danger" | "neutral";

const TONE_TEXT: Record<Tone, string> = {
  brand: "text-brand",
  success: "text-success",
  warning: "text-warning",
  danger: "text-danger",
  neutral: "text-white/60",
};

const TONE_SOFT: Record<Tone, string> = {
  brand: "bg-brand/12 text-brand",
  success: "bg-success/12 text-success",
  warning: "bg-warning/12 text-warning",
  danger: "bg-danger/12 text-danger",
  neutral: "bg-white/8 text-white/70",
};

const TONE_BAR: Record<Tone, string> = {
  brand: "bg-brand",
  success: "bg-success",
  warning: "bg-warning",
  danger: "bg-danger",
  neutral: "bg-white/30",
};

// Bare glass surface.
export function Card({ className, children, ...props }: React.ComponentProps<"div">) {
  return (
    <div className={cn("glass rounded-2xl", className)} {...props}>
      {children}
    </div>
  );
}

// Standard dashboard panel: titled header + body.
export function Panel({
  title,
  subtitle,
  action,
  children,
  className,
  bodyClassName,
}: {
  title: ReactNode;
  subtitle?: ReactNode;
  action?: ReactNode;
  children: ReactNode;
  className?: string;
  bodyClassName?: string;
}) {
  return (
    <section className={cn("glass flex flex-col rounded-2xl", className)}>
      <div className="flex items-center justify-between gap-3 border-b border-white/5 px-5 py-4">
        <div className="min-w-0">
          <h3 className="text-sm font-semibold text-white">{title}</h3>
          {subtitle && <p className="mt-0.5 text-xs text-white/45">{subtitle}</p>}
        </div>
        {action && <div className="shrink-0">{action}</div>}
      </div>
      <div className={cn("flex-1 p-5", bodyClassName)}>{children}</div>
    </section>
  );
}

// Small status pill.
export function Badge({
  children,
  tone = "neutral",
  className,
}: {
  children: ReactNode;
  tone?: Tone;
  className?: string;
}) {
  return (
    <span
      className={cn(
        "inline-flex items-center gap-1 rounded-full px-2.5 py-1 text-[11px] font-semibold",
        TONE_SOFT[tone],
        className,
      )}
    >
      {children}
    </span>
  );
}

// Round icon chip.
export function IconChip({
  icon: Icon,
  tone = "brand",
  className,
}: {
  icon: IconType;
  tone?: Tone;
  className?: string;
}) {
  return (
    <span
      className={cn(
        "grid h-10 w-10 shrink-0 place-items-center rounded-xl",
        TONE_SOFT[tone],
        className,
      )}
    >
      <Icon className="h-5 w-5" />
    </span>
  );
}

// Thin progress bar.
export function ProgressBar({
  value,
  tone = "brand",
  className,
}: {
  value: number;
  tone?: Tone;
  className?: string;
}) {
  return (
    <div className={cn("h-2 w-full overflow-hidden rounded-full bg-white/8", className)}>
      <div
        className={cn("h-full rounded-full transition-all", TONE_BAR[tone])}
        style={{ width: `${Math.min(100, Math.max(0, value))}%` }}
      />
    </div>
  );
}

// KPI metric card.
export function KpiCard({
  label,
  value,
  delta,
  deltaPositive,
  numericDelta,
  icon: Icon,
  tone = "brand",
}: {
  label: string;
  value: ReactNode;
  delta: string;
  deltaPositive: boolean;
  numericDelta: boolean;
  icon: IconType;
  tone?: Tone;
}) {
  return (
    <div className="glass card-hover rounded-2xl p-5">
      <div className="flex items-start justify-between gap-3">
        <span className="text-sm text-white/55">{label}</span>
        <span className={cn("grid h-9 w-9 place-items-center rounded-lg", TONE_SOFT[tone])}>
          <Icon className="h-4 w-4" />
        </span>
      </div>
      <div className="mt-3 text-3xl font-bold tracking-tight text-white">{value}</div>
      <div className="mt-2.5">
        <Badge tone={numericDelta ? (deltaPositive ? "success" : "danger") : "brand"}>
          {delta}
        </Badge>
      </div>
    </div>
  );
}

// Empty state.
export function EmptyState({
  icon: Icon,
  title,
  desc,
}: {
  icon: IconType;
  title: string;
  desc: string;
}) {
  return (
    <div className="px-6 py-12 text-center">
      <div className="mx-auto mb-3 grid h-12 w-12 place-items-center rounded-2xl bg-brand/10 text-brand">
        <Icon className="h-6 w-6" />
      </div>
      <h3 className="text-sm font-semibold text-white">{title}</h3>
      <p className="mx-auto mt-1 max-w-xs text-xs text-white/50">{desc}</p>
    </div>
  );
}

// Transient toast.
export function Toast({ message }: { message: string | null }) {
  if (!message) return null;
  return (
    <div
      role="status"
      className="animate-toast-in glass fixed bottom-6 left-1/2 z-50 w-[calc(100%-2rem)] max-w-sm -translate-x-1/2 rounded-2xl px-4 py-3 text-center text-sm text-white shadow-2xl sm:left-auto sm:right-6 sm:translate-x-0"
    >
      {message}
    </div>
  );
}

export { TONE_TEXT, TONE_SOFT, TONE_BAR };
