import type { ComponentProps, ReactNode } from 'react';
import { cx } from '@/site/lib/cx';
import { ArrowRight } from './icons';
import { SiteLink } from './link';

type Variant = 'solid' | 'outline' | 'ghost' | 'whatsapp';
type Size = 'md' | 'lg';

const base =
    'group/btn inline-flex items-center justify-center gap-3 rounded-full font-medium tracking-tight whitespace-nowrap transition-[background-color,color,border-color,transform] duration-300 ease-out-expo disabled:pointer-events-none disabled:opacity-50';

const variants: Record<Variant, string> = {
    // Inverts the stage: light pill on dark sections, dark pill on light ones.
    solid: 'bg-fg text-stage hover:bg-accent hover:text-ink',
    outline:
        'border border-line-strong text-fg hover:border-fg hover:bg-fg hover:text-stage',
    ghost: 'text-fg hover:text-accent-fg',
    // WhatsApp's own green, the same on every stage: the buttons that take
    // the visitor to the chat (navy on it reads at 7.7:1).
    whatsapp: 'bg-whatsapp text-ink hover:bg-whatsapp-soft',
};

const sizes: Record<Size, string> = {
    md: 'h-11 px-6 text-sm',
    lg: 'h-14 px-8 text-base',
};

function classes(variant: Variant, size: Size, className?: string): string {
    return cx(
        base,
        variants[variant],
        variant !== 'ghost' && sizes[size],
        className,
    );
}

function Content({ children, arrow }: { children: ReactNode; arrow: boolean }) {
    return (
        <>
            <span>{children}</span>
            {arrow ? (
                <ArrowRight className="ease-out-expo size-[1.1em] transition-transform duration-500 group-hover/btn:translate-x-1 rtl:-scale-x-100 rtl:group-hover/btn:-translate-x-1" />
            ) : null}
        </>
    );
}

type Common = {
    variant?: Variant;
    size?: Size;
    /** Show the trailing arrow (default: yes). */
    arrow?: boolean;
    children: ReactNode;
    className?: string;
};

/** A link that looks like a button. Internal navigation (Inertia). */
export function ButtonLink({
    href,
    variant = 'solid',
    size = 'md',
    arrow = true,
    children,
    className,
    ...props
}: Common & { href: string } & Omit<
        ComponentProps<typeof SiteLink>,
        'href' | 'className' | 'children' | 'size'
    >) {
    return (
        <SiteLink
            href={href}
            className={classes(variant, size, className)}
            {...props}
        >
            <Content arrow={arrow}>{children}</Content>
        </SiteLink>
    );
}

/** A plain <a> that looks like a button — for external targets (tel:, WhatsApp…). */
export function ButtonAnchor({
    variant = 'solid',
    size = 'md',
    arrow = true,
    children,
    className,
    ...props
}: Common & Omit<ComponentProps<'a'>, 'className' | 'children'>) {
    return (
        <a className={classes(variant, size, className)} {...props}>
            <Content arrow={arrow}>{children}</Content>
        </a>
    );
}

export function Button({
    variant = 'solid',
    size = 'md',
    arrow = false,
    children,
    className,
    type = 'button',
    ...props
}: Common & Omit<ComponentProps<'button'>, 'className' | 'children'>) {
    return (
        <button
            type={type}
            className={classes(variant, size, className)}
            {...props}
        >
            <Content arrow={arrow}>{children}</Content>
        </button>
    );
}
