import { Check } from 'lucide-react';
import type { InputHTMLAttributes, ReactNode } from 'react';

import { Switch as BaseSwitch } from '@/components/ui/switch';
import { cn } from '@/lib/utils';

/**
 * Radio card: a full-width bordered card around a native radio input.
 * Selected state shows a 2px primary border, primary-soft background and a corner check badge.
 */
export function Radio({
  label,
  hint,
  icon,
  className,
  ...props
}: InputHTMLAttributes<HTMLInputElement> & { label: string; hint?: ReactNode; icon?: ReactNode }) {
  return (
    <label
      className={cn(
        'group relative flex cursor-pointer items-start gap-3 rounded-lg border border-border bg-surface p-4 transition-[border-color,background-color,box-shadow] duration-180',
        'hover:border-border-strong has-[:checked]:border-2 has-[:checked]:border-primary has-[:checked]:bg-primary-soft has-[:checked]:p-[15px]',
        'has-[:focus-visible]:shadow-glow',
        className,
      )}
    >
      <input type="radio" className="peer sr-only" {...props} />
      {icon === undefined ? null : (
        <span className="grid h-10 w-10 shrink-0 place-items-center rounded-lg bg-surface-muted text-muted-foreground transition-colors group-has-[:checked]:bg-surface group-has-[:checked]:text-primary [&>svg]:h-5 [&>svg]:w-5">
          {icon}
        </span>
      )}
      <span className="min-w-0 flex-1 pr-6">
        <span className="block text-body font-semibold text-foreground">{label}</span>
        {hint === undefined ? null : <span className="mt-0.5 block text-small text-muted-foreground">{hint}</span>}
      </span>
      <span
        aria-hidden="true"
        className="absolute right-3 top-3 grid h-5 w-5 scale-50 place-items-center rounded-full bg-primary text-primary-foreground opacity-0 transition duration-180 ease-brand peer-checked:scale-100 peer-checked:opacity-100"
      >
        <Check className="h-3 w-3" strokeWidth={3} />
      </span>
    </label>
  );
}

/** Labelled switch row (kept for existing callers). */
export function Switch({
  label,
  checked,
  onCheckedChange,
}: {
  label: string;
  checked: boolean;
  onCheckedChange: (checked: boolean) => void;
}) {
  return (
    <label className="inline-flex cursor-pointer items-center gap-3 text-body font-medium">
      <BaseSwitch checked={checked} onCheckedChange={onCheckedChange} label={label} />
      <span aria-hidden="true">{label}</span>
    </label>
  );
}
