import { cn } from "@/lib/utils";

/* Shared UI for the auth forms (login / register): a brand-styled text field
   with optional password visibility toggle, an inline error, and a couple of
   inline stroke icons. Kept in one place so both forms stay consistent. */

export function FormField({
  id,
  label,
  type,
  placeholder,
  autoComplete,
  value,
  onChange,
  error,
  toggle,
}: {
  id: string;
  label: string;
  type: string;
  placeholder?: string;
  autoComplete?: string;
  value: string;
  onChange: (value: string) => void;
  error?: string;
  toggle?: { shown: boolean; onToggle: () => void };
}) {
  return (
    <div className="flex flex-col gap-1.5">
      <label htmlFor={id} className="text-sm font-medium text-white/80">
        {label}
      </label>
      <div className="relative">
        <input
          id={id}
          name={id}
          type={type}
          value={value}
          autoComplete={autoComplete}
          placeholder={placeholder}
          aria-invalid={!!error}
          aria-describedby={error ? `${id}-error` : undefined}
          onChange={(e) => onChange(e.target.value)}
          className={cn(
            "w-full rounded-xl border bg-white/5 px-4 py-3 text-sm text-white outline-none transition-colors placeholder:text-white/35",
            "focus:border-brand focus:ring-2 focus:ring-brand/30",
            toggle && "pr-11",
            error
              ? "border-danger/60 focus:border-danger focus:ring-danger/25"
              : "border-white/10",
          )}
        />
        {toggle && (
          <button
            type="button"
            onClick={toggle.onToggle}
            aria-label={toggle.shown ? "Hide password" : "Show password"}
            className="absolute right-3 top-1/2 -translate-y-1/2 text-white/45 transition-colors hover:text-white/80"
          >
            {toggle.shown ? <EyeOffIcon /> : <EyeIcon />}
          </button>
        )}
      </div>
      {error && <FieldError id={`${id}-error`}>{error}</FieldError>}
    </div>
  );
}

export function FieldError({
  children,
  id,
}: {
  children: React.ReactNode;
  id?: string;
}) {
  return (
    <p id={id} className="text-xs text-danger">
      {children}
    </p>
  );
}

/* ---------- inline icons (stroke-based, inherit currentColor) ---------- */

const iconBase = {
  width: 18,
  height: 18,
  viewBox: "0 0 24 24",
  fill: "none",
  stroke: "currentColor",
  strokeWidth: 1.75,
  strokeLinecap: "round" as const,
  strokeLinejoin: "round" as const,
};

function EyeIcon() {
  return (
    <svg {...iconBase}>
      <path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z" />
      <circle cx="12" cy="12" r="3" />
    </svg>
  );
}

function EyeOffIcon() {
  return (
    <svg {...iconBase}>
      <path d="M9.9 4.24A10.94 10.94 0 0 1 12 4c6.5 0 10 7 10 7a18.5 18.5 0 0 1-2.16 3.19M6.61 6.61A18.5 18.5 0 0 0 2 11s3.5 7 10 7a10.94 10.94 0 0 0 5.39-1.4" />
      <path d="m9.88 9.88 4.24 4.24M3 3l18 18" />
    </svg>
  );
}

export function CheckIcon() {
  return (
    <svg {...iconBase} width={28} height={28} strokeWidth={2}>
      <path d="M20 6 9 17l-5-5" />
    </svg>
  );
}

export function SpinnerIcon() {
  return (
    <svg {...iconBase} className="animate-spin">
      <path d="M21 12a9 9 0 1 1-6.22-8.56" />
    </svg>
  );
}
