import type { ReactNode } from "react";

/**
 * Form primitives for the prospectus-styled pages (login, register).
 *
 * These mirror the LMS `Field` API but render the printed treatments defined
 * under `.doc-root` — monospaced uppercase labels, hairline square inputs and a
 * signal-coloured error slot. Keeping them separate means the dashboard forms
 * are untouched.
 */

export function DocField({
  label,
  htmlFor,
  hint,
  error,
  required,
  counter,
  children,
}: {
  label: string;
  htmlFor: string;
  hint?: string;
  error?: string;
  required?: boolean;
  /** Right-aligned annotation on the label row, e.g. "Opsional". */
  counter?: string;
  children: ReactNode;
}) {
  return (
    <div className="space-y-2">
      <div className="flex items-baseline justify-between gap-3">
        <label htmlFor={htmlFor} className="doc-label">
          {label}
          {required ? <span className="ml-1 text-doc-signal">*</span> : null}
        </label>
        {counter ? <span className="doc-label">{counter}</span> : null}
      </div>
      {children}
      {error ? (
        <p className="doc-error" role="alert">
          {error}
        </p>
      ) : hint ? (
        <p className="doc-hint">{hint}</p>
      ) : null}
    </div>
  );
}

/** Square input styled for the prospectus pages. */
export function DocInput({
  className = "",
  ...props
}: React.InputHTMLAttributes<HTMLInputElement>) {
  return <input className={`doc-field ${className}`.trim()} {...props} />;
}

/** Square select styled for the prospectus pages. */
export function DocSelect({
  className = "",
  children,
  ...props
}: React.SelectHTMLAttributes<HTMLSelectElement>) {
  return (
    <select className={`doc-field ${className}`.trim()} {...props}>
      {children}
    </select>
  );
}

/** Form-level failure banner. */
export function DocNotice({ children }: { children: ReactNode }) {
  return (
    <div className="doc-notice" role="alert">
      <span className="mt-0.5 shrink-0 text-doc-signal" aria-hidden>
        <svg viewBox="0 0 24 24" className="size-4" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
          <path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3" />
          <path d="M12 9v4" />
          <path d="M12 17h.01" />
        </svg>
      </span>
      <div className="min-w-0 flex-1">{children}</div>
    </div>
  );
}
