import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";

/**
 * Square, monospaced-weight controls.
 *
 * The primary action is the ink block; hovering it floods the signal accent,
 * exactly like the `.btn` treatment on the prospectus pages.
 */
const buttonVariants = cva(
  "inline-flex items-center justify-center gap-2 whitespace-nowrap font-medium transition-colors disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0",
  {
    variants: {
      variant: {
        primary:
          "bg-brand-600 text-inverse hover:bg-accent-500 hover:text-doc-paper active:bg-accent-600",
        secondary:
          "border border-subtle bg-surface text-primary hover:bg-hover active:bg-active",
        outline:
          "border border-line text-primary hover:border-brand-600 hover:bg-brand-600 hover:text-inverse active:bg-accent-500",
        ghost: "text-secondary hover:bg-hover hover:text-primary active:bg-active",
        danger: "bg-danger-600 text-inverse hover:bg-danger-700 active:bg-danger-700",
        success: "bg-success-600 text-inverse hover:bg-success-700 active:bg-success-700",
        subtle:
          "border border-subtle bg-sunken text-primary hover:bg-active",
        link: "text-accent-600 underline-offset-4 hover:underline dark:text-accent-400",
      },
      size: {
        sm: "h-8 px-3 text-xs [&_svg]:size-3.5",
        md: "h-10 px-4 text-sm [&_svg]:size-4",
        lg: "h-11 px-6 text-base [&_svg]:size-5",
        icon: "h-9 w-9 [&_svg]:size-4",
        "icon-sm": "h-7 w-7 [&_svg]:size-3.5",
      },
    },
    defaultVariants: { variant: "primary", size: "md" },
  }
);

export interface ButtonProps
  extends React.ButtonHTMLAttributes<HTMLButtonElement>,
    VariantProps<typeof buttonVariants> {
  asChild?: boolean;
  loading?: boolean;
}

export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  ({ className, variant, size, asChild = false, loading = false, disabled, children, ...props }, ref) => {
    const Comp = asChild ? Slot : "button";
    return (
      <Comp
        ref={ref}
        className={cn(buttonVariants({ variant, size }), className)}
        disabled={disabled || loading}
        {...props}
      >
        {loading ? (
          <>
            <Spinner />
            {children}
          </>
        ) : (
          children
        )}
      </Comp>
    );
  }
);
Button.displayName = "Button";

/** Inline spinner sized by the parent button's `[&_svg]` rule. */
function Spinner() {
  return (
    <svg viewBox="0 0 24 24" fill="none" aria-hidden="true" className="animate-spin">
      <circle cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="3" className="opacity-25" />
      <path
        d="M12 2a10 10 0 0 1 10 10"
        stroke="currentColor"
        strokeWidth="3"
        strokeLinecap="round"
      />
    </svg>
  );
}

export { buttonVariants };
