import { Link } from '@inertiajs/react';
import type { ButtonHTMLAttributes, ComponentProps, ReactNode } from 'react';
import { cn } from '@/lib/utils';

type ActionVariant =
    | 'primary'
    | 'signal'
    | 'dark'
    | 'light'
    | 'outline'
    | 'hover-outline'
    | 'hover-outline-inverse';
type ActionSize = 'sm' | 'md' | 'lg';

const variants: Record<ActionVariant, string> = {
    primary: 'bg-[#e8422b] text-white hover:bg-[#c83221]',
    signal: 'bg-[#f6c84f] text-[#111516] hover:bg-white',
    dark: 'bg-[#111516] text-white hover:bg-[#30383a]',
    light: 'bg-white text-[#111516] hover:bg-[#f4f3ef]',
    outline:
        'border border-[#111516]/20 bg-transparent text-[#111516] hover:border-[#e8422b] hover:text-[#e8422b]',
    'hover-outline':
        'border border-transparent bg-[#e8422b]/[.07] text-[#b92d21] hover:border-[#e8422b] hover:bg-[#e8422b]/[.11] hover:text-[#a9251c]',
    'hover-outline-inverse':
        'border border-transparent bg-white/[.08] text-white hover:border-[#f6c84f] hover:bg-white/[.14] hover:text-[#f6c84f]',
};

const sizes: Record<ActionSize, string> = {
    sm: 'min-h-9 rounded-lg px-3.5 py-2 text-xs',
    md: 'min-h-11 rounded-xl px-5 py-3 text-sm',
    lg: 'min-h-13 rounded-xl px-6 py-3.5 text-base',
};

function actionClass(
    variant: ActionVariant,
    size: ActionSize,
    className?: string,
) {
    return cn(
        'group inline-flex w-fit items-center justify-center gap-2 font-black whitespace-nowrap transition-colors duration-200 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[#e8422b] disabled:pointer-events-none disabled:opacity-50',
        sizes[size],
        variants[variant],
        className,
    );
}

export function SiteActionLink({
    href,
    children,
    variant = 'primary',
    size = 'md',
    className,
    onClick,
}: {
    href: string;
    children: ReactNode;
    variant?: ActionVariant;
    size?: ActionSize;
    className?: string;
    onClick?: ComponentProps<typeof Link>['onClick'];
}) {
    return (
        <Link
            href={href}
            onClick={onClick}
            className={actionClass(variant, size, className)}
        >
            {children}
        </Link>
    );
}

export function SiteButton({
    children,
    variant = 'primary',
    size = 'md',
    className,
    type = 'button',
    ...props
}: Omit<
    ButtonHTMLAttributes<HTMLButtonElement>,
    'size' | 'className' | 'children' | 'type'
> & {
    className?: string;
    children: ReactNode;
    type?: ButtonHTMLAttributes<HTMLButtonElement>['type'];
    variant?: ActionVariant;
    size?: ActionSize;
}) {
    return (
        <button
            type={type}
            className={actionClass(variant, size, className)}
            {...props}
        >
            {children}
        </button>
    );
}
