import { Button as ButtonPrimitive } from "@base-ui/react/button"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"

/*
 * shadcn/ui Button, extended with the AlReyada design (Figma):
 *   variant="brand"  + size="pill"  → purple gradient pill  ("Get Started", "Subscribe")
 *   variant="white"  + size="pill"  → white→lavender pill   ("Explore Service", "Send Request")
 *   variant="light"  + size="pill"  → solid light pill      ("Request Service")
 *   variant="text"   + size="text"  → text link with icon   ("See All")
 *   variant="glass"  + size="icon-xl" → round outlined icon  (language switcher)
 * To render a link: <Button render={<Link href="…" />} nativeButton={false}>…</Button>
 */
const buttonVariantsBase = cva(
  "group/button inline-flex shrink-0 items-center justify-center rounded-lg border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
  {
    variants: {
      variant: {
        default: "bg-primary text-primary-foreground hover:bg-primary/80",
        outline:
          "border-border bg-background hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
        secondary:
          "bg-secondary text-secondary-foreground hover:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)] aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
        ghost:
          "hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
        destructive:
          "bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
        link: "text-primary underline-offset-4 hover:underline",

        /* ── AlReyada ── */
        brand: "bg-gradient-primary text-white hover:brightness-110",
        white: "bg-gradient-white text-primary-darker hover:brightness-95",
        light: "bg-secondary font-semibold text-secondary-foreground hover:bg-white",
        text: "text-white hover:text-lavender",
        glass: "border-white/40 bg-subtle/16 text-white hover:bg-white/20",
      },
      size: {
        default:
          "h-8 gap-1.5 px-2.5 has-data-[icon=inline-end]:pe-2 has-data-[icon=inline-start]:ps-2",
        xs: "h-6 gap-1 rounded-[min(var(--radius-md),10px)] px-2 text-xs in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pe-1.5 has-data-[icon=inline-start]:ps-1.5 [&_svg:not([class*='size-'])]:size-3",
        sm: "h-7 gap-1 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pe-1.5 has-data-[icon=inline-start]:ps-1.5 [&_svg:not([class*='size-'])]:size-3.5",
        lg: "h-9 gap-1.5 px-2.5 has-data-[icon=inline-end]:pe-2 has-data-[icon=inline-start]:ps-2",
        icon: "size-8",
        "icon-xs":
          "size-6 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-lg [&_svg:not([class*='size-'])]:size-3",
        "icon-sm":
          "size-7 rounded-[min(var(--radius-md),12px)] in-data-[slot=button-group]:rounded-lg",
        "icon-lg": "size-9",

        /* ── AlReyada ── */
        pill: "h-auto gap-2 rounded-full px-[22px] py-3 text-lg font-bold tracking-[0.5px] [&_svg:not([class*='size-'])]:size-[18px]",
        "pill-lg": "h-auto gap-2 rounded-full px-8 py-4 text-lg leading-5 font-bold [&_svg:not([class*='size-'])]:size-[18px]",
        text: "h-auto gap-2 px-3 py-2 text-sm font-bold tracking-[0.5px]",
        "icon-xl": "size-12 rounded-full [&_svg:not([class*='size-'])]:size-8",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
)

/**
 * cva only concatenates classes, so defaults like `rounded-lg text-sm` would clash with
 * `rounded-full text-lg` from our sizes. Run the result through `cn` (tailwind-merge) so the
 * later class wins — this matters when styling a <Link> directly with buttonVariants().
 */
function buttonVariants(props?: Parameters<typeof buttonVariantsBase>[0]) {
  return cn(buttonVariantsBase(props))
}

function Button({
  className,
  variant = "default",
  size = "default",
  ...props
}: ButtonPrimitive.Props & VariantProps<typeof buttonVariantsBase>) {
  return (
    <ButtonPrimitive
      data-slot="button"
      className={cn(buttonVariants({ variant, size, className }))}
      {...props}
    />
  )
}

export { Button, buttonVariants }
